GD Image调整大小不适用于PHP

时间:2013-04-12 08:15:20

标签: php gd thumbnails

我有以下代码:

$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的div。

对于某些图像,它们可以很好地缩放,但对于某些图像,输出图像的高度也是64像素,应该是例如32像素。

我不知道是什么导致了这个问题。

如果您需要更多信息,请询问。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

没关系。

我自己想出来了。

我简化了用于缩放缩略图的代码:

$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);

也许有人可以使用它。