2008-11-18 72 views
0
$sourcePath = 'images/'; // Path of original image 
$sourceUrl = ''; 
$sourceName = 'photo1.jpg'; // Name of original image 
$thumbPath = 'thumbs/'; // Writeable thumb path 
$thumbUrl = 'thumbs/'; 
$thumbName = "test_thumb.jpg"; // Tip: Name dynamically 
$thumbWidth = 100; // Intended dimension of thumb 

// Beyond this point is simply code. 
$sourceImage = imagecreatefromjpeg("$sourcePath/$sourceName"); 
$sourceWidth = imagesx($sourceImage); 
$sourceHeight = imagesy($sourceImage); 

$targetImage = imagecreate($thumbWidth,$thumbWidth); 
imagecopyresized($targetImage,$sourceImage,0,0,0,0,$thumbWidth,$thumbWidth,imagesx($sourceImage),imagesy($sourceImage)); 
imagejpeg($targetImage, "$thumbPath/$thumbName"); 

// By now, the thumbnail is copied into the $thumbpath 
// as the file name specified in $thumbName, so display 
echo "<img src='$thumbUrl$thumbName' alt=''>"; 

上面的代碼給了我一個縮略圖,這很好,但圖像質量很糟糕。它看起來像圖像有倒置的顏色,看起來它已被壓扁。我整天都在頭痛。有人有主意嗎?使用PHP生成縮略圖會導致圖像質量不佳

回答

18

使用imagecreatetruecolor代替imagecreate和imagecopyresampled而不是imagecopyresized。

+0

此挑選出質量,三江源非常感謝!任何關於壓扁的想法? – Drew 2008-11-18 12:11:10

+0

壓扁 - 是的。看到我的答案。 – philistyne 2008-11-18 12:55:13

1

嘗試:

imagejpeg($targetImage, "$thumbPath/$thumbName", 100); 
7

第三個參數值得包括多米尼克指出。它指定了JPEG質量。

關於「它看起來已被壓扁」的問題,請記住,您正在從源圖像製作方形縮略圖,該圖像本身可能是方形的,也可能不是方形。

解決此問題的一種方法是使用源維度來計算全角或全高(取決於圖像是縱向還是橫向)平方以從源複製。這意味着用動態計算的內容替換imagecopyresized()的參數中的「0,0,0,0」。

(編輯:例如)

function makeSquareThumb($srcImage, $destSize, $destImage = null) { 
    //I'm sure there's a better way than this, but it works... 
    //I don't like my folder and file checking in the middle, but need to illustrate the need for this. 

    $srcFolder = dirname($srcImage); //source folder 
    $srcName = basename($srcImage); //original image filename 

    //the IF ELSEIF ELSE below is NOT comprehensive - eg: what if the dest folder is the same as the source? 
    //writeable nature of the destination is not checked! 
    if(!destImage) { 
     $destFolder = $srcFolder.'/thumbs/'; 
     if(!is_dir($destFolder)) { 
      //make the thumbs folder if there isn't one! 
      mkdir($destFolder); 
     } 
     $destImage = $destFolder.$srcName; 
    } elseif (is_dir($destImage)) { 
     $destFolder = $destImage; 
     $destImage = $destFolder.'/'.$srcName; 
    } else { 
     $destFolder = dirname($destImage); 
    } 


    //Now make it! 
    $srcCanvas = imagecreatefromjpeg($srcImage); 
    $srcWidth = imagesx($srcCanvas); 
    $srcHeight = imagesy($srcCanvas); 

    //this let's us easily sample a square from the middle, regardless of apsect ratio. 
    $shortSide = array($srcWidth,$srcHeight); 
    sort($shortSide); 

    $src_x = $srcWidth/2 - $shortSide[0]/2; 
    $src_y = $srcHeight/2 - $shortSide[0]/2; 

    //do it! 
    $destCanvas = imagecreatetruecolor($destSize, $destSize); 
    imagecopyresampled($destCanvas,$srcCanvas,0,0,$src_x,$src_y,$destSize,$destSize,$shortSide[0],$shortSide[0]); 
    imagejpeg($destCanvas, $destImage); 
}