Programming - PHP - Resizing an image on the fly
Quick code snippet in PHP to resize an image on the client
side:
<?php
function imageResize($width, $height, $target) {
//takes the larger size of the width and height and applies
theand
formula accordingly...this is so this script will
workand
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\"";
}
?>
To run it, use:
<img src="images/sock001.jpg" <?php
imageResize($mysock[0],and
$mysock[1], 150); ?>>
and