简体   繁体   中英

resize image while keeping ratio

I found this function on google but it isn't working. I tested with a 818x800 image and the function will return the same size. What's wrong ?

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

I'm trying to resize the image to 300px width while keeping the ratio

<?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." />";
?>

Try it like this.

Try like this :

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

The imageresize function is not actually resizing the image. Its just returning the width and height of the image maintaining the aspect ratio. Is it that you are trying to achieve?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM