Nate_23 Posted August 20, 2009 Share Posted August 20, 2009 Does anyone know how to limit the size (height, width) of pictures in a file upload? Link to comment https://forums.phpfreaks.com/topic/171073-resizing-picture-in-a-file-upload/ Share on other sites More sharing options...
AikenAiken Posted August 20, 2009 Share Posted August 20, 2009 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; } Link to comment https://forums.phpfreaks.com/topic/171073-resizing-picture-in-a-file-upload/#findComment-902228 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.