我正在上传文件图像。我将其压缩到指定的宽度和高度,但是当我尝试显示PNG时,它总是显示空白图像,但它将显示jpg或jpeg文件。
function compress($extention, $destination, $quality, $tmpsource)
{
$extention = strtolower($extention);
list($width, $height) = getimagesize($destination);
$y = (800 * $height / $width);
$getType = null;
if ($extention == "jpeg") {
$source = imagecreatefromjpeg($destination);
}
if ($extention == "jpg") {
$source = imagecreatefromjpeg($destination);
}
if ($extention == "png") {
$source = imagecreatefrompng($destination);
}
$thumb = imagecreatetruecolor(800, $y);
imagecopyresized($thumb, $source, 0, 0, 0, 0, 800, $y, $width, $height);
if ($extention == "jpeg") {
imagejpeg($thumb, $destination, $quality);
}
if ($extention == "jpg") {
imagejpeg($thumb, $destination, $quality);
}
if ($extention == "png") {
imagepng($thumb, $destination, $quality);
}
return $destination;
}
这是我称之为的地方:
if (in_array($imgTypeArr[$i], $allowedTypes))
{
$newImgName = uniqid($newUniqName . "_") . "." . $getExt;
$imgUploadArr[] = $newImgName;
$destination = "../images/products/" . $proFolder . "/" . $newImgName;
move_uploaded_file($imgTmpArr[$i], $destination);
compress($getExt, $destination, 100);
}
imagepng
期望quality
值介于 -1 和 9 之间,但代码传递的值为 100:
compress($getExt, $destination, 100);
质量
压缩级别:从 0(无压缩(到 9。默认值 (-1( 使用 zlib 压缩默认值。有关更多信息,请参阅 » zlib 手册。
您可以省略该值,对 PNG 使用默认的 zlib 压缩,对 JPG 使用默认的 IJG 压缩(大约 75(:
compress($getExt, $destination);
或者,您可能希望为 JPG 和 PNG 指定不同的质量值。