dongwen5870 2017-08-05 13:50
浏览 183
已采纳

PHP - 上传PNG图像透明

I would like to upload png images transparent, I tried many things via google but still didnt work, still background is black. Here is my code, it is written in symfony 2.7. It is uploading and creating two images, one is small, the second is big.

Image before sending

Image after sending

So here you can see difference between images before upload and after upload.

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())));
            }
  • 写回答

1条回答 默认 最新

  • dsfo22654 2017-08-07 12:56
    关注
                imagejpeg($tmp1, $filename1, 100);
                imagejpeg($tmp2, $filename2, 100);
    

    JPEG does not support transparency. You need imagepng and use PNG format instead.

    That being said, you seem to have code that loads in the image (using imagecreatefrom* as appropriate), why not do the same for saving (image*)?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?