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


i using script lets users upload images. script resizes , converts images jpeg.

the problem have when png transparency uploaded, resulting jpeg image black there transparency.

how can edit below script replace black white? gif's not png's.

 // resize image , 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; } 

i tried editing case "png": portion match of case "gif": code resulting jpeg white.

update:

i fixed myself.

thanks, everyone, contributing!

i replaced:

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; 

with:

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; 

i found other similar question here on site. hope helpful.

adding background color transparent images using gd , php


Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -