首页
学习
活动
专区
圈层
工具
发布

压缩PNG
EN

Stack Overflow用户
提问于 2015-09-29 05:54:44
回答 2查看 562关注 0票数 0

我有这段上传图片的代码。它使带有PNG扩展名的缩略图具有9级压缩,但图像看起来并不好。我只想要-50%或更多的PNG的透明度压缩。

代码语言:javascript
复制
$path_thumbs = "../pictures/thumbs/";
$path_big = "../pictures/";
$img_thumb_width = 140; // 
$extlimit = "yes"; 
$limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");       
$file_type = $_FILES['image']['type'];
$file_name = $_FILES['image']['name'];
$file_size = $_FILES['image']['size'];
$file_tmp = $_FILES['image']['tmp_name'];

if(!is_uploaded_file($file_tmp)){
    echo "choose file for upload!. <br>--<a href=\"$_SERVER[PHP_SELF]\">return</a>";
    exit(); 
}

$ext = strrchr($file_name,'.');
$ext = strtolower($ext);
if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
    echo "dissallowed!  <br>--<a href=\"$_SERVER[PHP_SELF]\">return</a>";
    exit();
}

$getExt = explode ('.', $file_name);
$file_ext = $getExt[count($getExt)-1];
$rand_name = md5(time());
$rand_name= rand(0,10000);
$ThumbWidth = $img_thumb_width;

if($file_size){
    if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
        $new_img = imagecreatefromjpeg($file_tmp);
    }elseif($file_type == "image/x-png" || $file_type == "image/png"){
        $new_img = imagecreatefrompng($file_tmp);
    }elseif($file_type == "image/gif"){
        $new_img = imagecreatefromgif($file_tmp);
    }

    list($width, $height) = getimagesize($file_tmp);
    $imgratio = $width/$height;

    if ($imgratio>1){
        $newwidth = $ThumbWidth;
        $newheight = $ThumbWidth/$imgratio;
    }else{
        $newheight = $ThumbWidth;
        $newwidth = $ThumbWidth*$imgratio;
    }
    if (@function_exists(imagecreatetruecolor)){
        $resized_img = imagecreatetruecolor($newwidth, $newheight);
    }else{
        die("Error: Please make sure you have GD library ver 2+");
    }
    imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    ImagePng ($resized_img, "$path_thumbs/$rand_name.$file_ext,9");
    ImageDestroy ($resized_img);
    ImageDestroy ($new_img);
}

move_uploaded_file ($file_tmp, "$path_big/$rand_name.$file_ext");
EN

回答 2

Stack Overflow用户

发布于 2015-09-29 06:50:37

为了获得更好的图像质量,你应该使用imagecopyresizedimagecopyresampled instread。

对于图像透明度,你应该看看imagesavealpha

要使其工作,您需要启用它,在您调整图像大小之前,您还需要禁用alpha混合。最好是把它放在imagecreatetruecolor之后。

代码语言:javascript
复制
$resized_img = imagecreatetruecolor($newwidth, $newheight);
imagealphablending($resized_img, false);
imagesavealpha($resized_img, true);

至于大小,你的代码中有一个拼写错误

代码语言:javascript
复制
ImagePng ($resized_img,"$path_thumbs/$rand_name.$file_ext,9");

应该是

代码语言:javascript
复制
ImagePng ($resized_img, "$path_thumbs/$rand_name.$file_ext", 9);

您将压缩级别参数放入文件名中,而不是函数中。

这里的压缩级别并不意味着它会让你的文件变得更小。这是速度和文件大小之间的折衷。

你可以无损压缩一个文件的程度是有限制的。如果文件大小是个问题,你应该像JPEG一样用有损压缩压缩它。

票数 1
EN

Stack Overflow用户

发布于 2015-09-29 06:17:58

我认为(我没有验证)你应该使用函数imagecopyresampled而不是imagecopyresize。我认为imagecopyresampled的质量更高。您可能还希望从使用imagecreatetruecolor开始

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32832429

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档