-
<?
-
// constrained resize.. (max value)
-
// constraining to a MAXIMUM value means that we want the image
-
// to be NO MORE then $w or $h…
-
// so when it resizes an image… it'll constrain it to the same dimensions
-
// but it will not exceed in width or height the values of $max_width or $max_height
-
// http://thedevnet.com/php/snippets/constrained-image-resize-minmax-values/
-
-
function c_resize( $img, $max_width, $max_height)
-
{
-
$FullImage_width = imagesx ($img); // Original image width
-
$FullImage_height = imagesy ($img); // Original image height
-
-
// now we check for over-sized images and pare them down
-
// to the dimensions we need for display purposes
-
$ratio = ( $FullImage_width > $max_width ) ? (real)($max_width / $FullImage_width) : 1 ;
-
$new_width = ((int)($FullImage_width * $ratio));
-
$new_height = ((int)($FullImage_height * $ratio));
-
//check for images that are still too high
-
$ratio = ( $new_height > $max_height ) ? (real)($max_height / $new_height) : 1 ;
-
$new_width = ((int)($new_width * $ratio)); //mid-size width
-
$new_height = ((int)($new_height * $ratio)); //mid-size height
-
-
$NEW_IM = imagecreatetruecolor( $new_width , $new_height );
-
imagecopyresampled( $NEW_IM, $img,
-
0,0, 0,0, //starting points
-
$new_width, $new_height,
-
$FullImage_width, $FullImage_height );
-
-
return $NEW_IM;
-
}
-
-
// constrained resize.. (min value)
-
// constraining to a minimum value means that we want the image
-
// to be NO LESS then $w or $h…
-
// so when it resizes an image… it'll constrain it to the same dimensions
-
// but it will not be reduced less then a certain width or certain height…
-
// this is useful for a crop function
-
// http://thedevnet.com/php/snippets/constrained-image-resize-minmax-values/
-
function c_minresize($img, $w, $h)
-
{
-
$src_w = imagesx($img);
-
$src_h = imagesy($img);
-
-
$pct_w = ($w / $src_w) * 100;
-
$pct_h = ($h / $src_h) * 100;
-
-
if($pct_w > $pct_h)
-
{
-
$new_w = $src_w * ($pct_w / 100);
-
$new_h = $src_h * ($pct_w / 100);
-
}
-
else
-
{
-
$new_w = $src_w * ($pct_h / 100);
-
$new_h = $src_h * ($pct_h / 100);
-
}
-
-
$new_w *= 1.05;
-
$new_h *= 1.05;
-
-
return c_resize( $img, $new_w, $new_h);
-
}
-
?>