2013-08-23 159 views
1

我完全立足於這一個...我需要一個照片上傳腳本,保持PNG文件的透明度。目前透明區域是黑色的。任何幫助是極大的讚賞。這裏是我的代碼:PHP PNG照片上傳,保持透明

if(isset($_POST)) { 
    require_once('siteInfo.php'); 

    session_start(); 

    $ThumbSquareSize  = 90; 
    $BigImageMaxSize  = $_POST['size']; 
    $ThumbPrefix   = "thumb_"; 
    $DestinationDirectory = '../Uploads/'; 
    $Quality    = 100; 

    if (!isset($_FILES['ImageFile']) || !is_uploaded_file($_FILES['ImageFile']['tmp_name'])) { 
     die('Please select a file.'); 
    } 

    $RandomNumber = rand(0, 9999999999); 

    $ImageName  = str_replace(' ','-',strtolower($_FILES['ImageFile']['name'])); 
    $ImageSize  = $_FILES['ImageFile']['size']; 
    $TempSrc  = $_FILES['ImageFile']['tmp_name']; 
    $ImageType  = $_FILES['ImageFile']['type']; 

    switch (strtolower($ImageType)) { 
     case 'image/png': 
      $CreatedImage = imagecreatefrompng($_FILES['ImageFile']['tmp_name']); 
      break; 
     case 'image/gif': 
      $CreatedImage = imagecreatefromgif($_FILES['ImageFile']['tmp_name']); 
      break;   
     case 'image/jpeg': 
     case 'image/pjpeg': 
      $CreatedImage = imagecreatefromjpeg($_FILES['ImageFile']['tmp_name']); 
      break; 
     default: 
      // die('Unsupported file format. Please upload a PNG, GIF, or JPG file.'); //output error and exit 
    } 

    list($CurWidth,$CurHeight)=getimagesize($TempSrc); 

    $ImageExt = substr($ImageName, strrpos($ImageName, '.')); 
    $ImageExt = str_replace('.','',$ImageExt); 

    $ImageName  = preg_replace("/\\.[^.\\s]{3,4}$/", "", $ImageName); 

    $NewImageName = $ImageName.'-'.$RandomNumber.'.'.$ImageExt; 

    $thumb_DestRandImageName = $DestinationDirectory.$ThumbPrefix.$NewImageName; //Thumb name 
    $DestRandImageName   = $DestinationDirectory.$NewImageName; //Name for Big Image 

    if (resizeImage($CurWidth,$CurHeight,$BigImageMaxSize,$DestRandImageName,$CreatedImage,$Quality,$ImageType)) { 
     if(!cropImage($CurWidth,$CurHeight,$ThumbSquareSize,$thumb_DestRandImageName,$CreatedImage,$Quality,$ImageType)) { 
      echo 'Error Creating thumbnail'; 
     } 

     $query = "Update Content set ".$_POST['field']."='".$NewImageName."' WHERE ID=".$_POST['ID']; 

     mysqli_query($dbc, $query); 
     mysqli_close($dbc); 

     echo 'Uploaded file: <span>'.$NewImageName.'</span>.'; 
    } else { 
     die('Resize Error'); 
    } 
} 
function resizeImage($CurWidth,$CurHeight,$MaxSize,$DestFolder,$SrcImage,$Quality,$ImageType) { 
    if ($CurWidth <= 0 || $CurHeight <= 0) { 
     return false; 
    } 

    $ImageScale   = min($MaxSize/$CurWidth, $MaxSize/$CurHeight); 
    $NewWidth   = ceil($ImageScale*$CurWidth); 
    $NewHeight   = ceil($ImageScale*$CurHeight); 
    $NewCanves   = imagecreatetruecolor($NewWidth, $NewHeight); 

    if (imagecopyresampled($NewCanves, $SrcImage,0, 0, 0, 0, $NewWidth, $NewHeight, $CurWidth, $CurHeight)) { 
     switch(strtolower($ImageType)) { 
      case 'image/png': 
       imagepng($NewCanves,$DestFolder); 
       break; 
      case 'image/gif': 
       imagegif($NewCanves,$DestFolder); 
       break;   
      case 'image/jpeg': 
      case 'image/pjpeg': 
       imagejpeg($NewCanves,$DestFolder,$Quality); 
       break; 
      default: 
       return false; 
     } 

     if (is_resource($NewCanves)) { imagedestroy($NewCanves); } 
     return true; 
    } 
} 
function cropImage($CurWidth,$CurHeight,$iSize,$DestFolder,$SrcImage,$Quality,$ImageType){  
    if ($CurWidth <= 0 || $CurHeight <= 0) { 
     return false; 
    } 

    if ($CurWidth>$CurHeight) { 
     $y_offset = 0; 
     $x_offset = ($CurWidth - $CurHeight)/2; 
     $square_size = $CurWidth - ($x_offset * 2); 
    } else { 
     $x_offset = 0; 
     $y_offset = ($CurHeight - $CurWidth)/2; 
     $square_size = $CurHeight - ($y_offset * 2); 
    } 

    $NewCanves = imagecreatetruecolor($iSize, $iSize); 

    if (imagecopyresampled($NewCanves, $SrcImage,0, 0, $x_offset, $y_offset, $iSize, $iSize, $square_size, $square_size)) { 
     switch(strtolower($ImageType)) { 
      case 'image/png': 
       imagepng($NewCanves,$DestFolder); 
       break; 
      case 'image/gif': 
       imagegif($NewCanves,$DestFolder); 
       break;   
      case 'image/jpeg': 
      case 'image/pjpeg': 
       imagejpeg($NewCanves,$DestFolder,$Quality); 
       break; 
      default: 
       return false; 
     } 

     if (is_resource($NewCanves)) { imagedestroy($NewCanves); } 

     return true; 
    } 
} 
+0

退房http://php.net/manual/en/function.imagealphablending.php然後看看http://stackoverflow.com/questions/6109832/php-gd-create-a-transparent-png-image/6110100#6110100 –

+0

我加了「imagealphablending($ NewCanves,true);」之後「if(imagecopyresampled($ NewCanves,$ SrcImage,0,0,0,0,$ NewWidth,$ NewHeight,$ CurWidth,$ CurHeight)){」但它沒有工作。這是正確的地方嗎? –

+0

沒關係。我想到了。 –

回答

3

我增加了以下的 「resizeImage」 功能:

imagealphablending($NewCanves, false); 
imagesavealpha($NewCanves, true); 
imagealphablending($SrcImage, true); 

這解決了這一問題。

+0

謝謝,你救了我很多時間 – allpnay