Originally Posted by DatraxMada
Here is a function which is simpler to call and doesnt mix the PHP and IMG tags.
<?php
function imageResize($image, $target, $alt=NULL) {
//Gets the width and height of the image and outputs it as $theimage[0] (width) and $theimage[1] (height)
$theimage = getimagesize($image);
$width = $theimage[0];
$height = $theimage[1];
//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 inside an image tag so you can call it with "imageResize("images/sock001.jpg", "Some alt text", $target)"
echo "<img src=\"$image\" width=\"$width\" height=\"$height\" alt=\"$alt\">";
}
?>
<!- Now, you can simply use your imageResize function instead of mixing up your PHP with an IMG tag -->
<?php imageResize("images/sock001.jpg", 50, "Oh what a beautiful sock"); ?>