getimagesize()
imagecreatefromjpeg()
imagejpeg()
我在上传后调整图像大小,所以已经通过这些函数拉取了图像,并且在使用imagecreatefromjpeg()
调整大小后,还会使用imagecopyresampled()
。
但是,Page Speed仍然告诉我这些图像可以优化。在php脚本中如何实现优化?在imagejpeg()
中降低质量也没有效果。
getimagesize()
imagecreatefromjpeg()
imagejpeg()
我在上传后调整图像大小,所以已经通过这些函数拉取了图像,并且在使用imagecreatefromjpeg()
调整大小后,还会使用imagecopyresampled()
。
但是,Page Speed仍然告诉我这些图像可以优化。在php脚本中如何实现优化?在imagejpeg()
中降低质量也没有效果。
在imagejpeg函数中,您可以指定图像的质量。如果您已经将其设置为适当的值,则几乎无法做其他事情。
页面速度可能会认为所有大小超过某个特定值的图像都需要“压缩”,因此请确保它们在高度/宽度方面尽可能小且压缩过。
您可以在pagespeed文档http://code.google.com/speed/page-speed/docs/payload.html#CompressImages中了解有关页面速度及其压缩建议的更多信息,其中描述了一些压缩适当的技术/工具。
我还读到了以下内容:
有几个可用的工具可以对JPEG和PNG文件进行进一步的无损压缩,而不影响图像质量。对于JPEG,我们推荐在Linux上运行带有--strip-all选项的jpegtran或jpegoptim。对于PNG,我们推荐OptiPNG或PNGOUT。
因此,如果您确实想坚持使用Google的建议,也许可以使用PHP的exec
来在上传文件时对文件运行这些工具之一。
要使用PHP进行压缩,您可以执行以下操作(听起来您已经在执行此操作):
其中$source_url
是图像地址,$destination_url
是保存位置,$quality
是一个介于1到100之间的数字,选择使用多少JPEG压缩。
function compressImage($source_url, $destination_url, $quality) {
$info = getimagesize($source_url);
if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($source_url);
elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($source_url);
elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($source_url);
//save file
imagejpeg($image, $destination_url, $quality);
//return destination file
return $destination_url;
}
修复的功能:
function compressImage($source_url, $destination_url, $quality) {
//$quality :: 0 - 100
if( $destination_url == NULL || $destination_url == "" ) $destination_url = $source_url;
$info = getimagesize($source_url);
if ($info['mime'] == 'image/jpeg' || $info['mime'] == 'image/jpg')
{
$image = imagecreatefromjpeg($source_url);
//save file
//ranges from 0 (worst quality, smaller file) to 100 (best quality, biggest file). The default is the default IJG quality value (about 75).
imagejpeg($image, $destination_url, $quality);
//Free up memory
imagedestroy($image);
}
elseif ($info['mime'] == 'image/png')
{
$image = imagecreatefrompng($source_url);
imageAlphaBlending($image, true);
imageSaveAlpha($image, true);
/* chang to png quality */
$png_quality = 9 - round(($quality / 100 ) * 9 );
imagePng($image, $destination_url, $png_quality);//Compression level: from 0 (no compression) to 9(full compression).
//Free up memory
imagedestroy($image);
}else
return FALSE;
return $destination_url;
}
<?php
function resizeImage($imagePath, $width, $height, $blur, $filterType = Imagick::FILTER_LANCZOS, $bestFit = false)
{
//The blur factor where > 1 is blurry, < 1 is sharp.
$img= new \Imagick(realpath($imagePath));
$img->setCompression(Imagick::COMPRESSION_JPEG);
$img->setCompressionQuality(40);
$img->stripImage();
$img->resizeImage($width, $height, $filterType, $blur, $bestFit);
$img->writeImage();
}
?>
想了解如何使用Imagick调整图像大小,可以参考以下链接:
http://php.net/manual/zh/class.imagick.php
http://php.net/manual/zh/imagick.resizeimage.php
http://php.net/manual/zh/imagick.constants.php#imagick.constants.filters
优化图像非常重要。许多CMS平台都有模块或插件来执行此过程。但是,如果您自己编程,则可以在此页面找到完整的php教程https://a1websitepro.com/optimize-images-with-php-in-a-directory-on-your-server/,您将学习如何实现imagecreatefromjpeg($SrcImage);
和imagecreatefrompng($SrcImage);
以及imagecreatefromgif($SrcImage);
。该页面上有书面和视频说明。