1<?php 2/* 3 * Copyright Intermesh 4 * 5 * This file is part of Group-Office. You should have received a copy of the 6 * Group-Office license along with Group-Office. See the file /LICENSE.TXT 7 * 8 * If you have questions write an e-mail to info@intermesh.nl 9 * 10 */ 11 12/** 13 * This class has functions that handle dates and takes the user's date 14 * preferences into account. 15 * 16 * @copyright Copyright Intermesh BV. 17 * @version $Id: Image.php 19784 2016-01-26 13:56:16Z michaelhart86 $ 18 * @package GO.base.util 19 */ 20 21 22namespace GO\Base\Util; 23 24 25class Image { 26 27 private $original_image; 28 private $resized_image; 29 private $image_type; 30 public $load_success; 31 private $_original_filename; 32 33 public function __construct($filename=false) { 34 if ($filename) 35 $this->load_success=$this->load($filename); 36 } 37 38 /** 39 * See IMAGETYPE GD PHP contants 40 * 41 * @return int 42 */ 43 public function getImageType(){ 44 return $this->image_type; 45 } 46 47 /** 48 * Load an image file 49 * 50 * @param StringHelper $filename 51 * @return boolean 52 */ 53 public function load($filename) { 54 55 if(!function_exists("imagecreatefromjpeg")){ 56 trigger_error("Can't resize image because the PHP GD extension is not installed", E_USER_WARNING); 57 return false; 58 } 59 60 $image_info = getimagesize($filename); 61 $this->image_type = $image_info[2]; 62 63 $this->_original_filename=$filename; 64 65 if ($this->image_type == IMAGETYPE_JPEG) { 66 $this->original_image = imagecreatefromjpeg($filename); 67 } elseif ($this->image_type == IMAGETYPE_GIF) { 68 $this->original_image = imagecreatefromgif($filename); 69 } elseif ($this->image_type == IMAGETYPE_PNG) { 70 $this->original_image = imagecreatefrompng($filename); 71 } else { 72 return false; 73 } 74 return true; 75 } 76 77 /** 78 * Set a transparent background for GIF or PNG images 79 */ 80 private function transperancy() { 81 if ($this->image_type == IMAGETYPE_GIF || $this->image_type == IMAGETYPE_PNG) { 82 $trnprt_indx = imagecolortransparent($this->original_image); 83 $palletsize = imagecolorstotal($this->original_image); 84 // If we have a specific transparent color 85 if ($trnprt_indx >= 0 && $trnprt_indx < $palletsize) { 86 87 // Get the original image's transparent color's RGB values 88 $trnprt_color = imagecolorsforindex($this->original_image, $trnprt_indx); 89 90 // Allocate the same color in the new image resource 91 $trnprt_indx = imagecolorallocate($this->resized_image, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']); 92 93 // Completely fill the background of the new image with allocated color. 94 imagefill($this->resized_image, 0, 0, $trnprt_indx); 95 96 // Set the background color for new image to transparent 97 imagecolortransparent($this->resized_image, $trnprt_indx); 98 } 99 // Always make a transparent background color for PNGs that don't have one allocated already 100 elseif ($this->image_type == IMAGETYPE_PNG) { 101 102 // Turn off transparency blending (temporarily) 103 imagealphablending($this->resized_image, false); 104 105 // Create a new transparent color for image 106 $color = imagecolorallocatealpha($this->resized_image, 0, 0, 0, 127); 107 108 // Completely fill the background of the new image with allocated color. 109 imagefill($this->resized_image, 0, 0, $color); 110 111 // Restore transparency blending 112 imagesavealpha($this->resized_image, true); 113 } 114 } 115 } 116 117 /** 118 * Output image to browser 119 * 120 * @param int $image_type 121 */ 122 public function output($image_type=false) { 123 if (!$image_type) 124 $image_type = $this->image_type; 125 126 if ($image_type == IMAGETYPE_JPEG) { 127 imagejpeg($this->resized_image); 128 } elseif ($image_type == IMAGETYPE_GIF) { 129 imagegif($this->resized_image); 130 } elseif ($image_type == IMAGETYPE_PNG) { 131 imagepng($this->resized_image); 132 } 133 } 134 135 private function _getImage(){ 136 return isset($this->resized_image) ? $this->resized_image :$this->original_image; 137 } 138 139 /** 140 * Save the imaage to a file 141 * 142 * @param StringHelper $filename 143 * @param int $image_type 144 * @param int $compression 145 * @param oct $permissions file permissions 146 * @return boolean 147 */ 148 public function save($filename, $image_type=false, $compression=85, $permissions=null) { 149 150 if(isset($this->resized_image) || $image_type!=$this->image_type){ 151 152 if (!$image_type) 153 $image_type = $this->image_type; 154 155 $ret = false; 156 if ($image_type == IMAGETYPE_JPEG) { 157 $ret = imagejpeg($this->_getImage(), $filename, $compression); 158 } elseif ($image_type == IMAGETYPE_GIF) { 159 $ret = imagegif($this->_getImage(), $filename); 160 } elseif ($image_type == IMAGETYPE_PNG) { 161 $ret = imagepng($this->_getImage(), $filename); 162 } 163 164 if(!$ret) 165 return false; 166 }else 167 { 168 //image type and dimension unchanged. Simply copy original. 169 if(!copy($this->_original_filename, $filename)) 170 return false; 171 } 172 173 if ($permissions != null) 174 chmod($filename, $permissions); 175 176 return true; 177 178 } 179 180 /** 181 * Get the width in pixels 182 * 183 * @return int 184 */ 185 public function getWidth() { 186 return imagesx($this->original_image); 187 } 188 189 /** 190 * Get the height in pixels 191 * @return int 192 */ 193 public function getHeight() { 194 return imagesy($this->original_image); 195 } 196 197 /** 198 * Returns true if this is a landscape image 199 * 200 * @return boolean 201 */ 202 public function landscape() { 203 return $this->getWidth() > $this->getHeight(); 204 } 205 206 public function resizeToHeight($height) { 207 $ratio = $height / $this->getHeight(); 208 $width = $this->getWidth() * $ratio; 209 $this->resize($width, $height); 210 } 211 212 /** 213 * Resize to given width keeping aspect ration 214 * 215 * @param int $width 216 */ 217 public function resizeToWidth($width) { 218 $ratio = $width / $this->getWidth(); 219 $height = $this->getheight() * $ratio; 220 $this->resize($width, $height); 221 } 222 223 /** 224 * Scale image to given scale factor 225 * 226 * @param int $scale eg. 0.5 227 */ 228 public function scale($scale) { 229 $width = $this->getWidth() * $scale / 100; 230 $height = $this->getheight() * $scale / 100; 231 $this->resize($width, $height); 232 } 233 234 /** 235 * Resize image 236 * 237 * @param int $width 238 * @param int $height 239 */ 240 public function resize($width, $height) { 241 $current_width = $this->getWidth(); 242 $current_height = $this->getHeight(); 243 244 $this->resized_image = imagecreatetruecolor($width, $height); 245 246 $this->transperancy(); 247 248 imagecopyresampled($this->resized_image, $this->original_image, 0, 0, 0, 0, $width, $height, $current_width, $current_height); 249 } 250 251 /** 252 * Resize the image to fit a box while keeping aspect ratio. 253 * 254 * @param int $width 255 * @param int $height 256 * @param boolean $enlarge Enlarge image if it's smaller then the box 257 */ 258 public function fitBox($width, $height, $enlarge=false){ 259 if($this->landscape()){ 260 261 if($width<$this->getWidth() || $enlarge) 262 $this->resizeToWidth($width); 263 }else 264 { 265 if($height<$this->getHeight() || $enlarge) 266 $this->resizeToHeight($height); 267 } 268 } 269 270 /** 271 * Zoom th image to fir the given height and width. Image aspect ratio is used 272 * but if the image goes out of bounds then crop these parts. 273 * 274 * @param int $thumbnail_width 275 * @param int $thumbnail_height 276 */ 277 public function zoomcrop($thumbnail_width, $thumbnail_height) { //$imgSrc is a FILE - Returns an image resource. 278 $width_orig = $this->getWidth(); 279 $height_orig = $this->getHeight(); 280 281 //getting the image dimensions 282 $ratio_orig = $width_orig / $height_orig; 283 284 if ($thumbnail_width / $thumbnail_height > $ratio_orig) { 285 $new_height = $thumbnail_width / $ratio_orig; 286 $new_width = $thumbnail_width; 287 } else { 288 $new_width = $thumbnail_height * $ratio_orig; 289 $new_height = $thumbnail_height; 290 } 291 292 $this->resized_image = imagecreatetruecolor($thumbnail_width, $thumbnail_height); 293 294 $x = ($new_width - $thumbnail_width) / -2; 295 $y = ($new_height - $thumbnail_height) / -2; 296 297 $this->transperancy(); 298 imagecopyresampled($this->resized_image, $this->original_image, $x, $y, 0, 0, $new_width, $new_height, $width_orig, $height_orig); 299 } 300 301} 302