URL: http://www.netlobo.com/php_image_resize_gd2.html#
To use the resize function you must pass in the forced width, forced height, source image, and destination image. The function then uses the GD2 library functions to read the source image's size. It will then calculate the new image's size based off of the forced width and height. This function will maintain the original image's aspect ratio so your image won't look distorted. It won't change the source image at all but when the function is done running you will end up with the destination image which is resized to the dimensions you passed into the function. As far as I know, this function requires the GD2 library although you might be able to get it to work with the GD1 library. The GD library extension must be enabled in PHP before you can use GD2 functions so keep that in mind as well. You can use the PHP function phpinfo() to see if GD2 is enabled or to see what version of the GD library your server is using.
<?php function resampimagejpg( $forcedwidth, $forcedheight, $sourcefile, $destfile ) { $fw = $forcedwidth; $fh = $forcedheight; 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 ) ) { } } else if ( $t == 2 ) { } } ?>
Comments
Subscribe to comments
You need to login to post a comment.

This function is a bit limited in that it only works for jpeg files...
You can use if-statements to check which image you're using. Go check this function I made here on Snipplr, it'll help you out:
For instance, you can check for PNG files like this:
endsWith(".png",$file,false);Then, if you have a PNG for instance, simple do ancreateimagefrompng(), which excists, just likecreateimagefromgif()exists.