Changeset 1843

Show
Ignore:
Timestamp:
01/28/2008 10:53:28 AM (10 months ago)
Author:
Shadowhand
Message:

Changes to GD Image driver:

  • Saving to different format supported
  • Quality implemented, but can only be set on JPG images (due to GD limitations)
  • Quality defaults to 95%, same as IM
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/system/libraries/drivers/Image/GD.php

    r1839 r1843  
    22 
    33class Image_GD_Driver extends Image_Driver { 
    4  
    5         // GD image create function name 
    6         protected $imagecreate; 
    7  
    8         // GD image save function name 
    9         protected $imagesave; 
    104 
    115        // A transparent PNG as a string 
     
    2115        } 
    2216 
    23         protected function set_functions($type) 
    24         { 
    25                 static $imagecreate; 
    26                 static $imagesave; 
    27  
    28                 if ($imagecreate === NULL) 
    29                 { 
    30                         $imagecreate = array 
    31                         ( 
    32                                 1 => 'imagecreatefromgif', 
    33                                 2 => 'imagecreatefromjpeg', 
    34                                 3 => 'imagecreatefrompng' 
    35                         ); 
    36  
    37                         $imagesave = array 
    38                         ( 
    39                                 1 => 'imagegif', 
    40                                 2 => 'imagejpeg', 
    41                                 3 => 'imagepng' 
    42                         ); 
    43                 } 
    44  
    45                 // Set the create function 
    46                 isset($imagecreate[$type]) 
    47                         and function_exists($imagecreate[$type]) 
    48                         and $this->imagecreate = $imagecreate[$type]; 
    49  
    50                 // Set the save function 
    51                 isset($imagesave[$type]) 
    52                         and function_exists($imagesave[$type]) 
    53                         and $this->imagesave = $imagesave[$type]; 
    54  
    55                 return ! (empty($this->imagecreate) OR empty($this->imagesave)); 
    56         } 
    57  
    5817        public function process($image, $actions, $dir, $file) 
    5918        { 
    60                 // Make sure the image type is supported 
    61                 if ( ! $this->set_functions($image['type'])) 
     19                switch ($image['type']) 
     20                { 
     21                        case IMAGETYPE_JPEG: 
     22                                $create = 'imagecreatefromjpeg'; 
     23                        break; 
     24                        case IMAGETYPE_GIF: 
     25                                $create = 'imagecreatefromgif'; 
     26                        break; 
     27                        case IMAGETYPE_PNG: 
     28                                $create = 'imagecreatefrompng'; 
     29                        break; 
     30                } 
     31 
     32                switch (substr($file, strrpos($file, '.') + 1)) 
     33                { 
     34                        case 'jpg': 
     35                        case 'jpeg': 
     36                                $save = 'imagejpeg'; 
     37                        break; 
     38                        case 'gif': 
     39                                $save = 'imagegif'; 
     40                        break; 
     41                        case 'png': 
     42                                $save = 'imagepng'; 
     43                        break; 
     44                } 
     45 
     46                // Make sure the image type is supported for import 
     47                if (empty($create) OR ! function_exists($create)) 
    6248                        throw new Kohana_Exception('image.type_not_allowed', $image['file']); 
     49 
     50                // Make sure the image type is supported for saving 
     51                if (empty($save) OR ! function_exists($save)) 
     52                        throw new Kohana_Exception('image.type_not_allowed', $dir.$file); 
    6353 
    6454                // Load the image 
    6555                $this->image = $image; 
    6656 
    67                 // Image create function alias 
    68                 $create = $this->imagecreate; 
    69  
    7057                // Create the GD image resource 
    7158                $this->tmp_image = $create($image['file']); 
    7259 
     60                // Get the quality setting from the actions 
     61                $quality = arr::remove('quality', $actions); 
     62 
    7363                if ($status = $this->execute($actions)) 
    7464                { 
    75                         // Image save function alias 
    76                         $save = $this->imagesave; 
    77  
    78                         // Save the image to set the status 
    79                         $status = $save($this->tmp_image, $dir.$file); 
     65                        // Prevent the alpha from being lost 
     66                        imagealphablending($this->tmp_image, TRUE); 
     67                        imagesavealpha($this->tmp_image, TRUE); 
     68 
     69                        switch ($save) 
     70                        { 
     71                                case 'imagejpeg': 
     72                                        if ($quality === NULL) 
     73                                        { 
     74                                                // Default quality is 95% 
     75                                                $quality = 95; 
     76                                        } 
     77                                        else 
     78                                        { 
     79                                                // Make sure the quality is in range 
     80                                                $quality = max(1, min($quality, 100)); 
     81                                        } 
     82                                break; 
     83                                case 'imagegif': 
     84                                        // Remove the quality setting, GIF doesn't use it 
     85                                        unset($quality); 
     86                                break; 
     87                                case 'imagepng': 
     88                                        // Always use a compression level of 9 for PNGs. 
     89                                        // This does not affect quality, it only increases the level of compression! 
     90                                        $quality = 9; 
     91                                break; 
     92                        } 
     93 
     94                        // Set the status to the save return value, saving with the quality reques 
     95                        $status = isset($quality) ? $save($this->tmp_image, $dir.$file, $quality) : $save($this->tmp_image, $dir.$file); 
    8096 
    8197                        // Destroy the temporary image 
     
    100116 
    101117                // Create the temporary image to copy to 
    102                 $tmp = $this->imagecreatetransparent($properties['width'], $properties['height']); 
     118                $img = $this->imagecreatetransparent($properties['width'], $properties['height']); 
    103119 
    104120                // Execute the crop 
    105                 imagecopyresampled($tmp, $this->tmp_image, 0, 0, $properties['left'], $properties['top'], $width, $height, $width, $height); 
    106  
    107                 // Destroy the temporary image 
    108                 imagedestroy($this->tmp_image); 
    109  
    110                 // Set the temporary image to this image 
    111                 $this->tmp_image = $tmp; 
    112  
    113                 return TRUE; 
     121                if ($status = imagecopyresampled($img, $this->tmp_image, 0, 0, $properties['left'], $properties['top'], $width, $height, $width, $height)) 
     122                { 
     123                        // Swap the new image for the old one 
     124                        imagedestroy($this->tmp_image); 
     125                        $this->tmp_image = $img; 
     126                } 
     127 
     128                return $status; 
    114129        } 
    115130