2013-04-12 39 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

它使用固定的宽度和高度。如果您在表格中显示这些图像,则避免有时会出现大列,有时候会出现高列:您总是会获得相同的大小。 –