以下のように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_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);
}
問題は、PageSpeed が提案するサイズよりも少し大きいサイズの画像を常に取得することです。
たとえば、サイズが 8.85KB の画像の場合、PageSpeed は、このサイズを 356B 削減できることを示唆しています。
画像を圧縮し、可能な限り最小のサイズにするにはどうすればよいですか? PageSpeed が何も示唆しないようにし、100 ポイントを獲得するために。