Etiket arşivi: Images

Php Gd Create Thumbnail (crop)

<?php
    function createThumbnail($fileSrc,$thumbDest,$thumb_width,$thumb_height){
        $ext=strtolower(substr($fileSrc,strrpos($fileSrc,".")));
        if($ext==".png"){
            @$base_img = ImageCreateFromPNG($fileSrc);
        }else if(($ext==".jpeg") || ($ext == ".jpg")){
            @$base_img = ImageCreateFromJPEG($fileSrc);
        }else if(($ext == ".gif")){
            @$base_img = imagecreatefromgif($fileSrc);
        }
        if(!$base_img) return false;

        $img_width = imagesx($base_img);
        $img_height = imagesy($base_img);

        if($thumb_width <= $thumb_height){
            $size_percent = intval($thumb_height / ($img_height / 100));
            $new_width = intval($size_percent * ($img_width/100));
            $new_height = $thumb_height;
            $difference_width = intval(($new_width - $thumb_width)/2);
            $difference_height = 0;
        }else{
            $size_percent = intval($thumb_width / ($img_width / 100));
            $new_height = intval($size_percent * ($img_height/100));
            $new_width = $thumb_width;
            $difference_width = 0;
            $difference_height = intval(($new_height - $thumb_height)/2);
        }
        $thumb_img = ImageCreateTrueColor($new_width, $new_height);
        ImageCopyResampled($thumb_img,$base_img,0,0,0,0,$new_width,$new_height,$img_width,$img_height);

        $thumb_images = ImageCreateTrueColor($thumb_width, $thumb_height);
        ImageCopyMerge($thumb_images,$thumb_img,0,0,$difference_width,$difference_height,$thumb_width,$thumb_height,100);

        if($ext == ".png"){
            ImagePNG($thumb_images, $thumbDest);
        }else if(($ext == ".jpeg") || ($ext == ".jpg")){
            ImageJPEG($thumb_images, $thumbDest,82);
        }else if($ext == ".gif"){
            ImageGIF($thumb_images, $thumbDest);
        }

        @ImageDestroy($base_img);
        @ImageDestroy($thumb_images);
        @ImageDestroy($thumb_img);

        return true;
    }
?>