2013-04-12 36 views
0

我已經得到了下面的一段代碼:GD調整圖片大小不工作PHP

$biggest = ($width > $height) ? $width : $height; 
    $newWidth = 0; 
    $newHeight = 0; 

    if($biggest > $divSize){ 
     echo "BIGGEST<br />"; 
     $scale = $divSize/$biggest; 
     $newWidth = floor($width * $scale); 
     $newHeight = floor($height * $scale); 
    } else if($biggest < $divSize){ 
     echo "DIVSIZE<br />"; 
     $scale = $biggest/$divSize; 
     $newWidth = floor($width * $scale); 
     $newHeight = floor($height * $scale); 
    } 

    echo "SCALE: ".$scale."<br />"; 
    echo "BIGGEST: ".$biggest."<br />"; 
    echo "WIDTH: ".$width."<br />"; 
    echo "HEIGHT: ".$height."<br />"; 
    echo "NEWWIDTH: ".$newWidth."<br />"; 
    echo "NEWHEIGHT: ".$newHeight."<br />"; 

    $sourceImage = imagecreatefromstring(file_get_contents($fileName)); 
    $thumb = imagecreatetruecolor($newWidth, $newHeight);   
    imagecopyresampled($thumb, $sourceImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); 
    imagedestroy($sourceImage); 

這段代碼工作正常的一些圖片,但不是所有的人。

我有與64的尺寸股利提高64

因爲他們規模完全正常的一些圖片,但對某些圖像輸出圖像的高度也64PX又該由例如32PX。

我不知道是什麼原因導致此問題。

如果您需要更多信息,請詢問。

回答

0

沒關係。

我想出了自己。

我simplefied代碼用於縮放縮略圖頗有幾分:

$biggest = ($width > $height) ? $width : $height; 
$newWidth = 0; 
$newHeight = 0; 
$scale = ($biggest >= $thumbSize) ? $thumbSize/$biggest : $biggest/$thumbSize; 

$newWidth = floor($width * $scale); 
$newHeight = floor($height * $scale);  

$sourceImage = imagecreatefromstring(file_get_contents($fileName)); 
$thumb = imagecreatetruecolor($newWidth, $newHeight);   
imagecopyresampled($thumb, $sourceImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); 
imagedestroy($sourceImage); 

也許有人可以用這個。

3

你的功能很好,但有時你的圖像應該有一個靜態的大小(這避免了在一些網頁上打破設計)。

在這種情況下,您可以使用此功能。它調整適合在定義的寬度/高度內的圖像,如果圖像與所需縮略圖的比例不同,則將未使用的可用空間設置爲透明。

function resizePreservingAspectRatio($img, $targetWidth, $targetHeight) 
{ 
    $srcWidth = imagesx($img); 
    $srcHeight = imagesy($img); 

    // Determine new width/height preserving aspect ratio 
    $srcRatio = $srcWidth/$srcHeight; 
    $targetRatio = $targetWidth/$targetHeight; 
    if (($srcWidth <= $targetWidth) && ($srcHeight <= $targetHeight)) 
    { 
     $imgTargetWidth = $srcWidth; 
     $imgTargetHeight = $srcHeight; 
    } 
    else if ($targetRatio > $srcRatio) 
    { 
     $imgTargetWidth = (int) ($targetHeight * $srcRatio); 
     $imgTargetHeight = $targetHeight; 
    } 
    else 
    { 
     $imgTargetWidth = $targetWidth; 
     $imgTargetHeight = (int) ($targetWidth/$srcRatio); 
    } 

    // Creating new image with desired size 
    $targetImg = imagecreatetruecolor($targetWidth, $targetHeight); 

    // Add transparency if your reduced image does not fit with the new size 
    $targetTransparent = imagecolorallocate($targetImg, 255, 0, 255); 
    imagefill($targetImg, 0, 0, $targetTransparent); 
    imagecolortransparent($targetImg, $targetTransparent); 

    // Copies image, centered to the new one (if it does not fit to it) 
    imagecopyresampled(
     $targetImg, $img, ($targetWidth - $imgTargetWidth)/2, // centered 
     ($targetHeight - $imgTargetHeight)/2, // centered 
     0, 0, $imgTargetWidth, $imgTargetHeight, $srcWidth, $srcHeight 
    ); 

    return $targetImg; 
} 

用例:

$gd = imagecreatefromjpeg("images/image5.jpg"); 
$resized = resizePreservingAspectRatio($gd, 100, 100); 
header("Content-type: image/png"); 
imagepng($resized); 

此圖片:

enter image description here

變爲:

enter image description here

+0

我很抱歉,但我不 得到它。你的功能做了什麼,然後我的功能呢? 我的函數創建一個適合給定尺寸的縮略圖。我用css把拇指放在中間。 我認爲,無論您使用GD還是CSS來定位拇指,它都是一種品味問題。現在我的圖像越小越好。 – MmynameStackflow

+0

請讓現在認識你的功能有什麼好處? – MmynameStackflow

+0

它使用固定的寬度和高度。如果您在表格中顯示這些圖像,則避免有時會出現大列,有時候會出現高列:您總是會獲得相同的大小。 –