【问题标题】:GD Image resize doesn't work PHPGD 图像调整大小不起作用 PHP
【发布时间】:2013-04-04 16:23:14
【问题描述】:

我有以下代码:

$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 像素。

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

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

【问题讨论】:

    标签: php gd thumbnails


    【解决方案1】:

    您的功能很好,但有时您的图像应该具有静态尺寸(这可以避免破坏某些网页的设计)。

    在这种情况下,您可以使用此功能。它调整图像大小以适应定义的宽度/高度,如果图像与所需缩略图的比例不同,则将未使用的可用空间设置为透明。

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

    这张图片:

    变成:

    【讨论】:

    • 对不起,我不明白。你的功能比我的功能做什么?我的函数创建了一个适合给定尺寸的缩略图。我用css将拇指居中。我认为是否使用 GD 或 CSS 使拇指居中是一个品味问题。现在,我的图片可以更小,但也可以更小。
    • 请让我们现在见面你的功能有什么好处?
    • 它使用固定的宽度和高度。如果您在表格中显示这些图像,这可以避免有时有一个大列,有时是一个高列:您总是得到相同的大小。
    【解决方案2】:

    没关系。

    我自己想通了。

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

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

    也许有人可以使用它。

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 2012-08-12
      • 2010-11-03
      • 1970-01-01
      • 1970-01-01
      • 2017-12-09
      • 1970-01-01
      • 2012-12-27
      • 1970-01-01
      相关资源
      最近更新 更多