0

私はこのコードを作成しました。要求された画像が見つからない場合は、その場で画像のサイズを変更して保存します。

これに関する唯一の問題は、出力されたjpg画像の品質が低いことです。

画質を上げるために何か変更が必要なのかと思っていました。

if (isset($_GET['size'])) {
    $size = $_GET['size'];
}

if (isset($_GET['name'])) {
    $filename = $_GET['name'];
}

$filePath = "files/catalog/" . urldecode($filename);

// Content type

header('Content-Type: image/jpeg');

// Get new sizes
list($width, $height) = getimagesize($filePath);

if ($_GET["name"] && !$size) {
    $newwidth  = $width * $percent;
    $newheight = $height * $percent;

} else if ($_GET["name"] && $size) {
    switch ($size) {
        case "thumbs":
            $newwidth  = 192;
            $newheight = 248;
            break;
        case "large":
            $newwidth  = 425;
            $newheight = 550;
            break;
    }
}

$resizedFileName = $filename; 
$resizedFileName = str_replace(".jpg", "", $resizedFileName) . ".jpg";
$resizedFilePath = "files/catalog/" . urldecode($resizedFileName);


if (!file_exists($resizedFilePath)) {
    // Load
    $thumb  = imagecreatetruecolor($newwidth, $newheight);
    $source = imagecreatefromjpeg($filePath);

    // Resize
    imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

    // Output
    imagejpeg($thumb, $resizedFilePath);
    //file_put_contents($, $binarydata);

    $imageContent = file_get_contents($resizedFilePath);
    echo $imageContent;

} else {
    $imageContent = file_get_contents($resizedFilePath);
    echo $imageContent;
}
4

3 に答える 3

1

imagecopyresized()の代わりにimagecopyresampled()を使用する必要があり imagejpeg ()関数には3番目のオプションのパラメーターがあり、品質を指定できます。

于 2012-06-12T15:59:28.990 に答える
0

おそらくimagejpeg()間違って使用しているため、関数の2番目の変数は、パーセンテージで表した品質を表しています。

于 2012-06-12T16:00:15.060 に答える
0

あなたが欲しいimagecopyresampled()。サイズ変更は、不要なピクセルを破棄することで機能します。resampled()は平均化され、よりスムーズな結果を生成します。

于 2012-06-12T16:00:16.333 に答える