Mirgolth | Tiens voila une fonction trouvée sur php.net qui fait un resize pour gif, jpg et png.
Code :
- Improved version of ResizeGif given by tjhunter with Height % Widht.
- <?php
- /* ResizeGif with (height % width) */
- function RatioResizeImg( $image, $newWidth, $newHeight){
- //Open the gif file to resize
- eregi(".(.*)$",$image,$regs);
- switch($regs[1]){
- case "gif": $srcImage = ImageCreateFromGIF( $image ); break;
- case "jpg": $srcImage = ImageCreateFromJPEG( $image ); break;
- case "png": $srcImage = ImageCreateFromPNG( $image ); break;
- default: $srcImage = ImageCreateFromGIF( $image ); break;}
- //obtain the original image Height and Width
- $srcWidth = ImageSX( $srcImage );
- $srcHeight = ImageSY( $srcImage );
- // the follwing portion of code checks to see if
- // the width > height or if width < height
- // if so it adjust accordingly to make sure the image
- // stays smaller then the $newWidth and $newHeight
- $ratioWidth = $srcWidth/$newWidth;
- $ratioHeight = $srcHeight/$newHeight;
- if( $ratioWidth < $ratioHeight){
- $destWidth = $srcWidth/$ratioHeight;
- $destHeight = $newHeight;
- }else{
- $destWidth = $newWidth;
- $destHeight = $srcHeight/$ratioWidth;
- }
- // creating the destination image with the new Width and Height
- $destImage = imagecreate( $destWidth, $destHeight);
- //copy the srcImage to the destImage
- ImageCopyResized( $destImage, $srcImage, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight );
- //create the gif
- ImageGif( $destImage );
- //fre the memory used for the images
- ImageDestroy( $srcImage );
- ImageDestroy( $destImage );
- }
- //save output to a buffer
- ob_start();
- //Resize image ( will be stored in the buffer )
- ResizeGif( "/where/image/is/image.gif", "150", "150" );
- //copy output buffer to string
- $resizedImage = ob_get_contents();
- //clear output buffer that was saved
- ob_end_clean();
- //write $resizedImage to Database, file , echo to browser whatever you need to do with it
- ?>
|
|