小编Jef*_*eff的帖子

在将具有透明背景的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 …
Run Code Online (Sandbox Code Playgroud)

php gd resize image

20
推荐指数
1
解决办法
2万
查看次数

标签 统计

gd ×1

image ×1

php ×1

resize ×1