2017-02-27 379 views
1

我上傳圖像和調整大小,但在PNG上,它顯示黑色背景。PNG圖像顯示上傳/調整大小的黑色背景

你可以檢查一下代碼,讓我知道是什麼問題?

$newImageWidth = ceil($width * $scale); 
     $newImageHeight = ceil($height * $scale); 
     $newImage = imagecreatetruecolor($newImageWidth,$newImageHeight); 
     if ($ext == "jpg" || $ext == "jpeg") 
     { 
      $source = imagecreatefromjpeg($image); 
     } 
     else 
     if ($ext == "png") 
     { 
      $source = imagecreatefrompng($image); 
     } 
     else 
     { 
      $source = imagecreatefromgif($image); 
     } 

     imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height); 
     imagejpeg($newImage,$image,90); 
     chmod($image, 0777); 
     return $image; 

回答

1

答:

將此代碼添加imagecopyresampled前()函數

$tmp = imagecreatetruecolor($new_width,$new_height); 
imagefilledrectangle($tmp, 0, 0, $new_width, $new_height, imagecolorallocate($tmp, 255, 255, 255)); 

和開工,因爲我想....