How to replace black background with white when resizing/converting PNG images with transparent backgrounds to JPEG.

后端 未结 5 2130
执念已碎
执念已碎 2020-12-14 17:45

I am using a script that lets users upload images. The script resizes and converts the images to JPEG.

The problem I have is when a PNG with transparency is uploaded

5条回答
  •  一整个雨季
    2020-12-14 18:15

    Try like this (not tested):

    case "png":
      $file = imagecreatetruecolor($width, $height);
      $new = imagecreatefrompng($this->file_tempname);
      imagefilledrectangle ($file, 0, 0, $width, $height, imagecolorallocate($file, 0,0,0))
    

    (predraw a white background on $file image)

    Also, the for($i=0; $i<256; $i++) { imagecolorallocate($file, $i, $i, $i); } part looks strange.

提交回复
热议问题