使用 imagecolorallocate (PHP) 重新制作图像但得到不同的颜色
Remake images using imagecolorallocate (PHP) but getting different color
public function createNewImage222($image){
$image = imagecreatefromjpeg($image);
$width = imagesx($image);
$height = imagesy($image);
$img = imagecreatetruecolor($width,$height);
for ($x = 0; $x <$width; $x++)
{
for ($y = 0; $y <$height ; $y++){
$rgb = imagecolorat($image, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$color = imagecolorallocate($img,$r,$g,$b);
imagesetpixel($img,$x,$y,$color);
}
}
$new_img = imagecreate($width,$height);
imagecopyresized($new_img,$img,0,0,0,0,$width,$height,$width,$height);
return $new_img;}
这是我的代码并显示结果使用:
header('Content-Type: image/jpeg');
imagejpeg($result,null,100)
imagedestroy($result);
但颜色和尺寸有所不同。原始图像 13KB,结果 40KB。抱歉,我无法 post 图片。
$new_img = imagecreatetruecolor($width,$height);
http://php.net/manual/en/function.imagecreate.php
recommend the use of imagecreatetruecolor()
instead of imagecreate()
public function createNewImage222($image){
$image = imagecreatefromjpeg($image);
$width = imagesx($image);
$height = imagesy($image);
$img = imagecreatetruecolor($width,$height);
for ($x = 0; $x <$width; $x++)
{
for ($y = 0; $y <$height ; $y++){
$rgb = imagecolorat($image, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$color = imagecolorallocate($img,$r,$g,$b);
imagesetpixel($img,$x,$y,$color);
}
}
$new_img = imagecreate($width,$height);
imagecopyresized($new_img,$img,0,0,0,0,$width,$height,$width,$height);
return $new_img;}
这是我的代码并显示结果使用:
header('Content-Type: image/jpeg');
imagejpeg($result,null,100)
imagedestroy($result);
但颜色和尺寸有所不同。原始图像 13KB,结果 40KB。抱歉,我无法 post 图片。
$new_img = imagecreatetruecolor($width,$height);
http://php.net/manual/en/function.imagecreate.php
recommend the use of
imagecreatetruecolor()
instead ofimagecreate()