0

我有以下代码:

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

对于某些图像,它们可以完美缩放,但对于某些图像,输出图像的高度也是 64 像素,例如 32 像素。

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

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

4

2 回答 2

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

这个图片 :

在此处输入图像描述

变成:

在此处输入图像描述

于 2013-04-12T22:03:31.497 回答
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);

也许有人可以使用这个。

于 2013-04-12T09:13:02.570 回答