diff e2gallerypro/e2upload/Backend/Image.php @ 3:3f6b44aa6b35 judyates

[svn r4] added ability to buy stuff, from a Prints page, but it doesn't work well with the css, and it also has not been fitted into the perl make system.
author rlm
date Mon, 22 Feb 2010 08:02:39 -0500
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/e2gallerypro/e2upload/Backend/Image.php	Mon Feb 22 08:02:39 2010 -0500
     1.3 @@ -0,0 +1,290 @@
     1.4 +<?php
     1.5 +/**
     1.6 + * Styx::Image - Provides an Interface to the GD-Library for image manipulation
     1.7 + *
     1.8 + * @package Styx
     1.9 + * @subpackage Utility
    1.10 + *
    1.11 + * @license MIT-style License
    1.12 + * @author Christoph Pojer <christoph.pojer@gmail.com>
    1.13 + *
    1.14 + * @link http://www.bin-co.com/php/scripts/classes/gd_image/ Based on work by "Binny V A"
    1.15 + */
    1.16 +
    1.17 +class Image {
    1.18 +	/**
    1.19 +	 * The path to the image file
    1.20 +	 *
    1.21 +	 * @var string
    1.22 +	 */
    1.23 +	private $file;
    1.24 +	/**
    1.25 +	 * The image resource
    1.26 +	 *
    1.27 +	 * @var resource
    1.28 +	 */
    1.29 +	private $image;
    1.30 +	/**
    1.31 +	 * Metadata regarding the image
    1.32 +	 *
    1.33 +	 * @var array
    1.34 +	 */
    1.35 +	private $meta;
    1.36 +	
    1.37 +	/**
    1.38 +	 * @param string $file The path to the image file
    1.39 +	 */
    1.40 +	public function __construct($file){
    1.41 +		$file = realpath($file);
    1.42 +		if(!file_exists($file))
    1.43 +			return;
    1.44 +		
    1.45 +		$this->file = $file;
    1.46 +		$img = getimagesize($file);
    1.47 +
    1.48 +		$this->meta = array(
    1.49 +			'width' => $img[0],
    1.50 +			'height' => $img[1],
    1.51 +			'mime' => $img['mime'],
    1.52 +			'ext' => end(explode('/', $img['mime'])),
    1.53 +		);
    1.54 +		if($this->meta['ext']=='jpg')
    1.55 +			$this->meta['ext'] = 'jpeg';
    1.56 +		
    1.57 +		if(!in_array($this->meta['ext'], array('gif', 'png', 'jpeg')))
    1.58 +			return;
    1.59 +		
    1.60 +		if(in_array($this->meta['ext'], array('gif', 'png'))){
    1.61 +			$this->image = $this->create();
    1.62 +			
    1.63 +			$fn = 'imagecreatefrom'.$this->meta['ext'];
    1.64 +			$original = $fn($file);
    1.65 +			imagecopyresampled($this->image, $original, 0, 0, 0, 0, $this->meta['width'], $this->meta['height'], $this->meta['width'], $this->meta['height']);
    1.66 +		}else{
    1.67 +			$this->image = imagecreatefromjpeg($file);
    1.68 +		}
    1.69 +	}
    1.70 +	
    1.71 +	public function __destruct(){
    1.72 +		if(!empty($this->image)) imagedestroy($this->image);
    1.73 +	}
    1.74 +	
    1.75 +	/**
    1.76 +	 * Returns the size of the image
    1.77 +	 *
    1.78 +	 * @return array
    1.79 +	 */
    1.80 +	public function getSize(){
    1.81 +		return array(
    1.82 +			'width' => $this->meta['width'],
    1.83 +			'height' => $this->meta['height'],
    1.84 +		);
    1.85 +	}
    1.86 +	
    1.87 +	/**
    1.88 +	 * Creates a new, empty image with the desired size
    1.89 +	 *
    1.90 +	 * @param int $x
    1.91 +	 * @param int $y
    1.92 +	 * @param string $ext
    1.93 +	 * @return resource
    1.94 +	 */
    1.95 +	private function create($x = null, $y = null, $ext = null){
    1.96 +		if(!$x) $x = $this->meta['width'];
    1.97 +		if(!$y) $y = $this->meta['height'];
    1.98 +		
    1.99 +		$image = imagecreatetruecolor($x, $y);
   1.100 +		if(!$ext) $ext = $this->meta['ext'];
   1.101 +		if($ext=='png'){
   1.102 +			imagealphablending($image, false);
   1.103 +			imagefilledrectangle($image, 0, 0, $x, $y, imagecolorallocatealpha($image, 0, 0, 0, 127));
   1.104 +		}
   1.105 +		
   1.106 +		return $image;
   1.107 +	}
   1.108 +	
   1.109 +	/**
   1.110 +	 * Replaces the image resource with the given parameter
   1.111 +	 *
   1.112 +	 * @param resource $new
   1.113 +	 */
   1.114 +	private function set($new){
   1.115 +		imagedestroy($this->image);
   1.116 +		$this->image = $new;
   1.117 +		
   1.118 +		$this->meta['width'] = imagesx($this->image);
   1.119 +		$this->meta['height'] = imagesy($this->image);
   1.120 +	}
   1.121 +	
   1.122 +	/**
   1.123 +	 * Returns the path to the image file
   1.124 +	 *
   1.125 +	 * @return string
   1.126 +	 */
   1.127 +	public function getPathname(){
   1.128 +		return $this->file;
   1.129 +	}
   1.130 +	
   1.131 +	/**
   1.132 +	 * Rotates the image by the given angle
   1.133 +	 *
   1.134 +	 * @param int $angle
   1.135 +	 * @param array $bgcolor An indexed array with red/green/blue/alpha values
   1.136 +	 * @return Image
   1.137 +	 */
   1.138 +	public function rotate($angle, $bgcolor = null){
   1.139 +		if(empty($this->image) || !$angle || $angle>=360) return $this;
   1.140 +		
   1.141 +		$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));
   1.142 +
   1.143 +		return $this;
   1.144 +	}
   1.145 +	
   1.146 +	/**
   1.147 +	 * Resizes the image to the given size, automatically calculates
   1.148 +	 * the new ratio if parameter {@link $ratio} is set to true
   1.149 +	 *
   1.150 +	 * @param int $x
   1.151 +	 * @param int $y
   1.152 +	 * @param bool $ratio
   1.153 +	 * @return Image
   1.154 +	 */
   1.155 +	public function resize($x = null, $y = null, $ratio = true){
   1.156 +		if(empty($this->image) || (!$x && !$y)) return $this;
   1.157 +		
   1.158 +		if(!$y) $y = $ratio ? $this->meta['height']*$x/$this->meta['width'] : $this->meta['height'];
   1.159 +		if(!$x) $x = $ratio ? $this->meta['width']*$y/$this->meta['height'] : $this->meta['width'];
   1.160 +		
   1.161 +		$new = $this->create($x, $y);
   1.162 +		imagecopyresampled($new, $this->image, 0, 0, 0, 0, $x, $y, $this->meta['width'], $this->meta['height']);
   1.163 +		$this->set($new);
   1.164 +		
   1.165 +		return $this;
   1.166 +	}
   1.167 +	
   1.168 +	/**
   1.169 +	 * Crops the image. The values are given like margin/padding values in css
   1.170 +	 *
   1.171 +	 * <b>Example</b>
   1.172 +	 * <ul>
   1.173 +	 * <li>crop(10) - Crops by 10px on all sides</li>
   1.174 +	 * <li>crop(10, 5) - Crops by 10px on top and bottom and by 5px on left and right sides</li>
   1.175 +	 * <li>crop(10, 5, 5) - Crops by 10px on top and by 5px on left, right and bottom sides</li>
   1.176 +	 * <li>crop(10, 5, 3, 2) - Crops by 10px on top, 5px by right, 3px by bottom and 2px by left sides</li>
   1.177 +	 * </ul>
   1.178 +	 *
   1.179 +	 * @param int $top
   1.180 +	 * @param int $right
   1.181 +	 * @param int $bottom
   1.182 +	 * @param int $left
   1.183 +	 * @return Image
   1.184 +	 */
   1.185 +	public function crop($top, $right = null, $bottom = null, $left = null){
   1.186 +		if(empty($this->image)) return $this;
   1.187 +		
   1.188 +		if(!is_numeric($right) && !is_numeric($bottom) && !is_numeric($left))
   1.189 +			$right = $bottom = $left = $top;
   1.190 +		
   1.191 +		if(!is_numeric($bottom) && !is_numeric($left)){
   1.192 +			$bottom = $top;
   1.193 +			$left = $right;
   1.194 +		}
   1.195 +		
   1.196 +		if(!is_numeric($left))
   1.197 +			$left = $right;
   1.198 +		
   1.199 +		$x = $this->meta['width']-$left-$right;
   1.200 +		$y = $this->meta['height']-$top-$bottom;
   1.201 +
   1.202 +		if($x<0 || $y<0) return $this;
   1.203 +		
   1.204 +		$new = $this->create($x, $y);
   1.205 +		imagecopy($new, $this->image, 0, 0, $left, $top, $x, $y);
   1.206 +		$this->set($new);
   1.207 +		
   1.208 +		return $this;
   1.209 +	}
   1.210 +	
   1.211 +	/**
   1.212 +	 * Flips the image horizontally or vertically. To Flip both just use ->rotate(180)
   1.213 +	 *
   1.214 +	 * @see Image::rotate()
   1.215 +	 * @param string $type Either horizontal or vertical
   1.216 +	 * @return Image
   1.217 +	 */
   1.218 +	public function flip($type){
   1.219 +		if(empty($this->image) || !in_array($type, array('horizontal', 'vertical'))) return $this;
   1.220 +		
   1.221 +		$new = $this->create();
   1.222 +		
   1.223 +		if($type=='horizontal')
   1.224 +			for($x=0;$x<$this->meta['width'];$x++)
   1.225 +				imagecopy($new, $this->image, $this->meta['width']-$x-1, 0, $x, 0, 1, $this->meta['height']);
   1.226 +		elseif($type=='vertical')
   1.227 +			for($y=0;$y<$this->meta['height'];$y++)
   1.228 +				imagecopy($new, $this->image, 0, $this->meta['height']-$y-1, 0, $y, $this->meta['width'], 1);
   1.229 +		
   1.230 +		$this->set($new);
   1.231 +		
   1.232 +		return $this;
   1.233 +	}
   1.234 +	
   1.235 +	/**
   1.236 +	 * Stores the image in the desired directory or outputs it
   1.237 +	 *
   1.238 +	 * @param string $ext
   1.239 +	 * @param string $file
   1.240 +	 */
   1.241 +	private function process($ext = null, $file = null){
   1.242 +		if(!$ext) $ext = $this->meta['ext'];
   1.243 +		
   1.244 +		if($ext=='png') imagesavealpha($this->image, true);
   1.245 +		$fn = 'image'.$ext;
   1.246 +		$fn($this->image, $file);
   1.247 +		
   1.248 +		// If there is a new filename change the internal name too
   1.249 +		if($file) $this->file = $file;
   1.250 +	}
   1.251 +
   1.252 +	/**
   1.253 +	 * Saves the image to the given path
   1.254 +	 *
   1.255 +	 * @param string $file Leave empty to replace the original file
   1.256 +	 * @return Image
   1.257 +	 */
   1.258 +	public function save($file = null){
   1.259 +		if(empty($this->image)) return $this;
   1.260 +		
   1.261 +		if(!$file) $file = $this->file;
   1.262 +		
   1.263 +		$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
   1.264 +		if(!$ext){
   1.265 +			$file .= '.'.$this->meta['ext'];
   1.266 +			$ext = $this->meta['ext'];
   1.267 +		}
   1.268 +		
   1.269 +		if($ext=='jpg') $ext = 'jpeg';
   1.270 +		
   1.271 +		if(!in_array($ext, array('png', 'jpeg', 'gif')))
   1.272 +			return $this;
   1.273 +		
   1.274 +		$this->process($ext, $file);
   1.275 +		
   1.276 +		return $this;
   1.277 +	}
   1.278 +
   1.279 +	/**
   1.280 +	 * Outputs the manipulated image
   1.281 +	 *
   1.282 +	 * @return Image
   1.283 +	 */
   1.284 +	public function show(){
   1.285 +		if(empty($this->image)) return $this;
   1.286 +		
   1.287 +		header('Content-type: '.$this->meta['mime']);
   1.288 +		$this->process();
   1.289 +		
   1.290 +		return $this;
   1.291 +	}
   1.292 +	
   1.293 +}
   1.294 \ No newline at end of file