创建 png 文件

2024-01-10

我做了一个函数来处理 jpg 和 png 文件,但是在尝试上传 png 文件时出现错误。

这是函数:

function createImg ($type, $src, $dst, $width, $height, $quality) {

$newImage = imagecreatetruecolor($width,$height);
if ($type == "jpg/jpeg") {  
    //imagecreatefromjpeg() returns an image identifier representing the image obtained from the given filename.
    $source = imagecreatefromjpeg($src);
}
else if ($type == "png") {
    //imagecreatefrompng() returns an image identifier representing the image obtained from the given filename.
    $source = imagecreatefrompng($src);
}
imagecopyresampled($newImage,$source,0,0,0,0,$width,$height,getWidth($src),getHeight($src));
if ($type == "jpg/jpeg") {
    //imagejpeg() creates a JPEG file from the given image. 
    imagejpeg($newImage,$dst,$quality); 
}
else if ($type == "png") {
    //imagepng() creates a PNG file from the given image. 
    imagepng($newImage,$dst,$quality);      
}
return $dst;

}

对于 jpg 可以正常工作,但是对于 png 我收到此错误消息:

警告: imagepng() [function.imagepng]: gd-png: fatal libpng 错误: zlib 无法初始化压缩器 -- E:...\php\functions.upload.php 第 48 行出现流错误

警告:imagepng() [function.imagepng]:gd-png 错误:setjmp 在第 48 行的 E:...\php\functions.upload.php 中返回错误条件

EDIT :

我刚刚更改删除了 imagepng();只使用 imagejpeg,它的工作原理是这样的,我只想保存 jpg 文件。谢谢!


问题是因为imagejpeg质量最高可达 100,而imagepng最大质量为 9。 尝试这个

 else if ($type == "png") {
//imagepng() creates a PNG file from the given image. 
$q=9/100;
$quality*=$q;
imagepng($newImage,$dst,$quality);      
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

创建 png 文件 的相关文章

随机推荐