2

以下のように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 ポイントを獲得するために。

4

1 に答える 1

1

圧縮は、品質を損なう場合にのみ可能です。音楽や映像も同様です。

imagejpeg($image, $destination_url, $quality);
于 2018-06-13T07:16:48.723 に答える