Rate this script:  I Love it  /   I Hate it

thumbnail with gd


Code


<?php
function thumb($img, $w, $h, $fill = true) {
        if (!extension_loaded('gd') && !extension_loaded('gd2')) {
                trigger_error("No dispones de la libreria GD para generar la imagen.", E_USER_WARNING);
                return false;
        }

        $imgInfo = getimagesize($img);
        switch ($imgInfo[2]) {
                case 1: $im = imagecreatefromgif($img); break;
                case 2: $im = imagecreatefromjpeg($img)break;
                case 3: $im = imagecreatefrompng($img); break;
                defaulttrigger_error('Tipo de imagen no reconocido.', E_USER_WARNING)break;
        }

        if ($imgInfo[0] <= $w && $imgInfo[1] <= $h && !$fill) {
                $nHeight = $imgInfo[1];
                $nWidth = $imgInfo[0];
        }else{
                if ($w/$imgInfo[0] < $h/$imgInfo[1]) {
                        $nWidth = $w;
                        $nHeight = $imgInfo[1]*($w/$imgInfo[0]);
                }else{
                        $nWidth = $imgInfo[0]*($h/$imgInfo[1]);
                        $nHeight = $h;
                }
        }
 
        $nWidth = round($nWidth);
        $nHeight = round($nHeight);

        $newImg = imagecreatetruecolor($nWidth, $nHeight);

        imagecopyresampled($newImg, $im, 0, 0, 0, 0, $nWidth, $nHeight, $imgInfo[0], $imgInfo[1]);

        header("Content-type: ". $imgInfo['mime']);

        switch ($imgInfo[2]) {
                case 1: imagegif($newImg); break;
                case 2: imagejpeg($newImg)break;
                case 3: imagepng($newImg); break;
                defaulttrigger_error('Imposible mostrar la imagen.', E_USER_WARNING)break;
        }
 
        imagedestroy($newImg);
}
?>
 

 

 
thumbnail with gd scripts | thumbnail with gd snippet | thumbnail with gd example | thumbnail with gd tutorial | thumbnail with gd code