Image Resize Class

This class allows an image to be scale resized to a set dimension. You specify a pixel size and the largest edge will be resized to that length and the other edge will be resized proportionately to keep the same scale.

This class is super simple, very useful and uses the popular GD library extension.

Back to Portfolio Page

<?php

class image {

    private 
$src_img$type$src_x$src_y$new_img;

    public function 
__construct(){
    }
    
    public function 
__destruct(){
    }
    
    public function 
load_image($path){
        
$ext strtolower(array_pop(explode('.'$path)));
        if (
preg_match('/jpg|jpeg/'$ext)){
            
$this->type 'jpg';
            
$this->src_img imagecreatefromjpeg($path);
        }elseif (
preg_match('/png/'$ext)){
            
$this->type 'png';
            
$this->src_img imagecreatefrompng($path);
        }else{
            throw new 
Exception('Unable to load image, not a supported file type.'.$ext);
        }
        
$this->src_x imageSX($this->src_img);
        
$this->src_y imageSY($this->src_img);
    }
    
    public function 
scale_resize($thumb_size){
        
$scale min($thumb_size $this->src_x$thumb_size $this->src_y);
        if(
$scale 1){
            
$new_x floor($scale*$this->src_x);
            
$new_y floor($scale*$this->src_y);
            
            
$tmp_img imagecreatetruecolor($new_x$new_y);
            
imagecopyresampled($tmp_img$this->src_img0000$new_x$new_y$this->src_x$this->src_y);
            
imagedestroy($this->src_img);
            
$this->new_img $tmp_img;
        }
        unset(
$thumb_size);
    }
    
    public function 
save($save_to){
        if(
$this->type == 'jpg'){
            @
imagejpeg($this->new_img$save_to);
        }elseif(
$this->type == 'png'){
            @
imagepng($this->new_img$save_to);
        }
        unset(
$this->new_img);
    }

}

?>