Image Resize Script won't crop height properly.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Trenchant
Forum Contributor
Posts: 291
Joined: Mon Nov 29, 2004 6:04 pm
Location: Web Dummy IS

Image Resize Script won't crop height properly.

Post by Trenchant »

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?

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>
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Image Resize Script won't crop height properly.

Post by onion2k »

Grab the library in my signature and look in image/fx/resize.class.php and image/fx/crop.class.php. It should be relatively straightforward to understand.

Or just use the library. :) For example..

Code: Select all

include_once "image/image.inc.php";
 
    $image = new image($tmp_name);
 
    if (!$image->testImageHandle()) {
        //Display an error to the user;
    }
 
    $image->attach(new image_fx_resize(0,600));
    $image->attach(new image_fx_crop(800));
 
    $image->imageJpeg("picture.jpg");
That would resize all the images to 600 pixels tall and then crop any that are wider than 800 pixels down to 800. In general you're better off cropping horizontally than vertically, otherwise you end up chopping off peoples faces from portraits.
Post Reply