0

我有动态加载的图像,不能大于 115px。我从外部来源获得它们,所以我无法控制它。

我可以通过以下方式获得图像尺寸:

$list = getimagesize($imagePath);
$width = $list[0];
$height = $list[1];

但现在如果它们大于 115px,我需要调整大小。试过这个,但它没有保持比例:

$height = round(($height * $width) / $width);

有人可以帮我弄这个吗?提前致谢。

4

5 回答 5

1

您应该考虑使用imagecopyresampledimagecopyresized

如果您希望保持图像正确缩放,您将不得不投入更多的算术,但这应该不会太糟糕。

这是一些用于缩放图像的伪代码:

$max_height = 115;
if ($height > $max_height)
{
    $scale = $max_height / $height;
    $height = intval($height * $scale);
    $width  = intval($width  * $scale);
}

如果高度和宽度必须小于 115 ,以下是更通用的形式:

$max_size = 115;
if (max($height, $width) > $max_size)
{
    $scale = $max_size / max($height, $width);
    $height = intval($height * $scale);
    $width  = intval($width  * $scale);
}

这保证了图像的最大尺寸(高度或宽度)不会大于 115。

于 2012-10-02T17:09:39.930 回答
0

If you mean they can't be larger than 115x115. The process you'd have to go by is something like:

If wider than 115, set width to be 115px and height = height/original_width * 115;

I have some code from an image uploader script which does this

    list($Width, $Height) = getimagesize($row["img_file"]);

    if(($Width > $Height) && ($Width > 115))
    {
        $Height = ceil(($Height / $Width) * 115) ;
        $Width = 115;
    }
    elseif(($Height > $Width) && ($Height > 115))
    {
        $Width = ceil(($Width / $Height)* 115);
        $Height = 115;
    }
    elseif(($Height > 115) && ($Height == $Width))
    {
        $Height = 115;
        $Width = 115;
    }

After you have the correct dimensions you can resize the image using imagecreatefromjpeg/png/gif and then imagecopyresampled.

Another section of code from the image upload script which resized images from URLs using the calculated dimensions

        if ($extension == "png")
        {
            $image = imagecreatefrompng($URLofImage)or die("Width Of Image: ".$widthofImage." Height Of Image: ".$heightofImage." Height: ".$height." Width: ".$width);
            $image_p = imagecreatetruecolor($widthofImage, $heightofImage)or die("Width Of Image: ".$widthofImage." Height Of Image: ".$heightofImage." Height: ".$height." Width: ".$width);
            imagealphablending($image_p, false);
            $color = imagecolorallocatealpha($image_p, 0, 0, 0, 0);
            imagesavealpha($image_p, true);
            list($width, $height) = getimagesize($URLofImage);

            imagecopyresampled($image_p, $image, 0, 0, 0, 0, $widthofImage, $heightofImage, $width, $height);
            imagepng($image_p, "./images/thumbs/".$name, 9);

            return ("./images/thumbs/".$name);
        }
于 2012-10-02T17:12:27.140 回答
0

根据您的最低浏览器要求,并且这确实适用于 HTML 输出,您可以在 html 中处理它,并为自己节省一些处理。

<img alt='image alt' src='imagesrc' style='max-width:115px; max-height:115px; height:auto; width:auto;' />
于 2012-10-02T18:45:09.897 回答
0

你应该直接看http://www.php.net/manual/en/function.getimagesize.php#97564

$max_width = 115; 
$max_height = 115;

list($width, $height) = getimagesize($imagePath);
$ratioh = $max_height/$height;
$ratiow = $max_width/$width;
$ratio = min($ratioh, $ratiow);

// New dimensions
$width = intval($ratio * $width);
$height = intval($ratio * $height);
于 2012-10-02T17:12:57.700 回答
0

你的比例可以说是高/宽比,所以把它放到一个变量中:

$比率= $宽度/$高度;

如果你有这样的图像:100x200 像素,你的比例是1/2。如果您想要一个高度为 20 像素的图像,则宽度应为 10 像素。

$newHeight = 20;

$newWidth = round($newHeight * $ratio); //给你10

还可以说您想将宽度设为 10px,只需使用它来查找新宽度:

$newWidth = 10;

$newHeight = round($newWidth / $ratio);

因此,您应该至少指定一个边长,然后 ratio 将帮助您找到另一边。

于 2012-10-02T17:17:38.947 回答