2017-08-05 136 views
1

我想上传PNG图像透明,我尝试了很多东西通过谷歌,但仍然没有工作,仍然背景是黑色的。这是我的代码,它是用symfony 2.7编写的。它正在上传和创建两个图像,一个很小,另一个很大。PHP - 上传PNG图像透明

Image before sending

Image after sending

所以在这里你可以看到上传前上传后的图像之间的差异。

if ($upload_type == 'image/png' || $upload_type == 'image/jpg' || $upload_type == 'image/jpeg' || $upload_type == 'image/gif') { 
       $filename = stripslashes($upload_name); 
       $extension = $this->getExtension($filename); 
       $extension = strtolower($extension); 

       if ($extension == "jpg" || $extension == "jpeg") { 
        $upload_temp = $_FILES['form-chat-send-image']['tmp_name']; 
        $src = imagecreatefromjpeg($upload_temp); 
       } else if ($extension == "png") { 
        $upload_temp = $_FILES['form-chat-send-image']['tmp_name']; 
        $src = imagecreatefrompng($upload_temp); 
       } else { 
        $src = imagecreatefromgif($upload_temp); 
       } 

       list($width, $height) = getimagesize($upload_temp); 
       $newWidth1 = $width; 
       $newHeight1 = $height; 
       if($width>1200 || $height>1200) { 
        if ($height < $width) { 
         $newWidth1 = 1200; 
         $newHeight1 = ($height/$width) * $newWidth1; 
        } else { 
         $newHeight1 = 1200; 
         $newWidth1 = ($width/$height) * $newHeight1; 
        } 
       } 
       $tmp1 = imagecreatetruecolor($newWidth1, $newHeight1); 
       $newWidth2 = $width; 
       $newHeight2 = $height; 
       if($width>220 || $height>220) { 
        if ($height < $width) { 
         $newWidth2 = 220; 
         $newHeight2 = ($height/$width) * $newWidth2; 
        } else { 
         $newHeight2 = 220; 
         $newWidth2 = ($width/$height) * $newHeight2; 
        } 
       } 
       $tmp2 = imagecreatetruecolor($newWidth2, $newHeight2); 
       imagecolortransparent($tmp2, imagecolorallocate($tmp2, 0, 0, 0)); 
       imagealphablending($tmp2, false); 
       imagesavealpha($tmp2, true); 

       imagecopyresampled($tmp1, $src, 0, 0, 0, 0, $newWidth1, $newHeight1, 
        $width, $height); 

       imagecopyresampled($tmp2, $src, 0, 0, 0, 0, $newWidth2, $newHeight2, 
        $width, $height); 

       $dateName = new \DateTime(); 
       $newName = $conversation->getId().'-'.$dateName->format('m_d_Y_H_i_s'); 

       $filename1 = $uploadTo . $newName . "." . $extension; 
       $filename2 = $uploadTo . "small/" . $newName . "." . $extension; 

       imagejpeg($tmp1, $filename1, 100); 
       imagejpeg($tmp2, $filename2, 100); 

       imagedestroy($src); 
       imagedestroy($tmp1); 
       imagedestroy($tmp2); 
       $uploadDB = $newName . "." . $extension; 

       $msgImg = new Message(); 
       $msgImg->setText($uploadDB); 
       $msgImg->setConversation($conversation); 
       $msgImg->setForm('img'); 
       if($type==2){ 
        $msgImg->setType(0); 
       } 
       else if($type==1){ 
        $msgImg->setType(1); 
       } 
       $em->persist($msgImg); 
       $em->flush(); 

       $_FILES = array(); 
       return $this->redirect($this->generateUrl('conversation',array('id'=>$conversation->getId()))); 
      } 
+0

(您是否尝试在本地保存和查看PNG?结果?) – greybeard

+0

本地?如果你的意思是本地主机,那么它是相同的服务器上 – Kchaan

+0

*保存/使用本地*在*没有上载* *你似乎不信任。 – greybeard

回答

0
  imagejpeg($tmp1, $filename1, 100); 
      imagejpeg($tmp2, $filename2, 100); 

JPEG不支持透明度。您需要imagepng并改用PNG格式。

这就是说,你似乎有加载在图像中的代码(使用imagecreatefrom*在适当的情况下),为什么不同样保存(image*)?

+0

非常感谢你,我没有意识到,我将所有图像保存为jpeg格式。 :) – Kchaan