를 읽을 GD I 다음 PHP 스크립트가 : 인수로 원시 이진 데이터를 얻을 수 읽기와 .PNG 파일로이 저장하도록되어PHP는 원시 픽셀 데이터
<?php
class GimFile {
public $data = '';
public $header = '';
public $rgba = '';
public $width = 0;
public $height = 0;
public function __construct($imagedata) {
$this->data = $imagedata;
if (substr($this->data, 1, 3) != 'GIM') {
exit("This data is not in GIM format");
}
$this->header = substr($this->data, 0, 128);
$this->rgba = substr($this->data, 128);
// PHP can't unpack signed short from big-endian
// so we unpack it as unsigned, and subtract 2^16 if >2^15
$dimensions = array_values(unpack("n2", substr($this->header, 72, 76)));
for($i = 0; $i < count($dimensions); $i++) {
if($dimensions[$i] >= pow(2, 15)) {
$dimensions[$i] -= pow(2, 16);
}
}
list($this->width, $this->height) = $dimensions;
}
public function save($dest) {
//create image
$img = imagecreatetruecolor($this->width, $this->height);
//fill by iterating through your raw pixel data
for($x = 0; $x < $this->width; $x++) {
for($y = 0; $y < $this->height; $y++) {
$pos = ($y * $this->width + $x) * 4;
list($red, $green, $blue, $alpha) = array_values(unpack("C4", substr($this->rgba, $pos, $pos+4)));
$alpha >>= 1; // alpha transprancy is saved as 8bit, we need 7 bit
$color = imagecolorallocatealpha ($img, $red, $green, $blue, $alpha);
imagesetpixel($img, $x, $y, $color);
}
}
imagepng($img, $dest);
imagedestroy($img);
}
}
header('Content-type: image/png');
$contents = file_get_contents('gim');
$gim = new GimFile($contents);
$gim->save('lol.png');
?>
. 이 데이터에는 다음이 포함됩니다.
첫 번째 128 바이트는 머리글이며 그 중 오프셋 72 및 74에는 너비와 높이가 각각 포함됩니다 (부호있는 짧은 빅 엔디안). 이 데이터는 생성자에서 구문 분석됩니다. 나머지 데이터는 원시 RGBA 데이터입니다.
save 함수는 원시 RGBA 데이터 (모든 픽셀을 반복)를 통과하여 색상을 찾고 같은 너비와 높이의 새 이미지에 씁니다. 그것을 파일에 저장합니다.
그러나 어떤 이유로 든 결과가 잘못되었습니다. http://2971.a.hostable.me/gim/gim
다음 PNG가 예상된다 : http://2971.a.hostable.me/gim/expected.png
다음 PNG가 생성됩니다
다음 파일을 데이터가 필요한 포함하는 데 사용됩니다 http://2971.a.hostable.me/gim/output.png
는 가장 가까운에 내가 예상 된 결과에 와서, 설정했다 $ 알파를 0으로 설정했지만 결과는 다음과 같습니다. http://2971.a.hostable.me/gim/noalpha.png
누구든지 예상되는 결과를 산출하기 위해 스크립트를 수정하는 데 도움을 줄 수 있습니까?
미리 감사드립니다.
내가 메시지를 가지고 대신 이미지의
nvm, 이미 이걸 가지고 있습니다. 저장된 알파 투명도는 실제로 8 비트 정수 였고 0은 완전히 투명했고 255는 완전히 불투명 한 것으로 보입니다. 'imagecolorallocatealpha()'와 완전히 반대입니다. 그래서 나는'$ alpha'에서 255를 제거하고, 부호를 바꿔 비트를 오른쪽으로 한 번 바꿨습니다. 매력처럼 일했습니다. 나는 심지어 당신이 대답하기를 귀찮게하는 유일한 사람 (여러 이사회에서)이었던 것처럼 당신의 대답을 올바른 것으로 받아 들일 것입니다. 어쨌든 고마워. :) 최종 코드 :'$ alpha = ((int) (substr ($ alpha - 255, 1))) >> 1;' –