Jump to content

Resizing picture in a file upload.


Nate_23

Recommended Posts

function createthumb( $src_filename, $dst_filename, $max_width, $max_height )

{

        // Get information about the image

        list($src_width, $src_height, $type, $attr) = getimagesize( $src_filename );

 

        // Load the image based on filetype

        switch( $type ) {

        case IMAGETYPE_JPEG:

                $src_img = imagecreatefromjpeg( $src_filename );

                break;

        case IMAGETYPE_PNG:

                $src_img = imagecreatefrompng( $src_filename );

                break;

        case IMAGETYPE_GIF:

                $src_img = imagecreatefromgif( $src_filename );

                break;

        default:

                return false;

        }

        // Did the image fail to load?

        if ( !$src_img ) {

                return false;

        }

 

        // Calculate the output size based on the original's aspect ratio

        if ( $src_width / $src_height > $max_width / $max_height ) {

                $thumb_w = $max_width;

                $thumb_h = $src_height * $max_width / $src_width;

        }

        else {

                $thumb_w = $src_width * $max_height / $src_height;

                $thumb_h = $max_height;

        }

 

        // Create the output image and copy to source to it

        $dst_img = ImageCreateTrueColor( $thumb_w, $thumb_h );

        imagecopyresampled( $dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $src_width, $src_height );

 

        /* Sharpen before save?

        $sharpenMatrix = array( array(-1, -1, -1), array(-1, 16, -1), array(-1, -1, -1) );

        $divisor = 8;

        $offset = 0;

        imageconvolution( $dst_img, $sharpenMatrix, $divisor, $offset );

        //*/

 

        // Get information about the output image

        $path_info = pathinfo($dst_filename);

     

        // Setup default return info

        $return = true;

     

        // Load the image based on filetype

        switch ( strtolower( $path_info['extension'] ) ) {

        case 'jpg':

        case 'jpeg':

                imagejpeg( $dst_img, $dst_filename );

                break;

        case 'png':

                imagepng( $dst_img, $dst_filename );

                break;

        case 'gif':

                imagegif( $dst_img, $dst_filename );

                break;

        default:

                $return = $false;

        }

 

        // Clean up memory

        imagedestroy( $dst_img );

        imagedestroy( $src_img );

     

        return $return;

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.