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=''>";

上記のコードでサムネイルが表示されるのは素晴らしいのですが、画質がひどいです。画像の色が反転したように見え、押しつぶされたように見えます。これをやっていて一日中頭が痛い。アイデアはありますか?

4

3 に答える 3

18

imagecreateの代わりに imagecreatetruecolor を使用し、 imagecopyresizedの代わりに imagecopyresampled を使用します。

于 2008-11-18T12:07:27.793 に答える
7

Dominic が指摘するように、3 番目のパラメーターは含める価値があります。jpeg の品質を指定します。

「そして、押しつぶされたように見える」という問題については、それ自体が正方形である場合とそうでない場合があるソース画像から正方形のサムネイルを作成していることを思い出してください。

これを回避する 1 つの方法は、ソースの寸法を操作して、ソースからコピーする全幅または全高 (画像が縦長か横長かに応じて) の正方形を作成することです。これは、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);
}
于 2008-11-18T12:12:59.547 に答える
1

試す:

imagejpeg($targetImage, "$thumbPath/$thumbName", 100);
于 2008-11-18T12:03:32.920 に答える