Image Resize Script won't crop height properly.
Posted: Thu Aug 07, 2008 11:32 pm
I'm using an example/premade image resize function. It scales an image to a max height or width and saves the new version to a different file. The only problem is if I set a different max height and width it only scales the larger number which in my case doesn't work.
This is for gallery software. The width cannot exceed 800 pixels and the height cannot exceed 600 pixels or images don't show up right. Right now it resizes images to a max size of 800 pixels by both height and width. Landscape photo's show up great but portrait photo's dont they get cut off when displayed in the gallery.
I would fix this on my own but I've tried and I can't understand the logic behind image resizing. Can someone give me a hand?
This is for gallery software. The width cannot exceed 800 pixels and the height cannot exceed 600 pixels or images don't show up right. Right now it resizes images to a max size of 800 pixels by both height and width. Landscape photo's show up great but portrait photo's dont they get cut off when displayed in the gallery.
I would fix this on my own but I've tried and I can't understand the logic behind image resizing. Can someone give me a hand?
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Image Resize test</title>
</head>
<body>
<?php
function resampimagejpg( $forcedwidth, $forcedheight, $sourcefile, $destfile )
{
$fw = $forcedwidth;
$fh = $forcedheight;
$is = getimagesize( $sourcefile );
if( $is[0] >= $is[1] )
{
$orientation = 0;
}
else
{
$orientation = 1;
$fw = $forcedheight;
$fh = $forcedwidth;
}
if ( $is[0] > $fw || $is[1] > $fh )
{
if( ( $is[0] - $fw ) >= ( $is[1] - $fh ) )
{
$iw = $fw;
$ih = ( $fw / $is[0] ) * $is[1];
}
else
{
$ih = $fh;
$iw = ( $ih / $is[1] ) * $is[0];
}
$t = 1;
}
else
{
$iw = $is[0];
$ih = $is[1];
$t = 2;
}
if ( $t == 1 )
{
$img_src = imagecreatefromjpeg( $sourcefile );
$img_dst = imagecreatetruecolor( $iw, $ih );
imagecopyresampled( $img_dst, $img_src, 0, 0, 0, 0, $iw, $ih, $is[0], $is[1] );
if( !imagejpeg( $img_dst, $destfile, 90 ) )
{
return false;
}
imagedestroy($img_src);
imagedestroy($img_dst);
}
else if ( $t == 2 )
{
copy( $sourcefile, $destfile );
return true;
}
}
resampimagejpg( '800', '600', 'upload/1.jpg', 'temp/1.jpg' );
?>
</body>
</html>