PHP-上传PNG图像透明
我想上传透明的png图片,我通过谷歌尝试了很多东西,但还是没用,背景还是黑色的。这是我的代码,它是用symfony 2.7编写的。它正在上传和创建两个图像,一个是小的,第二个是大的 所以在这里你们可以看到上传前和上传后图像的区别PHP-上传PNG图像透明,php,upload,png,transparency,transparent,Php,Upload,Png,Transparency,Transparent,我想上传透明的png图片,我通过谷歌尝试了很多东西,但还是没用,背景还是黑色的。这是我的代码,它是用symfony 2.7编写的。它正在上传和创建两个图像,一个是小的,第二个是大的 所以在这里你们可以看到上传前和上传后图像的区别 if ($upload_type == 'image/png' || $upload_type == 'image/jpg' || $upload_type == 'image/jpeg' || $upload_type == 'image/gif') {
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())));
}
if($upload|U type=='image/png'| |$upload|U type=='image/jpg'| |$upload|U type=='image/jpeg'| |$upload|U type=='image/gif'){
$filename=stripslashes($upload\u name);
$extension=$this->getExtension($filename);
$extension=strtolower($extension);
如果($extension==“jpg”| |$extension==“jpeg”){
$upload_temp=$_文件['form-chat-send-image']['tmp_name'];
$src=imagecreatefromjpeg($upload\u temp);
}else if($extension==“png”){
$upload_temp=$_文件['form-chat-send-image']['tmp_name'];
$src=imagecreatefrompng($upload\u temp);
}否则{
$src=imagecreatefromformgif($upload\u temp);
}
列表($width,$height)=getimagesize($upload\u temp);
$newWidth1=$width;
$newHeight1=$height;
如果($width>1200 | |$height>1200){
如果($height<$width){
$newWidth1=1200;
$newHeight1=($height/$width)*$newWidth1;
}否则{
$newHeight1=1200;
$newWidth1=($width/$height)*$newHeight1;
}
}
$tmp1=imagecreatetruecolor($newWidth1,$newHeight1);
$newWidth2=$width;
$newHeight2=$height;
如果($width>220 | |$height>220){
如果($height<$width){
$newWidth2=220;
$newHeight2=($height/$width)*$newWidth2;
}否则{
$newHeight2=220;
$newWidth2=($width/$height)*$newHeight2;
}
}
$tmp2=imagecreatetruecolor($newWidth2,$newHeight2);
imagecolortransparent($tmp2,imagecolorallocate($tmp2,0,0));
imagealphablending($tmp2,false);
imagesavealpha($tmp2,true);
imagecopyresampled($tmp1、$src、0、0、0、$newWidth1、$newHeight1、,
$width,$height);
imagecopyresampled($tmp2、$src、0、0、0、$newWidth2、$newHeight2、,
$width,$height);
$dateName=new\DateTime();
$newName=$conversation->getId().-'.$dateName->format('mu d_Y_H_i_s');
$filename1=$uploadTo.$newName.“.”$extension;
$filename2=$uploadTo.“small/”$newName.“.”$extension;
imagejpeg($tmp1,$filename100);
imagejpeg($tmp2,$filename2100);
(1)(src);
图像处理(tmp1美元);
图像处理(tmp2美元);
$uploadDB=$newName.“.”$extension;
$msgImg=新消息();
$msgImg->setText($uploadDB);
$msgImg->setConversation($conversation);
$msgImg->setForm('img');
如果($type==2){
$msgImg->setType(0);
}
else if($type==1){
$msgImg->setType(1);
}
$em->persist($msgImg);
$em->flush();
$\u FILES=array();
返回$this->redirect($this->generateUrl('conversation',array('id'=>$conversation->getId());
}
JPEG不支持透明度。您需要imagepng
并改用PNG格式
也就是说,您似乎已经在图像中加载了代码(根据需要使用imagecreatefrom*
),为什么不在保存时执行相同的操作(image*
)
JPEG不支持透明度。您需要imagepng
并改用PNG格式
也就是说,您似乎有加载到图像中的代码(根据需要使用
imagecreatefrom*
),为什么不在本地保存(image*
)?(您是否尝试在本地保存和查看PNG?结果?)?如果您指的是localhost,那么它与服务器上的相同本地保存/使用与中的相同,没有您似乎不信任的上载。本地它没有黑色背景,是透明的,只有在上载时才是黑色。请在本地发布两幅图像的副本。(您是否尝试保存并查看PNG?结果?)?如果您指的是localhost,那么它与服务器上的相同在本地保存/使用与中一样,没有您似乎不信任的上载。在本地,它没有黑色背景,它是透明的,只有在上载时才使用黑色。请发布您的两幅图像的副本。非常感谢,我没有意识到我正在将所有图像保存为jpeg格式。:)非常感谢,我没有意识到我正在将所有图像保存为jpeg格式。:)
imagejpeg($tmp1, $filename1, 100);
imagejpeg($tmp2, $filename2, 100);