image.php (8111B)
1 <?php 2 /** 3 * @package OpenCart 4 * @author Daniel Kerr 5 * @copyright Copyright (c) 2005 - 2017, OpenCart, Ltd. (https://www.opencart.com/) 6 * @license https://opensource.org/licenses/GPL-3.0 7 * @link https://www.opencart.com 8 */ 9 10 /** 11 * Image class 12 */ 13 class Image { 14 private $file; 15 private $image; 16 private $width; 17 private $height; 18 private $bits; 19 private $mime; 20 21 /** 22 * Constructor 23 * 24 * @param string $file 25 * 26 */ 27 public function __construct($file) { 28 if (!extension_loaded('gd')) { 29 exit('Error: PHP GD is not installed!'); 30 } 31 32 if (file_exists($file)) { 33 $this->file = $file; 34 35 $info = getimagesize($file); 36 37 $this->width = $info[0]; 38 $this->height = $info[1]; 39 $this->bits = isset($info['bits']) ? $info['bits'] : ''; 40 $this->mime = isset($info['mime']) ? $info['mime'] : ''; 41 42 if ($this->mime == 'image/gif') { 43 $this->image = imagecreatefromgif($file); 44 } elseif ($this->mime == 'image/png') { 45 $this->image = imagecreatefrompng($file); 46 } elseif ($this->mime == 'image/jpeg') { 47 $this->image = imagecreatefromjpeg($file); 48 } 49 } else { 50 exit('Error: Could not load image ' . $file . '!'); 51 } 52 } 53 54 /** 55 * 56 * 57 * @return string 58 */ 59 public function getFile() { 60 return $this->file; 61 } 62 63 /** 64 * 65 * 66 * @return array 67 */ 68 public function getImage() { 69 return $this->image; 70 } 71 72 /** 73 * 74 * 75 * @return string 76 */ 77 public function getWidth() { 78 return $this->width; 79 } 80 81 /** 82 * 83 * 84 * @return string 85 */ 86 public function getHeight() { 87 return $this->height; 88 } 89 90 /** 91 * 92 * 93 * @return string 94 */ 95 public function getBits() { 96 return $this->bits; 97 } 98 99 /** 100 * 101 * 102 * @return string 103 */ 104 public function getMime() { 105 return $this->mime; 106 } 107 108 /** 109 * 110 * 111 * @param string $file 112 * @param int $quality 113 */ 114 public function save($file, $quality = 90) { 115 $info = pathinfo($file); 116 117 $extension = strtolower($info['extension']); 118 119 if (is_resource($this->image)) { 120 if ($extension == 'jpeg' || $extension == 'jpg') { 121 imagejpeg($this->image, $file, $quality); 122 } elseif ($extension == 'png') { 123 imagepng($this->image, $file); 124 } elseif ($extension == 'gif') { 125 imagegif($this->image, $file); 126 } 127 128 imagedestroy($this->image); 129 } 130 } 131 132 /** 133 * 134 * 135 * @param int $width 136 * @param int $height 137 * @param string $default 138 */ 139 public function resize($width = 0, $height = 0, $default = '') { 140 if (!$this->width || !$this->height) { 141 return; 142 } 143 144 $xpos = 0; 145 $ypos = 0; 146 $scale = 1; 147 148 $scale_w = $width / $this->width; 149 $scale_h = $height / $this->height; 150 151 if ($default == 'w') { 152 $scale = $scale_w; 153 } elseif ($default == 'h') { 154 $scale = $scale_h; 155 } else { 156 $scale = min($scale_w, $scale_h); 157 } 158 159 if ($scale == 1 && $scale_h == $scale_w && $this->mime != 'image/png') { 160 return; 161 } 162 163 $new_width = (int)($this->width * $scale); 164 $new_height = (int)($this->height * $scale); 165 $xpos = (int)(($width - $new_width) / 2); 166 $ypos = (int)(($height - $new_height) / 2); 167 168 $image_old = $this->image; 169 $this->image = imagecreatetruecolor($width, $height); 170 171 if ($this->mime == 'image/png') { 172 imagealphablending($this->image, false); 173 imagesavealpha($this->image, true); 174 $background = imagecolorallocatealpha($this->image, 255, 255, 255, 127); 175 imagecolortransparent($this->image, $background); 176 } else { 177 $background = imagecolorallocate($this->image, 255, 255, 255); 178 } 179 180 imagefilledrectangle($this->image, 0, 0, $width, $height, $background); 181 182 imagecopyresampled($this->image, $image_old, $xpos, $ypos, 0, 0, $new_width, $new_height, $this->width, $this->height); 183 imagedestroy($image_old); 184 185 $this->width = $width; 186 $this->height = $height; 187 } 188 189 /** 190 * 191 * 192 * @param string $watermark 193 * @param string $position 194 */ 195 public function watermark($watermark, $position = 'bottomright') { 196 switch($position) { 197 case 'topleft': 198 $watermark_pos_x = 0; 199 $watermark_pos_y = 0; 200 break; 201 case 'topcenter': 202 $watermark_pos_x = intval(($this->width - $watermark->getWidth()) / 2); 203 $watermark_pos_y = 0; 204 break; 205 case 'topright': 206 $watermark_pos_x = $this->width - $watermark->getWidth(); 207 $watermark_pos_y = 0; 208 break; 209 case 'middleleft': 210 $watermark_pos_x = 0; 211 $watermark_pos_y = intval(($this->height - $watermark->getHeight()) / 2); 212 break; 213 case 'middlecenter': 214 $watermark_pos_x = intval(($this->width - $watermark->getWidth()) / 2); 215 $watermark_pos_y = intval(($this->height - $watermark->getHeight()) / 2); 216 break; 217 case 'middleright': 218 $watermark_pos_x = $this->width - $watermark->getWidth(); 219 $watermark_pos_y = intval(($this->height - $watermark->getHeight()) / 2); 220 break; 221 case 'bottomleft': 222 $watermark_pos_x = 0; 223 $watermark_pos_y = $this->height - $watermark->getHeight(); 224 break; 225 case 'bottomcenter': 226 $watermark_pos_x = intval(($this->width - $watermark->getWidth()) / 2); 227 $watermark_pos_y = $this->height - $watermark->getHeight(); 228 break; 229 case 'bottomright': 230 $watermark_pos_x = $this->width - $watermark->getWidth(); 231 $watermark_pos_y = $this->height - $watermark->getHeight(); 232 break; 233 } 234 235 imagealphablending( $this->image, true ); 236 imagesavealpha( $this->image, true ); 237 imagecopy($this->image, $watermark->getImage(), $watermark_pos_x, $watermark_pos_y, 0, 0, $watermark->getWidth(), $watermark->getHeight()); 238 239 imagedestroy($watermark->getImage()); 240 } 241 242 /** 243 * 244 * 245 * @param int $top_x 246 * @param int $top_y 247 * @param int $bottom_x 248 * @param int $bottom_y 249 */ 250 public function crop($top_x, $top_y, $bottom_x, $bottom_y) { 251 $image_old = $this->image; 252 $this->image = imagecreatetruecolor($bottom_x - $top_x, $bottom_y - $top_y); 253 254 imagecopy($this->image, $image_old, 0, 0, $top_x, $top_y, $this->width, $this->height); 255 imagedestroy($image_old); 256 257 $this->width = $bottom_x - $top_x; 258 $this->height = $bottom_y - $top_y; 259 } 260 261 /** 262 * 263 * 264 * @param int $degree 265 * @param string $color 266 */ 267 public function rotate($degree, $color = 'FFFFFF') { 268 $rgb = $this->html2rgb($color); 269 270 $this->image = imagerotate($this->image, $degree, imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2])); 271 272 $this->width = imagesx($this->image); 273 $this->height = imagesy($this->image); 274 } 275 276 /** 277 * 278 * 279 */ 280 private function filter() { 281 $args = func_get_args(); 282 283 call_user_func_array('imagefilter', $args); 284 } 285 286 /** 287 * 288 * 289 * @param string $text 290 * @param int $x 291 * @param int $y 292 * @param int $size 293 * @param string $color 294 */ 295 private function text($text, $x = 0, $y = 0, $size = 5, $color = '000000') { 296 $rgb = $this->html2rgb($color); 297 298 imagestring($this->image, $size, $x, $y, $text, imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2])); 299 } 300 301 /** 302 * 303 * 304 * @param object $merge 305 * @param object $x 306 * @param object $y 307 * @param object $opacity 308 */ 309 private function merge($merge, $x = 0, $y = 0, $opacity = 100) { 310 imagecopymerge($this->image, $merge->getImage(), $x, $y, 0, 0, $merge->getWidth(), $merge->getHeight(), $opacity); 311 } 312 313 /** 314 * 315 * 316 * @param string $color 317 * 318 * @return array 319 */ 320 private function html2rgb($color) { 321 if ($color[0] == '#') { 322 $color = substr($color, 1); 323 } 324 325 if (strlen($color) == 6) { 326 list($r, $g, $b) = array($color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5]); 327 } elseif (strlen($color) == 3) { 328 list($r, $g, $b) = array($color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2]); 329 } else { 330 return false; 331 } 332 333 $r = hexdec($r); 334 $g = hexdec($g); 335 $b = hexdec($b); 336 337 return array($r, $g, $b); 338 } 339 }