Function:
function force_width_height($width,$height,$max_width=250,$max_height=50) {
if ($width >$max_width) $over_width=true; else $over_width=false;
if ($height >$max_height) $over_height=true; else $over_height=false;
if ($over_height and $over_width)
{
return ‘width=”$max_width” height=”‘.($max_width*$height)/$width.'” ‘;
}
elseif ($over_height)
{
return ‘width=”‘.($max_height*$width)/$height.'” height=”$max_height” ‘;
}
elseif ($over_width)
{
return ‘width=”‘.$max_width.'” height=”‘.($max_width*$height)/$width.'” ‘;
}
else
{
return ‘width=”‘.$width.'” height=”‘.$height.'” ‘;
}
}
Usage (let’s say we need to fit some image to width=87px and height=65px):
$site_path = ‘/var/www/html/domain.com’; //full site path (needed to check if file exists)
$web_path = ‘domain.com’;
$image_filename = ‘/images/image.gif’; //Filename of image
if(files_exists($site_path . $image_filename)) list($width,$height)=getimagesize($image_url);
$fixed_size_attr = force_width_height($width, $height, 87, 65);
echo ‘< img src=”http://’ . $web_path . $image_url.'” ‘.$fixed_size_attr.’ >’;
I hope you don’t mind but I fixed your function because it was buggy 🙂
function force_width_height($width, $height, $max_width = null, $max_height = null) {
if ($width >= $height) {
$x = $width / $height;
} else {
$x = $height / $width;
}
$finalWidth = $max_height / $x;
$finalHeight = $max_width / $x;
if ($x == 1) {
if ($width > $max_width) {
if ($max_width $max_width)
$over_width = true; else
$over_width = false;
if ($height > $max_height)
$over_height = true; else
$over_height = false;
if ($over_height && $over_width) {
if ($width >= $height) {
return ‘width=”‘ . $max_width . ‘” height=”‘ . $finalHeight . ‘” ‘;
} else {
return ‘width=”‘ . $finalWidth . ‘” height=”‘ . $max_height . ‘” ‘;
}
} elseif ($over_height) {
return ‘width=”‘ . $finalWidth . ‘” height=”‘ . $max_height . ‘” ‘;
} elseif ($over_width) {
return ‘width=”‘ . $max_width . ‘” height=”‘ . $finalHeight . ‘” ‘;
} else {
return ‘width=”‘ . $width . ‘” height=”‘ . $height . ‘” ‘;
}
}
}
it works great in combination with fitImage 🙂
actually mine was buggy too 😀 here is a new version 🙂
function force_width_height($width, $height, $max_width = null, $max_height = null) {
$x = $width / $height ;
if ($width > $max_width) $over_width = true; else $over_width = false;
if ($height > $max_height) $over_height = true; else $over_height = false;
if ($width == $height) {
return ‘width=”‘ . $max_width . ‘” height=”‘ . $max_height . ‘” ‘;
} else if ($width > $height){
if ($over_height || $over_width) {
if (($width / $max_width) > ($height / $max_height)) {
$finalWidth = $max_width;
$finalHeight = $finalWidth / $x;
} else if (($width / $max_width) $width) {
if ($over_height || $over_width) {
if (($width / $max_width) > ($height / $max_height)) {
$finalWidth = $max_width;
$finalHeight = $finalWidth * $x;
} else if (($width / $max_width) < ($height / $max_height)) {
$finalHeight = $max_height;
$finalWidth = $finalHeight / $x;
} else {
$finalWidth = $max_width;
$finalHeight = $max_height;
}
return 'width="' . $finalWidth . '" height="' . $finalHeight . '" ';
} else {
return 'width="' . $width . '" height="' . $height . '" ';
}
}
}