Location: PHPKode > projects > THINi Reports > THINiReports-0.1.1/Image.class.php
<?php
class Image
{
    public $Image_Original_Path;
    public $Image_Original_Filename;
    public $Image_Original_Width;
    public $Image_Original_Height;
    public $Image_Original_Type;
    public $Image_Original_Attr;
    public $Image_Original_Resource;
    
    private $Image_Scaled_Scale;
    private $Image_Scaled_Width;
    private $Image_Scaled_Height;
    private $Image_Scaled_Resource;
    
    private $Image_Cropped_Resource;
    
    public $Image_Output_Type = "png";
    public $Image_Output_Width;
    public $Image_Output_Height;
    public $Image_Output_Resource;
    
    public function __construct()
    {
    }
    
    public function __destruct()
    {
        if($this->Image_Original_Resource){
            @imagedestroy($this->Image_Original_Resource);
        }
        if($this->Image_Scaled_Resource){
            @imagedestroy($this->Image_Scaled_Resource);
        }
        if($this->Image_Cropped_Resource){
            @imagedestroy($this->Image_Cropped_Resource);
        }
        if($this->Image_Output_Resource){
            @imagedestroy($this->Image_Output_Resource);
        }
    }
    
    public function Image_Load($Image_Original_Path)
    {
        $this->Image_Original_Path = $Image_Original_Path;
        $this->Image_Original_Filename = basename($Image_Original_Path);
        $this->Image_Dimensions($Image_Original_Path);
        $this->Image_Type($Image_Original_Path);
        
        if($this->Image_Original_Type && !$this->Image_Original_Resource){
            $this->Image_Create_From_Type($Image_Original_Path);
        }
        $this->Image_Output_Resource = $this->Image_Original_Resource;
    }
    
    /*
     * Image_Create_From_Type
     * Creates an image resource using GD Library function 'imagecreatefrom*'
     */
    private function Image_Create_From_Type($Image_Path)
    {
        $Image_Create_Function = "imagecreatefrom".$this->Image_Original_Type;
        $this->Image_Original_Resource = $Image_Create_Function($Image_Path);
    }
    
    /*
     * Image_Dimensions
     * Retrieve image width and height
     */
    public function Image_Dimensions($Image_Path)
    {
        list($w, $h) = getimagesize($Image_Path);
        $this->Image_Output_Width = $this->Image_Original_Width = $w;
        $this->Image_Output_Height = $this->Image_Original_Height = $h;
    }
    
    /*
     * Image_Type
     * Determine image type
     */
    public function Image_Type($Image_Path)
    {
        // First attempt, using the file extension
        $extension = strtolower(end(explode('.', $Image_Path)));
        switch($extension){
            case 'gif':
                $this->Image_Original_Type = 'gif';
            break;
            case 'png':
                $this->Image_Original_Type = 'png';
            break;
            case 'bmp':
                $this->Image_Original_Type = 'bmp';
            break;
            case 'jpg':
            case 'jpeg':
                $this->Image_Original_Type = 'jpeg';
            break;
            default:
            
                // Second attempt, determine type from resource
                
                if($img = imagecreatefromjpeg($Image_Path)){
                    $this->Image_Original_Type = 'jpeg';
                    $this->Image_Original_Resource = $img;
                }
                elseif($img = imagecreatefrompng($Image_Path)){
                    $this->Image_Original_Type = 'png';
                    $this->Image_Original_Resource = $img;
                }
                elseif($img = imagecreatefromgif($Image_Path)){
                    $this->Image_Original_Type = 'gif';
                    $this->Image_Original_Resource = $img;
                }
                else{
                    $this->Image_Original_Type = false;
                }
        }
    }
    
    /*
     * Image_Scale
     * Scale an image (down) proportionately to fit within $Image_Max_Width and $Image_Max_Height
     */
    public function Image_Scale($Image_Max_Width, $Image_Max_Height)
    {
        $this->Image_Scaled_Scale = min(($Image_Max_Width / $this->Image_Output_Width), ($Image_Max_Height / $this->Image_Output_Height));
        if($this->Image_Scaled_Scale < 1)
        {
            $this->Image_Scaled_Width = round($this->Image_Scaled_Scale * $this->Image_Output_Width);
            $this->Image_Scaled_Height = round($this->Image_Scaled_Scale * $this->Image_Output_Height);
            
            $this->Image_Scaled_Resource = imagecreatetruecolor($this->Image_Scaled_Width, $this->Image_Scaled_Height);
            imagecopyresampled(
                $this->Image_Scaled_Resource, $this->Image_Output_Resource,
                0, 0,
                0, 0,
                $this->Image_Scaled_Width, $this->Image_Scaled_Height,
                $this->Image_Output_Width, $this->Image_Output_Height
            );
            $this->Image_Output_Resource = $this->Image_Scaled_Resource;
            $this->Image_Output_Width = $this->Image_Scaled_Width;
            $this->Image_Output_Height = $this->Image_Scaled_Height;
        }
    }
    
    /*
     * Image_Crop
     * Crop an image horizontaly from pixel $x to $w and verticaly from $y to $h
     */
    public function Image_Crop($x, $y, $w, $h)
    {
        $this->Image_Cropped_Resource = imagecreatetruecolor($w, $h);
        imagecopyresampled(
            $this->Image_Cropped_Resource, $this->Image_Output_Resource,
            0, 0,
            $x, $y,
            $w, $h,
            $w, $h
        );
        $this->Image_Output_Resource = $this->Image_Cropped_Resource;
        $this->Image_Output_Width = $this->Image_Cropped_Width = $w;
        $this->Image_Output_Height = $this->Image_Cropped_Height = $h;
    }
    
    /*
     * Image_Square
     * Crop an image to square, optionaly resize to $wh x $wh
     * If portrait, chop top and bottom. If landscape, chop left and right
     */
    public function Image_Square($owh = false)
    {
        $wh = min(imagesx($this->Image_Output_Resource), imagesy($this->Image_Output_Resource));
        //$wh = ($wh) ? $wh : min(imagesx($this->Image_Output_Resource), imagesy($this->Image_Output_Resource));
        $x = floor((imagesx($this->Image_Output_Resource) - $wh) / 2);
        $y = floor((imagesy($this->Image_Output_Resource) - $wh) / 2);
        $this->Image_Crop($x, $y, $wh, $wh);
        
        if($owh && ($owh < $wh)){
            $this->Image_Scale($owh, $owh);
        }
    }
    
    /*
     * Image_Validate_Type
     * Check for valid format (supported by this class)
     */
    public function Image_Validate_Type($Image_Type)
    {
        switch($Image_Type){
            case 'gif':
            case 'png':
            case 'jpeg':
                return true;
            break;
            
            // By default all types not listed above are not supported
            default:
                return false;
        }
    }
    
    /*
     * Image_Output_Browser
     * Output an image to a web browser
     */
    public function Image_Output_Browser($type = false)
    {
        //Check for valid output format / type
        $type = ($this->Image_Validate_Type($type)) ? $type : $this->Image_Output_Type;
        
        //Use GD ImageOutput function (imagepng, imagejpeg, imagegif)
        $Image_Output_Function = "image".$type;
        
        //Send the picture to the browser
        header("Content-type: image/$type");
        $Image_Output_Function($this->Image_Output_Resource);
    }
    
    /*
     * Display_Image_Attributes
     * Debug function. Test that the class is working
     */
    public function Display_Image_Attributes()
    {
        echo $this->Image_Original_Path."<br />\n";
        echo $this->Image_Original_Width."<br />\n";
        echo $this->Image_Original_Height."<br />\n";
        echo $this->Image_Original_Type."<br />\n";
        echo $this->Image_Original_Attr."<br />\n";
    }

}
?>
Return current item: THINi Reports