Location: PHPKode > scripts > KG Image Resizer Thumbnail Class > kg-image-resizer-thumbnail-class/kgResizer.class.php
<?PHP

########################################################################################################################
#
#   KG Resizer v1.0
#   Class Name  : KG Resizer Class
#   Version     : 1.0
#   Requirement : PHP4 >
#   Build Date  : March 8, 2008 - Saturday
#   Developer   : Muharrem ERİN (TÜRKİYE) - hide@address.com - muharremerin.com - mhrrmrn.com - kisiselgunce.com
#   Licence     : GNU General Public License (c) 2008
#
########################################################################################################################

class kgResizer {

    var $image_path = null;
    var $not_found_image = null;
    var $image = null;
    var $resize_image = null;
    var $image_info = null;
    var $save_as_image_name = null;

    function _image_control($image_path, $image, $not_found_image, $save_as_image_name) {
        $this -> image_path = $image_path;
        $this -> image = $image;
        $this -> not_found_image = $not_found_image;
        if ($this -> image != '') {
            if (file_exists($this -> image_path.$this -> image)) {
                $this -> resize_image = $this -> image_path.$this -> image;
                $this -> save_as_image_name = $save_as_image_name;
            }else{
                $this -> resize_image = $this -> image_path.$this -> not_found_image;
                $this -> save_as_image_name = $this -> not_found_image;
            }
        }else{
            $this -> resize_image = $this -> image_path.$this -> not_found_image;
            $this -> save_as_image_name = $this -> not_found_image;
        }
        $this -> image_info = getImageSize($this -> resize_image);
    }

    function _image_resize($default_width, $default_height, $image_bg, $width, $height) {

        if ($this -> image_info['mime'] == 'image/jpeg' || $this -> image_info['mime'] == 'image/jpg') {
            $old_image = imageCreateFromJPEG($this -> resize_image);
        }elseif ($this -> image_info['mime'] == 'image/gif') {
            $old_image = imageCreateFromGIF($this -> resize_image);
        }elseif ($this -> image_info['mime'] == 'image/png') {
            $old_image = imageCreateFromPNG($this -> resize_image);
        }

        if (empty($width)) { $width = $default_width; }
        if (empty($height)) { $height = $default_height; }

        $min_zoom = 1;
        $max_zoom = 200;

        $_x = $this -> image_info[0] / $width * 100;
        $_y = $this -> image_info[1] / $height * 100;
        if((100 - $_x) < (100 - $_y)) {
            $_o = $_x;
        }else{
            $_o = $_y;
        }
        if($_o > 10000 / $min_zoom) { $_o = 10000 / $min_zoom; }
        if($_o < 10000 / $max_zoom) { $_o = 10000 / $max_zoom; }

        $new_x = $this -> image_info[0] / $_o * 100;
        $new_y = $this -> image_info[1] / $_o * 100;

        $pos_x = ($width - $new_x) / 2;
        $pos_y = ($height - $new_y) / 2;

        $new_image = imageCreateTrueColor($width, $height);

        if (empty($image_bg)) { $image_bg = '255,255,255'; }
        $bg_color_cols = explode(',', $image_bg);
        $bg_color = imageColorallocate($new_image, trim($bg_color_cols[0]), trim($bg_color_cols[1]), trim($bg_color_cols[2]));
        imageFill($new_image, 0, 0, $bg_color);

        imageCopyResampled($new_image, $old_image, $pos_x, $pos_y, 0, 0, $new_x, $new_y, $this -> image_info[0], $this -> image_info[1]);

        if ($this -> image_info['mime'] == 'image/jpeg') {
            header("Content-type: image/jpeg");
            header('Content-Disposition: inline; filename='.$this -> save_as_image_name);
            imageJPEG($new_image);
        }elseif ($this -> image_info['mime'] == 'image/gif') {
            header("Content-type: image/gif");
            header('Content-Disposition: inline; filename='.$this -> save_as_image_name);
            imageGIF($new_image);
        }elseif ($this -> image_info['mime'] == 'image/png') {
            header("Content-type: image/png");
            header('Content-Disposition: inline; filename='.$this -> save_as_image_name);
            imagePNG($new_image);
        }
        imageDestroy($old_image);
        imageDestroy($new_image);
    }

    function resizer_set($image_path, $not_found_image, $default_width, $default_height, $image_bg, $image, $width, $height, $save_as_image_name) {
        $this -> _image_control($image_path, $image, $not_found_image, $save_as_image_name);
        $this -> _image_resize($default_width, $default_height, $image_bg, $width, $height);
    }

}
?>
Return current item: KG Image Resizer Thumbnail Class