2008-11-18 71 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); 
}