Php 像PageSpeed一样优化/无损压缩图像
我将JPEG图像压缩如下:Php 像PageSpeed一样优化/无损压缩图像,php,Php,我将JPEG图像压缩如下: function convert_img($img_source) { $img_destination = $img_source; $max_width = 150; $max_height = 150; $src = imagecreatefromjpeg($img_source); list($width,$height) = getimagesize($img_source); $x_ratio = $max_
function convert_img($img_source) {
$img_destination = $img_source;
$max_width = 150;
$max_height = 150;
$src = imagecreatefromjpeg($img_source);
list($width,$height) = getimagesize($img_source);
$x_ratio = $max_width/$width;
$y_ratio = $max_height/$height;
if ($width <= $max_width && $height <= $max_height) {
$tn_width = $width;
$tn_height = $height;
} elseif ($x_ratio * $height < $max_height) {
$tn_height = ceil($x_ratio * $height);
$tn_width = $max_width;
} else {
$tn_width = ceil($y_ratio * $width);
$tn_height = $max_height;
}
$tmp = imagecreatetruecolor($tn_width,$tn_height);
imagecopyresampled($tmp,$src,0,0,0,0,$tn_width,$tn_height,$width,$height);
imagejpeg($tmp,$img_destination,80);
imagedestroy($src);
imagedestroy($tmp);
}
function convert\u img($img\u source){
$img_destination=$img_source;
$max_width=150;
$max_height=150;
$src=imagecreatefromjpeg($img_source);
列表($width,$height)=getimagesize($img_source);
$x_比率=$max_width/$width;
$y_比率=$max_高度/$height;
如果($width压缩只会损害质量,音乐和视频也是如此
imagejpeg($image, $destination_url, $quality);
您正在尝试优化在线验证程序吗?:-)我试图在PageSpeed中获得100分:)我认为356字节在现实世界中并不重要:-)我知道,但我想看看是否有办法实现它:)我将有更大的图像,并且每个图像将节省10kb。如果你想压缩JPEG,请准备接受一些质量损失。你可以节省几个字节通过剥离元数据,而不是每幅图像10k。