2017-02-17 178 views
0

我正在使用下面的代碼來創建圖像縮略圖。它適用於JPG & JPEG圖像,但不適用於PNG圖像縮略圖。使用PHP png圖像透明背景

$id = $_POST['mid']; 
    $previousImage = $_POST["oldImage"]; 

    $imgSize = $_FILES['profile_image']['size']; 
    //call thumbnail creation function and store thumbnail name 
    $upload_img = cwUpload('profile_image',$mat_upload_dir,'',TRUE,$mat_upload_dir_thumb,'200','200'); 

    //full path of the thumbnail image 
    $thumb_src = $mat_upload_dir_thumb.$upload_img; 

    $query="update tbl_matrimony set image='".$upload_img."' where id='".$id."'"; 
    $res = mysql_query($query) or die(mysql_error()); 
    if ($res) 
    { 
     echo "Image Uploaded"; 
    } 
    else 
    { 
     echo "Error occurred"; 
    } 
} 

function cwUpload($field_name = '', $target_folder = '', $file_name = '', $thumb = FALSE, $thumb_folder = '', $thumb_width = '', $thumb_height = ''){ 
    //folder path setup 
    $target_path = $target_folder; 
    $thumb_path = $thumb_folder; 

    //file name setup 
    $filename_err = explode(".",$_FILES[$field_name]['name']); 
    $filename_err_count = count($filename_err); 
    $file_ext = $filename_err[$filename_err_count-1]; 
    if($file_name != '') 
    { 
     $fileName = $file_name.'.'.$file_ext; 
    } 
    else 
    { 
     $fileName = $_FILES[$field_name]['name']; 
    } 

    //upload image path 
    $upload_image = $target_path.basename($fileName); 

    //upload image 

    if(move_uploaded_file($_FILES[$field_name]['tmp_name'],$upload_image)) 
    { 
     unlink($mat_upload_dir.$previousImage); 
     //thumbnail creation 
     if($thumb == TRUE) 
     { 
      $thumbnail = $thumb_path.$fileName; 
      list($width,$height) = getimagesize($upload_image); 
      $thumb_create = imagecreatetruecolor($thumb_width,$thumb_height); 
      switch($file_ext){ 
       case 'jpg': 
        $source = imagecreatefromjpeg($upload_image); 
        break; 
       case 'jpeg': 
        $source = imagecreatefromjpeg($upload_image); 
        break; 
       case 'png': 
        $source = imagecreatefrompng($upload_image); 
        break; 
       case 'gif': 
        $source = imagecreatefromgif($upload_image); 
        break; 
       default: 
        $source = imagecreatefromjpeg($upload_image); 
      } 
      imagecopyresized($thumb_create,$source,0,0,0,0,$thumb_width,$thumb_height,$width,$height); 
      switch($file_ext){ 
       case 'jpg' || 'jpeg': 
        imagejpeg($thumb_create,$thumbnail,100); 
        break; 
       case 'png': 
        imagepng($thumb_create,$thumbnail,100); 
        break; 
       case 'gif': 
        imagegif($thumb_create,$thumbnail,100); 
        break; 
       default: 
        imagejpeg($thumb_create,$thumbnail,100); 
      } 
     } 

     return $fileName; 
    } 
    else 
    { 
     return false; 
    } 
} 
?> 
+0

什麼是錯誤您收到如果有的話? 'png'的預期行爲是什麼,你得到了什麼? –

+0

我正在獲取png圖像的黑色背景。 –

+0

也許'imagecreatefrompng'工作不正常。看看[它的文檔](https://secure.php.net/manual/en/function.imagecreatefrompng.php)以及它下面的註釋。 –

回答

0

嘗試爲大寫字母PNG添加一個大小寫。像這樣:

case 'PNG': 
      $source = imagecreatefrompng($upload_image); 
      break; 

case 'png' || 'PNG': 
      imagepng($thumb_create,$thumbnail,100); 
      break; 
+0

我用這個,但它會創建一個黑backgoround。 –

+0

請參閱此問題:http://stackoverflow.com/questions/2611852/imagecreatefrompng-makes-a-black-background-instead-of-transparent – Aj334