[英]Png black background when upload and resize image
Can you help me with my code in php? 您可以用我的PHP代码帮助我吗?
I don't know how make my pictures transparent. 我不知道如何使我的图片透明。 They have black background after the uploading.
上传后背景为黑色。 I have the code here.
我在这里有代码。 (and some text for small post and content)
(以及一些小帖子和内容的文字)
Thhank you. 谢谢你
<?php
function zmensi_obrazok($image_max_width, $image_max_height, $obrazok, $obrazok_tmp, $obrazok_size, $filename){
$postvars = array(
"image" => $obrazok,
"image_tmp" => $obrazok_tmp,
"image_size" => $obrazok_size,
"image_max_width" => $image_max_width,
"image_max_height" => $image_max_height
);
$valid_exts = array("jpg","jpeg","png");
$ext = end(explode(".",strtolower($obrazok)));
if($postvars["image_size"] <= 1024000){
if(in_array($ext,$valid_exts)){
if($ext == "jpg" || $ext == "jpeg"){
$image = imagecreatefromjpeg($postvars["image_tmp"]);
}
else if($ext == "png"){
$image = imagecreatefrompng($postvars["image_tmp"]);
}
list($width,$height) = getimagesize($postvars["image_tmp"]);
$old_width = imagesx($image);
$old_height = imagesy($image);
$scale = min($postvars["image_max_width"]/$old_width, $postvars["image_max_height"]/$old_height);
$new_width = ceil($scale*$old_width);
$new_height = ceil($scale*$old_height);
$tmp = imagecreatetruecolor($new_width,$new_height);
imagecopyresampled($tmp,$image,0,0,0,0,$new_width,$new_height,$width,$height);
imagejpeg($tmp,$filename,100);
return "";
imagedestroy($image);
imagedestroy($tmp);
}
}
}
?>
I think this link will answer your question: http://www.php.net/manual/pl/function.imagecopyresampled.php#104028 我认为此链接将回答您的问题: http : //www.php.net/manual/pl/function.imagecopyresampled.php#104028
In your code the answer will be something like: 在您的代码中,答案将类似于:
// preserve transparency
if($ext == "gif" or $ext == "png"){
imagecolortransparent($tmp, imagecolorallocatealpha($tmp, 0, 0, 0, 127));
imagealphablending($tmp, false);
imagesavealpha($tmp, true);
}
Paste this before executing imagecopyresampled
. 在执行
imagecopyresampled
之前粘贴此内容。
If you did want to save to a JPEG rather than saving to a PNG, you can just change the background colour of the target image to white before you do the copy: 如果您确实想保存为JPEG而不是保存为PNG,则可以在复制之前将目标图像的背景色更改为白色:
$tmp = imagecreatetruecolor($new_width,$new_height);
imagefilledrectangle($tmp, 0, 0, $new_width, $new_height, imagecolorallocate($tmp, 255, 255, 255));
imagecopyresampled($tmp,$image,0,0,0,0,$new_width,$new_height,$width,$height);
Then you'll end up with a JPEG without any transparency, but the background colour will be white rather than black. 然后,您将得到没有任何透明度的JPEG,但是背景颜色将是白色而不是黑色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.