<?php

/* metodo de usar:
    resizeImage($src, $target, $width, $height, $quality, $force)
    $src     : caminho para a imagem original : original image path
    $target  : caminho para a imagem destino  : target image path
    $width   : largura para a imagem destino  : target image width
    $height  : algura para a imagem destino   : target image height
    $quality : qualidade da imagem destino    : target image quality
    $force   : forca a altura e largura       : force width and height to be fixed
*/

/* exemplos de uso : examples of use:
    resizeImage($src, $target, 15, 15, 75, 1)     // cria uma imagem de 15x15 na qualidade 75
                                                  // creates an image of 15x15 with the quality 75
    resizeImage($src, $target, 128, 128, 75, 0)   // cria uma imagem com maximo de 128 de largura ou altura na qualidade 75
                                                  // creates an image with a maximum of 128 pixels of height or width in the 75 quality
    resizeImage($src, $target, 500, 0, 100, 0)    // cria uma imagem com um maximo de 500px de largura na qualidade maxima
                                                  // creates an image with a maximum of 500 pixels of width in the maximum quality
*/


function resizeImage($src,$dest,$width,$height,$quality=75,$force) {
    
// Joao Orui May 2nd 2004
    // Edited: September 2nd 2005
    
$imginfo     = @getimagesize($src);
    
$widthimage  $imginfo[0];
    
$heightimage $imginfo[1];
    if (
$imginfo == null)
        return 
False;

    if (
$imginfo[2] != && $imginfo[2] != && $imginfo[2] != 3)
        return 
False;
        
# 1==GIF 2==JPEG 3==PNG

    
if (!$force) {
        if (
$widthimage  >= $width && $width) {
            
$fator        $widthimage / (float)$width;
            
$newwidth     $width;
            
$newheight    ceil($heightimage $fator);
            
$heightimage  $newheight;
            
$widthimage   $width;
        } else {
            
$newwidth     $widthimage;
            
$newheight    $heightimage;
        }
    
        if (
$heightimage >= $height && $height) {
            
$fator       $heightimage / (float)$height
            
$newheight   $height;
            
$newwidth    ceil($widthimage $fator);
        }

        
$off_w 0;
        
$off_h 0;
    } else {
        
$newwidth  $width;
        
$newheight $height;
        if (
$imginfo[0] > $imginfo[1]) {
           
$off_w = ($imginfo[0]-$imginfo[1])/2;
           
$off_h 0;
           
$imginfo[0] = $imginfo[1];
        } elseif (
$imginfo[1] > $imginfo[0]) {
           
$off_w 0;
           
$off_h = ($imginfo[1]-$imginfo[0])/2;
           
$imginfo[1] = $imginfo[0];
        } else {
           
$off_w 0;
           
$off_h 0;
        }
    }

    if (
$imginfo[2] == 1)
        
$src_img =imagecreatefromgif($src);
    elseif (
$imginfo[2] == 2)
        
$src_img imagecreatefromjpeg($src);
    else
        
$src_img imagecreatefrompng($src);

    if (!
$src_img)
        return 
False;

    if (
function_exists('imagecreatetruecolor'))
        
$dst_img imagecreatetruecolor($newwidth$newheight);
    else
        
$dst_img imagecreate($newwidth$newheight);

    if (
function_exists('imagecopyresampled'))
        
imagecopyresampled($dst_img$src_img00$off_w$off_h$newwidth, (int)$newheight$imginfo[0], $imginfo[1]);
    else
        
imagecopyresized($dst_img$src_img00$off_w$off_h$newwidth, (int)$newheight$imginfo[0], $imginfo[1]);

    
imagejpeg($dst_img$dest$quality);
    
imagedestroy($src_img);
    
imagedestroy($dst_img);

    
$imginfo getimagesize($dest);

    if (!
$imginfo) {
        @
unlink($dest);
        return 
False;
    } else {
        return 
True;
    } 
}


?>