I've developed this snippet to scale images to a size, set by the maximum width || height.
This snippet is fantastic for galleries, cutouts, small webpages, limited sized webpage, forums, anywthing where users can manually edit the webpage and create a "possibility" of screwing it up, this should help!
This does not distort images, whatsoever! it rescales the width and height according to the maximum width/height you allocate.
I am working on developing a function which will cover both in one snippet, but for now, this is what i have :p
How it works (examples)
Insert the function in-between the img tags where width & height paramaters usually would go;
<?php
$img = 'images/myfiles/image1.jpg';
echo '<img src="' .$img. '" ' .width_scale($img,"200"). '>';
?>
///////////////////////////
// Scale images by width //
///////////////////////////
function width_scale($file,$maxwidth) {
list($width,$height) = getimagesize($file);
if ($width > $maxwidth) {
$new_width = $maxwidth;
$dec_per = $new_width / $width * 100;
$new_height = $height / 100 * $dec_per;
return 'width="' .$new_width. '" height="' .$new_height. '"';
}
else {
return 'width="' .$width. '" height="' .$height. '"';
}
}
////////////////////////////
// Scale images by height //
////////////////////////////
function height_scale($file,$maxheight) {
list($width,$height) = getimagesize($file);
if ($height > $maxheight) {
$new_height = $maxheight;
$dec_per = $new_height / $height * 100;
$new_width = $width / 100 * $dec_per;
return 'width="' .$new_width. '" height="' .$new_height. '"';
}
else {
return 'width="' .$width. '" height="' .$height. '"';
}
}
Would this work (not a big PHP user):
Syntax: image_scale($img,"200","250")
Example:
<?php
$img = 'images/myfiles/image1.jpg';
echo '<img src="' .$img. '" ' .width_scale($img,"200","250"). '>';
?>
//////////////////////////////////////////////////////////////
// Scale images by width+height //
//////////////////////////////////////////////////////////////
function image_scale($file,$maxwidth,$maxheight) {
list($width,$height) = getimagesize($file);
if (($width > $maxwidth) && ($height > $maxheight)) {
$new_width = $maxwidth;
$new_height = $maxheight;
$dec_perw = $new_width / $width * 100;
$dec_perh = $new_height / $height * 100;
$new_width = $width / 100 * $dec_per;
$new_height = $height / 100 * $dec_per;
return 'width="' .$new_width. '" height="' .$new_height. '"';
}
else {
return 'width="' .$width. '" height="' .$height. '"';
}
}