Mercurial > judyates
view e2gallerypro/e2upload/Backend/Image.php @ 24:d30806958168 judyates
remove quotes
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 19 Jul 2014 14:35:00 -0400 |
parents | 3f6b44aa6b35 |
children |
line wrap: on
line source
1 <?php2 /**3 * Styx::Image - Provides an Interface to the GD-Library for image manipulation4 *5 * @package Styx6 * @subpackage Utility7 *8 * @license MIT-style License9 * @author Christoph Pojer <christoph.pojer@gmail.com>10 *11 * @link http://www.bin-co.com/php/scripts/classes/gd_image/ Based on work by "Binny V A"12 */14 class Image {15 /**16 * The path to the image file17 *18 * @var string19 */20 private $file;21 /**22 * The image resource23 *24 * @var resource25 */26 private $image;27 /**28 * Metadata regarding the image29 *30 * @var array31 */32 private $meta;34 /**35 * @param string $file The path to the image file36 */37 public function __construct($file){38 $file = realpath($file);39 if(!file_exists($file))40 return;42 $this->file = $file;43 $img = getimagesize($file);45 $this->meta = array(46 'width' => $img[0],47 'height' => $img[1],48 'mime' => $img['mime'],49 'ext' => end(explode('/', $img['mime'])),50 );51 if($this->meta['ext']=='jpg')52 $this->meta['ext'] = 'jpeg';54 if(!in_array($this->meta['ext'], array('gif', 'png', 'jpeg')))55 return;57 if(in_array($this->meta['ext'], array('gif', 'png'))){58 $this->image = $this->create();60 $fn = 'imagecreatefrom'.$this->meta['ext'];61 $original = $fn($file);62 imagecopyresampled($this->image, $original, 0, 0, 0, 0, $this->meta['width'], $this->meta['height'], $this->meta['width'], $this->meta['height']);63 }else{64 $this->image = imagecreatefromjpeg($file);65 }66 }68 public function __destruct(){69 if(!empty($this->image)) imagedestroy($this->image);70 }72 /**73 * Returns the size of the image74 *75 * @return array76 */77 public function getSize(){78 return array(79 'width' => $this->meta['width'],80 'height' => $this->meta['height'],81 );82 }84 /**85 * Creates a new, empty image with the desired size86 *87 * @param int $x88 * @param int $y89 * @param string $ext90 * @return resource91 */92 private function create($x = null, $y = null, $ext = null){93 if(!$x) $x = $this->meta['width'];94 if(!$y) $y = $this->meta['height'];96 $image = imagecreatetruecolor($x, $y);97 if(!$ext) $ext = $this->meta['ext'];98 if($ext=='png'){99 imagealphablending($image, false);100 imagefilledrectangle($image, 0, 0, $x, $y, imagecolorallocatealpha($image, 0, 0, 0, 127));101 }103 return $image;104 }106 /**107 * Replaces the image resource with the given parameter108 *109 * @param resource $new110 */111 private function set($new){112 imagedestroy($this->image);113 $this->image = $new;115 $this->meta['width'] = imagesx($this->image);116 $this->meta['height'] = imagesy($this->image);117 }119 /**120 * Returns the path to the image file121 *122 * @return string123 */124 public function getPathname(){125 return $this->file;126 }128 /**129 * Rotates the image by the given angle130 *131 * @param int $angle132 * @param array $bgcolor An indexed array with red/green/blue/alpha values133 * @return Image134 */135 public function rotate($angle, $bgcolor = null){136 if(empty($this->image) || !$angle || $angle>=360) return $this;138 $this->set(imagerotate($this->image, $angle, is_array($bgcolor) ? imagecolorallocatealpha($this->image, $bgcolor[0], $bgcolor[1], $bgcolor[2], !empty($bgcolor[3]) ? $bgcolor[3] : null) : $bgcolor));140 return $this;141 }143 /**144 * Resizes the image to the given size, automatically calculates145 * the new ratio if parameter {@link $ratio} is set to true146 *147 * @param int $x148 * @param int $y149 * @param bool $ratio150 * @return Image151 */152 public function resize($x = null, $y = null, $ratio = true){153 if(empty($this->image) || (!$x && !$y)) return $this;155 if(!$y) $y = $ratio ? $this->meta['height']*$x/$this->meta['width'] : $this->meta['height'];156 if(!$x) $x = $ratio ? $this->meta['width']*$y/$this->meta['height'] : $this->meta['width'];158 $new = $this->create($x, $y);159 imagecopyresampled($new, $this->image, 0, 0, 0, 0, $x, $y, $this->meta['width'], $this->meta['height']);160 $this->set($new);162 return $this;163 }165 /**166 * Crops the image. The values are given like margin/padding values in css167 *168 * <b>Example</b>169 * <ul>170 * <li>crop(10) - Crops by 10px on all sides</li>171 * <li>crop(10, 5) - Crops by 10px on top and bottom and by 5px on left and right sides</li>172 * <li>crop(10, 5, 5) - Crops by 10px on top and by 5px on left, right and bottom sides</li>173 * <li>crop(10, 5, 3, 2) - Crops by 10px on top, 5px by right, 3px by bottom and 2px by left sides</li>174 * </ul>175 *176 * @param int $top177 * @param int $right178 * @param int $bottom179 * @param int $left180 * @return Image181 */182 public function crop($top, $right = null, $bottom = null, $left = null){183 if(empty($this->image)) return $this;185 if(!is_numeric($right) && !is_numeric($bottom) && !is_numeric($left))186 $right = $bottom = $left = $top;188 if(!is_numeric($bottom) && !is_numeric($left)){189 $bottom = $top;190 $left = $right;191 }193 if(!is_numeric($left))194 $left = $right;196 $x = $this->meta['width']-$left-$right;197 $y = $this->meta['height']-$top-$bottom;199 if($x<0 || $y<0) return $this;201 $new = $this->create($x, $y);202 imagecopy($new, $this->image, 0, 0, $left, $top, $x, $y);203 $this->set($new);205 return $this;206 }208 /**209 * Flips the image horizontally or vertically. To Flip both just use ->rotate(180)210 *211 * @see Image::rotate()212 * @param string $type Either horizontal or vertical213 * @return Image214 */215 public function flip($type){216 if(empty($this->image) || !in_array($type, array('horizontal', 'vertical'))) return $this;218 $new = $this->create();220 if($type=='horizontal')221 for($x=0;$x<$this->meta['width'];$x++)222 imagecopy($new, $this->image, $this->meta['width']-$x-1, 0, $x, 0, 1, $this->meta['height']);223 elseif($type=='vertical')224 for($y=0;$y<$this->meta['height'];$y++)225 imagecopy($new, $this->image, 0, $this->meta['height']-$y-1, 0, $y, $this->meta['width'], 1);227 $this->set($new);229 return $this;230 }232 /**233 * Stores the image in the desired directory or outputs it234 *235 * @param string $ext236 * @param string $file237 */238 private function process($ext = null, $file = null){239 if(!$ext) $ext = $this->meta['ext'];241 if($ext=='png') imagesavealpha($this->image, true);242 $fn = 'image'.$ext;243 $fn($this->image, $file);245 // If there is a new filename change the internal name too246 if($file) $this->file = $file;247 }249 /**250 * Saves the image to the given path251 *252 * @param string $file Leave empty to replace the original file253 * @return Image254 */255 public function save($file = null){256 if(empty($this->image)) return $this;258 if(!$file) $file = $this->file;260 $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));261 if(!$ext){262 $file .= '.'.$this->meta['ext'];263 $ext = $this->meta['ext'];264 }266 if($ext=='jpg') $ext = 'jpeg';268 if(!in_array($ext, array('png', 'jpeg', 'gif')))269 return $this;271 $this->process($ext, $file);273 return $this;274 }276 /**277 * Outputs the manipulated image278 *279 * @return Image280 */281 public function show(){282 if(empty($this->image)) return $this;284 header('Content-type: '.$this->meta['mime']);285 $this->process();287 return $this;288 }290 }