在保持比例的同时调整图像大小

时间:2013-07-23 08:40:51

标签: php image math resize

我在谷歌上发现了这个功能,但它无法正常工作。我测试了818x800图像,该功能将返回相同的大小。怎么了?

function imageResize($width, $height, $target) {
//takes the larger size of the width and height and applies the formula accordingly...this is so this script will work  dynamically with any size image
if ($width > $height) {
$percentage = ($target / $width);
} else {
$percentage = ($target / $height);
}
//gets the new value and applies the percentage, then rounds the value
$width = round($width * $percentage);
$height = round($height * $percentage);
//returns the new sizes in html image tag format...this is so you can plug this function inside an image tag and just get the
return "width=\"$width\" height=\"$height\"";

}

$mysock = getimagesize("http://www.pirate-punk.com/pochette.php?i=ZGwvcHAvNjI1MC84NiBDcmV3IC0gMjAwMCAtIEJhZCBCYWQgUmVnZ2FlLnppcCM4IDYgQ3JldyAtIEJhZCBCYWQgUmVnZ2FlLWZyb250ICBbd3d3LlBpcmF0ZS1QdW5rLm5ldF0uanBnCg==");
echo "<img src=\"$pochetteimg\" ";
imageResize($mysock[0], $mysock[1], 300);
echo ">";

我正在尝试将图像调整为300px宽度,同时保持比率

3 个答案:

答案 0 :(得分:0)

<?php
function imageResize($width, $height, $target) {
if ($width > $height) { $percentage = ($target / $width); } else { $percentage = ($target / $height); }
$width = round($width * $percentage);
$height = round($height * $percentage);
return "width=\"$width\" height=\"$height\"";
}

$mysock = getimagesize("pochette.jpg");
$size = imageResize($mysock[0], $mysock[1], 300);
echo "<img src=\"pochette.jpg\" ".$size." />";
?>

试试这样。

答案 1 :(得分:0)

试试这样:

$mysock = getimagesize("YourUrl");
echo "<img src='" . $pochetteimg . "' " . imageResize($mysock[0], $mysock[1], 300) . "/>";

答案 2 :(得分:0)

imageresize功能实际上并未调整图像大小。它只是返回图像的宽度和高度,保持纵横比。是你想要实现的吗?