2012-06-12 92 views
0

我製作了這段代碼,如果它找不到請求的圖像,然後將其存儲,它會重新調整圖像的大小。PHP-GD圖像重新大小丟失圖像的質量

唯一的問題是輸出的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; 
} 

回答

0

可能因爲您正在使用imagejpeg()錯誤,函數中的第二個變量代表質量百分比!

0

你想要imagecopyresampled()。通過丟棄不必要的像素來調整工作大小。 resampled()將平均數據併產生更平滑的結果。