我做了一个函数来处理 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 文件。谢谢!