900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > php改变图片大小png背景变黑 php – 当将透明背景的PNG图像调整大小/转换为JPEG时

php改变图片大小png背景变黑 php – 当将透明背景的PNG图像调整大小/转换为JPEG时

时间:2022-11-28 07:21:34

相关推荐

php改变图片大小png背景变黑 php – 当将透明背景的PNG图像调整大小/转换为JPEG时

我正在使用一个脚本,让用户上传图像。脚本调整大小并将图像转换为JPEG。

我遇到的问题是上传透明度的PNG时,生成的JPEG图像是透明度为黑色的图像。

如何编辑下面的脚本来替换黑色的白色?它已经为GIF而做,但不是PNG的。

// RESIZE IMAGE AND PUT IN USER DIRECTORY

switch($this->file_ext)

{

case "gif":

$file = imagecreatetruecolor($width, $height);

$new = imagecreatefromgif($this->file_tempname);

$kek=imagecolorallocate($file, 255, 255, 255);

imagefill($file,0,0,$kek);

imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height);

imagejpeg($file, $photo_dest, 100);

ImageDestroy($new);

ImageDestroy($file);

break;

case "bmp":

$file = imagecreatetruecolor($width, $height);

$new = $this->imagecreatefrombmp($this->file_tempname);

for($i=0; $i<256; $i++) { imagecolorallocate($file, $i, $i, $i); }

imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height);

imagejpeg($file, $photo_dest, 100);

ImageDestroy($new);

ImageDestroy($file);

break;

case "jpeg":

case "jpg":

$file = imagecreatetruecolor($width, $height);

$new = imagecreatefromjpeg($this->file_tempname);

for($i=0; $i<256; $i++) { imagecolorallocate($file, $i, $i, $i); }

imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height);

imagejpeg($file, $photo_dest, 100);

ImageDestroy($new);

ImageDestroy($file);

break;

case "png":

$file = imagecreatetruecolor($width, $height);

$new = imagecreatefrompng($this->file_tempname);

for($i=0; $i<256; $i++) { imagecolorallocate($file, $i, $i, $i); }

imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height);

imagejpeg($file, $photo_dest, 100);

ImageDestroy($new);

ImageDestroy($file);

break;

}

chmod($photo_dest, 0777);

return true;

}

我尝试编辑案例“png”:部分匹配案例“gif”:代码,但生成的JPEG是完全白色的。

更新:

我自己修好了

谢谢大家,贡献!

我换了

case "png":

$file = imagecreatetruecolor($width, $height);

$new = imagecreatefrompng($this->file_tempname);

for($i=0; $i<256; $i++) { imagecolorallocate($file, $i, $i, $i); }

imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height);

imagejpeg($file, $photo_dest, 100);

ImageDestroy($new);

ImageDestroy($file);

break;

有:

case "png":

$file = imagecreatetruecolor($width, $height);

$new = imagecreatefrompng($this->file_tempname);

$kek=imagecolorallocate($file, 255, 255, 255);

imagefill($file,0,0,$kek);

imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height);

imagejpeg($file, $photo_dest, 100);

ImageDestroy($new);

ImageDestroy($file);

break;

php改变图片大小png背景变黑 php – 当将透明背景的PNG图像调整大小/转换为JPEG时 如何用白色替换黑色背景...

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。