Mega Code Archive

 
Categories / Php / Graphics
 

Image Thumb

<?php /* some settings */ ignore_user_abort(); set_time_limit( 0 ); error_reporting( FATAL | ERROR | WARNING ); /* security check */ ini_set( 'register_globals', '0' ); /* start buffered output */ ob_start(); /* temporary kludge */ //while ( list( $key, $val ) = each( $HTTP_GET_VARS ) ) $_GET[ $key ] = $val; /* some checks */ if ( ! isset( $_GET['s'] ) ) die( 'Source image not specified' ); if ( isset( $_GET['w'] ) && ereg( "^[0-9]+$", $_GET['w'] ) ) $MAX_WIDTH = $_GET['w']; else $MAX_WIDTH = 320; if ( isset( $_GET['h'] ) && ereg( "^[0-9]+$", $_GET['h'] ) ) $MAX_HEIGHT = $_GET['h']; else $MAX_HEIGHT = 200; /* get source image size */ $src_size = getimagesize( $_GET['s'] ); /* resize the image (if needed) */ if ( $src_size[0] > $MAX_WIDTH && $src_size[1] > $MAX_HEIGHT ) { if ( $src_size[0] > $src_size[1] ) { $dest_width = $MAX_WIDTH; $dest_height = ( $src_size[1] * $MAX_WIDTH ) / $src_size[0]; } else { $dest_width = ( $src_size[0] * $MAX_HEIGHT ) / $src_size[1]; $dest_height = $MAX_HEIGHT; } } else if ( $src_size[0] > $MAX_WIDTH ) { $dest_width = $MAX_WIDTH; $dest_height = ( $src_size[1] * $MAX_WIDTH ) / $src_size[0]; } else if ( $src_size[1] > $MAX_HEIGHT ) { $dest_width = ( $src_size[0] * $MAX_HEIGHT ) / $src_size[1]; $dest_height = $MAX_HEIGHT; } else { $dest_width = $src_size[0]; $dest_height = $src_size[1]; } if ( extension_loaded( 'gd' ) ) { /* check the source file format */ $ext = substr( $_GET['s'], strrpos( $_GET['s'], '.' ) + 1 ); if ( $ext == 'jpg' || $ext == 'jpeg' ) $src = imagecreatefromjpeg( $_GET['s'] ) or die( 'Cannot load input JPEG image' ); else if ( $ext == 'gif' ) $src = imagecreatefromgif( $_GET['s'] ) or die( 'Cannot load input GIF image' ); else if ( $ext == 'png' ) $src = imagecreatefrompng( $_GET['s'] ) or die( 'Cannot load input PNG image' ); else die( 'Unsupported source file format' ); /* create and output the destination image */ $dest = imagecreate( $dest_width, $dest_height ) or die( 'Cannot initialize new GD image stream' ); imagecopyresized( $dest, $src, 0, 0, 0, 0, $dest_width, $dest_height, $src_size[0], $src_size[1] ); if ( imagetypes() & IMG_PNG ) { header ( 'Content-type: image/png' ); imagepng( $dest ); } else if ( imagetypes() & IMG_JPG ) { header ( 'Content-type: image/jpeg' ); imagejpeg( $dest ); } else if ( imagetypes() & IMG_GIF ) { header ( 'Content-type: image/gif' ); imagegif( $dest ); } else print 'Cannot find a suitable output format'; } else print 'GD-library support is not available'; /* end buffered output */ ob_end_flush(); ?>