| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | define('IMAGE_PROCESSING_MEMORY_MAX_USAGE', 160); |
|---|
| 23 | |
|---|
| 24 | class Image { |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | private $file; |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | private $image; |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | private $meta; |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | private $dirty; |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | public function __construct($file) |
|---|
| 52 | { |
|---|
| 53 | $this->dirty = false; |
|---|
| 54 | |
|---|
| 55 | $this->meta = self::checkFileForProcessing($file); |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | if ($this->meta['fileinfo']['memory_limit'] < IMAGE_PROCESSING_MEMORY_MAX_USAGE * 1024 * 1024) |
|---|
| 59 | { |
|---|
| 60 | ini_set('memory_limit', IMAGE_PROCESSING_MEMORY_MAX_USAGE . 'M'); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | $this->file = $file; |
|---|
| 64 | |
|---|
| 65 | if($this->meta['ext'] != 'jpeg') |
|---|
| 66 | { |
|---|
| 67 | $this->image = $this->create(); |
|---|
| 68 | |
|---|
| 69 | $fn = 'imagecreatefrom'.$this->meta['ext']; |
|---|
| 70 | $original = false; |
|---|
| 71 | if (function_exists($fn)) |
|---|
| 72 | { |
|---|
| 73 | $original = @$fn($file); |
|---|
| 74 | } |
|---|
| 75 | if (!$original) throw new Exception('imagecreate_failed:' . $fn); |
|---|
| 76 | |
|---|
| 77 | if (!@imagecopyresampled($this->image, $original, 0, 0, 0, 0, $this->meta['width'], $this->meta['height'], $this->meta['width'], $this->meta['height'])) |
|---|
| 78 | throw new Exception('cvt2truecolor_failed:' . $this->meta['width'] . ' x ' . $this->meta['height']); |
|---|
| 79 | imagedestroy($original); |
|---|
| 80 | unset($original); |
|---|
| 81 | } |
|---|
| 82 | else |
|---|
| 83 | { |
|---|
| 84 | $this->image = @imagecreatefromjpeg($file); |
|---|
| 85 | if (!$this->image) throw new Exception('imagecreate_failed:imagecreatefromjpeg'); |
|---|
| 86 | } |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | public function __destruct(){ |
|---|
| 90 | if(!empty($this->image)) imagedestroy($this->image); |
|---|
| 91 | unset($this->image); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | public static function getSupportedTypes() |
|---|
| 100 | { |
|---|
| 101 | static $supported_types; |
|---|
| 102 | |
|---|
| 103 | if (empty($supported_types)) |
|---|
| 104 | { |
|---|
| 105 | $gdi = gd_info(); |
|---|
| 106 | |
|---|
| 107 | $supported_types = array(); |
|---|
| 108 | if (!empty($gdi['GIF Read Support'])) |
|---|
| 109 | $supported_types[] = 'gif'; |
|---|
| 110 | if (!empty($gdi['PNG Support'])) |
|---|
| 111 | $supported_types[] = 'png'; |
|---|
| 112 | if (!empty($gdi['JPEG Support']) || !empty($gdi['JPG Support']) ) |
|---|
| 113 | $supported_types[] = 'jpeg'; |
|---|
| 114 | if (!empty($gdi['WBMP Support'])) |
|---|
| 115 | $supported_types[] = 'wbmp'; |
|---|
| 116 | if (!empty($gdi['XPM Support'])) |
|---|
| 117 | $supported_types[] = 'xpm'; |
|---|
| 118 | if (!empty($gdi['XBM Support'])) |
|---|
| 119 | $supported_types[] = 'xbm'; |
|---|
| 120 | $supported_types[] = 'bmp'; |
|---|
| 121 | } |
|---|
| 122 | return $supported_types; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | public static function guestimateRequiredMemorySpace($file) |
|---|
| 139 | { |
|---|
| 140 | $val = trim(ini_get('memory_limit')); |
|---|
| 141 | $last = strtoupper(substr($val, -1, 1)); |
|---|
| 142 | $val = floatval($val); |
|---|
| 143 | switch($last) |
|---|
| 144 | { |
|---|
| 145 | |
|---|
| 146 | case 'G': |
|---|
| 147 | $val *= 1024.0; |
|---|
| 148 | case 'M': |
|---|
| 149 | $val *= 1024.0; |
|---|
| 150 | case 'K': |
|---|
| 151 | $val *= 1024.0; |
|---|
| 152 | break; |
|---|
| 153 | } |
|---|
| 154 | $limit = $val; |
|---|
| 155 | |
|---|
| 156 | $in_use = (function_exists('memory_get_usage') ? memory_get_usage() : 1000000 ); |
|---|
| 157 | |
|---|
| 158 | $rv = array( |
|---|
| 159 | 'memory_limit' => $limit, |
|---|
| 160 | 'mem_in_use' => $in_use, |
|---|
| 161 | 'path' => $file |
|---|
| 162 | ); |
|---|
| 163 | |
|---|
| 164 | if(file_exists($file)) |
|---|
| 165 | { |
|---|
| 166 | $raw_size = @filesize($file); |
|---|
| 167 | $rv['filesize'] = $raw_size; |
|---|
| 168 | |
|---|
| 169 | $img = @getimagesize($file, $info_ex); |
|---|
| 170 | if ($img) |
|---|
| 171 | { |
|---|
| 172 | $width = $img[0]; |
|---|
| 173 | $height = $img[1]; |
|---|
| 174 | $rv['imageinfo'] = $img; |
|---|
| 175 | $rv['imageinfo_extra'] = $info_ex; |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | $needed = 4.0 * $width * $height; |
|---|
| 182 | $needed *= 34.0 / 27.0; |
|---|
| 183 | $rv['needed'] = $needed; |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | $will_eat = $needed * 2.8; |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | if (0) $will_eat += $raw_size; |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | if (!empty($img['mime']) && $img['mime'] == 'image/jpeg') |
|---|
| 200 | { |
|---|
| 201 | $will_eat /= 2.0; |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | $rv['usage_guestimate'] = $will_eat; |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | $does_fit = ($limit - $in_use > $will_eat); |
|---|
| 208 | $rv['usage_min_advised'] = $will_eat + $in_use; |
|---|
| 209 | $rv['will_fit'] = $does_fit; |
|---|
| 210 | } |
|---|
| 211 | else |
|---|
| 212 | { |
|---|
| 213 | |
|---|
| 214 | $rv['not_an_image_file'] = true; |
|---|
| 215 | } |
|---|
| 216 | } |
|---|
| 217 | else |
|---|
| 218 | { |
|---|
| 219 | |
|---|
| 220 | $rv['not_an_image_file'] = true; |
|---|
| 221 | } |
|---|
| 222 | return $rv; |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | public static function checkFileForProcessing($file) |
|---|
| 232 | { |
|---|
| 233 | $finfo = self::guestimateRequiredMemorySpace($file); |
|---|
| 234 | if (!empty($finfo['not_an_image_file'])) |
|---|
| 235 | throw new Exception('no_imageinfo'); |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | if (!isset($finfo['imageinfo'])) |
|---|
| 239 | throw new Exception('no_imageinfo'); |
|---|
| 240 | |
|---|
| 241 | $file = $finfo['path']; |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | if ($finfo['memory_limit'] < IMAGE_PROCESSING_MEMORY_MAX_USAGE * 1024 * 1024) |
|---|
| 245 | { |
|---|
| 246 | |
|---|
| 247 | $finfo['will_fit'] = ($finfo['usage_min_advised'] < IMAGE_PROCESSING_MEMORY_MAX_USAGE * 1024 * 1024); |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | $img = $finfo['imageinfo']; |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | if (!$finfo['will_fit']) |
|---|
| 254 | throw new Exception('img_will_not_fit:' . ceil($finfo['usage_min_advised'] / 1E6) . ' MByte'); |
|---|
| 255 | |
|---|
| 256 | $explarr = explode('/', $img['mime']); |
|---|
| 257 | $ext_from_mime = end($explarr); |
|---|
| 258 | $meta = array( |
|---|
| 259 | 'width' => $img[0], |
|---|
| 260 | 'height' => $img[1], |
|---|
| 261 | 'mime' => $img['mime'], |
|---|
| 262 | 'ext' => $ext_from_mime, |
|---|
| 263 | 'fileinfo' => $finfo |
|---|
| 264 | ); |
|---|
| 265 | |
|---|
| 266 | if($meta['ext'] == 'jpg') |
|---|
| 267 | $meta['ext'] = 'jpeg'; |
|---|
| 268 | else if($meta['ext'] == 'bmp') |
|---|
| 269 | $meta['ext'] = 'bmp'; |
|---|
| 270 | else if($meta['ext'] == 'x-ms-bmp') |
|---|
| 271 | $meta['ext'] = 'bmp'; |
|---|
| 272 | if(!in_array($meta['ext'], self::getSupportedTypes())) |
|---|
| 273 | throw new Exception('unsupported_imgfmt:' . $meta['ext']); |
|---|
| 274 | |
|---|
| 275 | return $meta; |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | |
|---|
| 283 | |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | public static function calculate_resize_dimensions($orig_x, $orig_y, $x = null, $y = null, $ratio = true, $resizeWhenSmaller = false) |
|---|
| 293 | { |
|---|
| 294 | if(empty($orig_x) || empty($orig_y) || (empty($x) && empty($y))) return false; |
|---|
| 295 | |
|---|
| 296 | $xStart = $x; |
|---|
| 297 | $yStart = $y; |
|---|
| 298 | $ratioX = $orig_x / $orig_y; |
|---|
| 299 | $ratioY = $orig_y / $orig_x; |
|---|
| 300 | $ratio |= (empty($y) || empty($x)); |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | if($ratio) { |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | if(!empty($x) && ($x < $orig_x || $resizeWhenSmaller)) |
|---|
| 307 | $y = $x / $ratioX; |
|---|
| 308 | |
|---|
| 309 | elseif(!empty($y) && ($y < $orig_y || $resizeWhenSmaller)) |
|---|
| 310 | $x = $y / $ratioY; |
|---|
| 311 | else { |
|---|
| 312 | $y = $orig_y; |
|---|
| 313 | $x = $orig_x; |
|---|
| 314 | } |
|---|
| 315 | |
|---|
| 316 | |
|---|
| 317 | if((!empty($yStart) && $y > $yStart) || (!empty($xStart) && $x > $xStart)) { |
|---|
| 318 | if($y > $yStart) { |
|---|
| 319 | $y = $yStart; |
|---|
| 320 | $x = $y / $ratioY; |
|---|
| 321 | } elseif($x > $xStart) { |
|---|
| 322 | $x = $xStart; |
|---|
| 323 | $y = $x / $ratioX; |
|---|
| 324 | } |
|---|
| 325 | } |
|---|
| 326 | } |
|---|
| 327 | |
|---|
| 328 | |
|---|
| 329 | $x = round($x); |
|---|
| 330 | $y = round($y); |
|---|
| 331 | |
|---|
| 332 | |
|---|
| 333 | $rv = array( |
|---|
| 334 | 'width' => $x, |
|---|
| 335 | 'height' => $y, |
|---|
| 336 | 'must_resize' => false |
|---|
| 337 | ); |
|---|
| 338 | |
|---|
| 339 | if ($x != $orig_x || $y != $orig_y) |
|---|
| 340 | { |
|---|
| 341 | $rv['must_resize'] = true; |
|---|
| 342 | } |
|---|
| 343 | return $rv; |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | |
|---|
| 347 | |
|---|
| 348 | |
|---|
| 349 | |
|---|
| 350 | |
|---|
| 351 | public function getSize(){ |
|---|
| 352 | return array( |
|---|
| 353 | 'width' => $this->meta['width'], |
|---|
| 354 | 'height' => $this->meta['height'], |
|---|
| 355 | ); |
|---|
| 356 | } |
|---|
| 357 | |
|---|
| 358 | |
|---|
| 359 | |
|---|
| 360 | |
|---|
| 361 | |
|---|
| 362 | |
|---|
| 363 | |
|---|
| 364 | public function getMetaInfo(){ |
|---|
| 365 | return array_merge(array(), (is_array($this->meta) ? $this->meta : array())); |
|---|
| 366 | } |
|---|
| 367 | |
|---|
| 368 | |
|---|
| 369 | |
|---|
| 370 | |
|---|
| 371 | |
|---|
| 372 | |
|---|
| 373 | |
|---|
| 374 | public function isDirty(){ |
|---|
| 375 | return $this->dirty; |
|---|
| 376 | } |
|---|
| 377 | |
|---|
| 378 | |
|---|
| 379 | |
|---|
| 380 | |
|---|
| 381 | |
|---|
| 382 | |
|---|
| 383 | |
|---|
| 384 | |
|---|
| 385 | |
|---|
| 386 | |
|---|
| 387 | private function create($x = null, $y = null, $ext = null){ |
|---|
| 388 | if(!$x) $x = $this->meta['width']; |
|---|
| 389 | if(!$y) $y = $this->meta['height']; |
|---|
| 390 | |
|---|
| 391 | $image = @imagecreatetruecolor($x, $y); |
|---|
| 392 | if (!$image) throw new Exception('imagecreatetruecolor_failed'); |
|---|
| 393 | if(!$ext) $ext = $this->meta['ext']; |
|---|
| 394 | if($ext == 'png'){ |
|---|
| 395 | if (!@imagealphablending($image, false)) |
|---|
| 396 | throw new Exception('imagealphablending_failed'); |
|---|
| 397 | $alpha = @imagecolorallocatealpha($image, 0, 0, 0, 127); |
|---|
| 398 | if (!$alpha) throw new Exception('imageallocalpha50pctgrey_failed'); |
|---|
| 399 | imagefilledrectangle($image, 0, 0, $x, $y, $alpha); |
|---|
| 400 | } |
|---|
| 401 | |
|---|
| 402 | return $image; |
|---|
| 403 | } |
|---|
| 404 | |
|---|
| 405 | |
|---|
| 406 | |
|---|
| 407 | |
|---|
| 408 | |
|---|
| 409 | |
|---|
| 410 | private function set($new){ |
|---|
| 411 | if(!empty($this->image)) imagedestroy($this->image); |
|---|
| 412 | $this->dirty = true; |
|---|
| 413 | $this->image = $new; |
|---|
| 414 | |
|---|
| 415 | $this->meta['width'] = imagesx($this->image); |
|---|
| 416 | $this->meta['height'] = imagesy($this->image); |
|---|
| 417 | } |
|---|
| 418 | |
|---|
| 419 | |
|---|
| 420 | |
|---|
| 421 | |
|---|
| 422 | |
|---|
| 423 | |
|---|
| 424 | public function getImagePath(){ |
|---|
| 425 | return $this->file; |
|---|
| 426 | } |
|---|
| 427 | |
|---|
| 428 | |
|---|
| 429 | |
|---|
| 430 | |
|---|
| 431 | |
|---|
| 432 | |
|---|
| 433 | public function getResource(){ |
|---|
| 434 | return $this->image; |
|---|
| 435 | } |
|---|
| 436 | |
|---|
| 437 | |
|---|
| 438 | |
|---|
| 439 | |
|---|
| 440 | |
|---|
| 441 | |
|---|
| 442 | |
|---|
| 443 | |
|---|
| 444 | public function rotate($angle, $bgcolor = null){ |
|---|
| 445 | if(empty($this->image) || !$angle || $angle>=360) return $this; |
|---|
| 446 | |
|---|
| 447 | $alpha = (is_array($bgcolor) ? @imagecolorallocatealpha($this->image, $bgcolor[0], $bgcolor[1], $bgcolor[2], !empty($bgcolor[3]) ? $bgcolor[3] : null) : $bgcolor); |
|---|
| 448 | if (!$alpha) throw new Exception('imagecolorallocatealpha_failed'); |
|---|
| 449 | $img = @imagerotate($this->image, $angle, $alpha); |
|---|
| 450 | if (!$img) throw new Exception('imagerotate_failed'); |
|---|
| 451 | $this->set($img); |
|---|
| 452 | unset($img); |
|---|
| 453 | |
|---|
| 454 | return $this; |
|---|
| 455 | } |
|---|
| 456 | |
|---|
| 457 | |
|---|
| 458 | |
|---|
| 459 | |
|---|
| 460 | |
|---|
| 461 | |
|---|
| 462 | |
|---|
| 463 | |
|---|
| 464 | |
|---|
| 465 | |
|---|
| 466 | |
|---|
| 467 | |
|---|
| 468 | |
|---|
| 469 | |
|---|
| 470 | public function resize($x = null, $y = null, $ratio = true, $resizeWhenSmaller = false) |
|---|
| 471 | { |
|---|
| 472 | if(empty($this->image) || (empty($x) && empty($y))) |
|---|
| 473 | { |
|---|
| 474 | throw new Exception('resize_inerr'); |
|---|
| 475 | } |
|---|
| 476 | |
|---|
| 477 | $dims = Image::calculate_resize_dimensions($this->meta['width'], $this->meta['height'], $x, $y, $ratio, $resizeWhenSmaller); |
|---|
| 478 | if ($dims === false) |
|---|
| 479 | { |
|---|
| 480 | throw new Exception('resize_inerr:' . $this->meta['width'] . ' x ' . $this->meta['height']); |
|---|
| 481 | } |
|---|
| 482 | |
|---|
| 483 | |
|---|
| 484 | if ($dims['must_resize']) |
|---|
| 485 | { |
|---|
| 486 | $new = $this->create($dims['width'], $dims['height']); |
|---|
| 487 | if(@imagecopyresampled($new, $this->image, 0, 0, 0, 0, $dims['width'], $dims['height'], $this->meta['width'], $this->meta['height'])) { |
|---|
| 488 | $this->set($new); |
|---|
| 489 | unset($new); |
|---|
| 490 | return $this; |
|---|
| 491 | } |
|---|
| 492 | unset($new); |
|---|
| 493 | throw new Exception('imagecopyresampled_failed:' . $this->meta['width'] . ' x ' . $this->meta['height']); |
|---|
| 494 | } |
|---|
| 495 | else |
|---|
| 496 | { |
|---|
| 497 | return $this; |
|---|
| 498 | } |
|---|
| 499 | } |
|---|
| 500 | |
|---|
| 501 | |
|---|
| 502 | |
|---|
| 503 | |
|---|
| 504 | |
|---|
| 505 | |
|---|
| 506 | |
|---|
| 507 | |
|---|
| 508 | |
|---|
| 509 | |
|---|
| 510 | |
|---|
| 511 | |
|---|
| 512 | |
|---|
| 513 | |
|---|
| 514 | |
|---|
| 515 | |
|---|
| 516 | |
|---|
| 517 | |
|---|
| 518 | public function crop($top, $right = null, $bottom = null, $left = null){ |
|---|
| 519 | if(empty($this->image)) return $this; |
|---|
| 520 | |
|---|
| 521 | if(!is_numeric($right) && !is_numeric($bottom) && !is_numeric($left)) |
|---|
| 522 | $right = $bottom = $left = $top; |
|---|
| 523 | |
|---|
| 524 | if(!is_numeric($bottom) && !is_numeric($left)){ |
|---|
| 525 | $bottom = $top; |
|---|
| 526 | $left = $right; |
|---|
| 527 | } |
|---|
| 528 | |
|---|
| 529 | if(!is_numeric($left)) |
|---|
| 530 | $left = $right; |
|---|
| 531 | |
|---|
| 532 | $x = $this->meta['width']-$left-$right; |
|---|
| 533 | $y = $this->meta['height']-$top-$bottom; |
|---|
| 534 | |
|---|
| 535 | if($x<0 || $y<0) return $this; |
|---|
| 536 | |
|---|
| 537 | $new = $this->create($x, $y); |
|---|
| 538 | if (!@imagecopy($new, $this->image, 0, 0, $left, $top, $x, $y)) |
|---|
| 539 | throw new Exception('imagecopy_failed'); |
|---|
| 540 | |
|---|
| 541 | $this->set($new); |
|---|
| 542 | unset($new); |
|---|
| 543 | |
|---|
| 544 | return $this; |
|---|
| 545 | } |
|---|
| 546 | |
|---|
| 547 | |
|---|
| 548 | |
|---|
| 549 | |
|---|
| 550 | |
|---|
| 551 | |
|---|
| 552 | |
|---|
| 553 | |
|---|
| 554 | |
|---|
| 555 | public function flip($type){ |
|---|
| 556 | if(empty($this->image) || !in_array($type, array('horizontal', 'vertical'))) return $this; |
|---|
| 557 | |
|---|
| 558 | $new = $this->create(); |
|---|
| 559 | |
|---|
| 560 | if($type=='horizontal') |
|---|
| 561 | { |
|---|
| 562 | for($x=0;$x<$this->meta['width'];$x++) |
|---|
| 563 | { |
|---|
| 564 | if (!@imagecopy($new, $this->image, $this->meta['width']-$x-1, 0, $x, 0, 1, $this->meta['height'])) |
|---|
| 565 | throw new Exception('imageflip_failed'); |
|---|
| 566 | } |
|---|
| 567 | } |
|---|
| 568 | elseif($type=='vertical') |
|---|
| 569 | { |
|---|
| 570 | for($y=0;$y<$this->meta['height'];$y++) |
|---|
| 571 | { |
|---|
| 572 | if (!@imagecopy($new, $this->image, 0, $this->meta['height']-$y-1, 0, $y, $this->meta['width'], 1)) |
|---|
| 573 | throw new Exception('imageflip_failed'); |
|---|
| 574 | } |
|---|
| 575 | } |
|---|
| 576 | |
|---|
| 577 | $this->set($new); |
|---|
| 578 | unset($new); |
|---|
| 579 | |
|---|
| 580 | return $this; |
|---|
| 581 | } |
|---|
| 582 | |
|---|
| 583 | |
|---|
| 584 | |
|---|
| 585 | |
|---|
| 586 | |
|---|
| 587 | |
|---|
| 588 | |
|---|
| 589 | |
|---|
| 590 | |
|---|
| 591 | |
|---|
| 592 | |
|---|
| 593 | |
|---|
| 594 | |
|---|
| 595 | |
|---|
| 596 | |
|---|
| 597 | public function process($ext = null, $file = null, $quality = 100, $store_original_if_unaltered = false){ |
|---|
| 598 | if(empty($this->image)) return $this; |
|---|
| 599 | |
|---|
| 600 | if(!$ext) $ext = $this->meta['ext']; |
|---|
| 601 | $ext = strtolower($ext); |
|---|
| 602 | |
|---|
| 603 | if($ext=='jpg') $ext = 'jpeg'; |
|---|
| 604 | else if($ext=='png') imagesavealpha($this->image, true); |
|---|
| 605 | |
|---|
| 606 | if($file == null) |
|---|
| 607 | $file = $this->file; |
|---|
| 608 | if(!$file) throw new Exception('process_nofile'); |
|---|
| 609 | if(!is_dir(dirname($file))) throw new Exception('process_nodir'); |
|---|
| 610 | if ($store_original_if_unaltered && !$this->isDirty() && $ext == $this->meta['ext']) |
|---|
| 611 | { |
|---|
| 612 | |
|---|
| 613 | $rv = true; |
|---|
| 614 | if ($file != $this->file) |
|---|
| 615 | { |
|---|
| 616 | $rv = @copy($this->file, $file); |
|---|
| 617 | } |
|---|
| 618 | } |
|---|
| 619 | else |
|---|
| 620 | { |
|---|
| 621 | $rv = false; |
|---|
| 622 | $fn = 'image'.$ext; |
|---|
| 623 | if (function_exists($fn)) |
|---|
| 624 | { |
|---|
| 625 | if($ext == 'jpeg') |
|---|
| 626 | $rv = @$fn($this->image, $file, $quality); |
|---|
| 627 | elseif($ext == 'png') |
|---|
| 628 | $rv = @$fn($this->image, $file, 9); |
|---|
| 629 | else |
|---|
| 630 | $rv = @$fn($this->image, $file); |
|---|
| 631 | } |
|---|
| 632 | } |
|---|
| 633 | if (!$rv) |
|---|
| 634 | throw new Exception($fn . '_failed'); |
|---|
| 635 | |
|---|
| 636 | |
|---|
| 637 | $this->file = $file; |
|---|
| 638 | |
|---|
| 639 | return $this; |
|---|
| 640 | } |
|---|
| 641 | |
|---|
| 642 | |
|---|
| 643 | |
|---|
| 644 | |
|---|
| 645 | |
|---|
| 646 | |
|---|
| 647 | |
|---|
| 648 | |
|---|
| 649 | |
|---|
| 650 | |
|---|
| 651 | |
|---|
| 652 | |
|---|
| 653 | |
|---|
| 654 | |
|---|
| 655 | public function save($file = null, $quality = 100, $store_original_if_unaltered = false){ |
|---|
| 656 | if(empty($this->image)) return $this; |
|---|
| 657 | |
|---|
| 658 | if(!$file) $file = $this->file; |
|---|
| 659 | |
|---|
| 660 | $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION)); |
|---|
| 661 | if(!$ext){ |
|---|
| 662 | $file .= '.'.$this->meta['ext']; |
|---|
| 663 | $ext = $this->meta['ext']; |
|---|
| 664 | } |
|---|
| 665 | |
|---|
| 666 | if($ext=='jpg') $ext = 'jpeg'; |
|---|
| 667 | |
|---|
| 668 | return $this->process($ext, $file, $quality, $store_original_if_unaltered); |
|---|
| 669 | } |
|---|
| 670 | |
|---|
| 671 | |
|---|
| 672 | |
|---|
| 673 | |
|---|
| 674 | |
|---|
| 675 | |
|---|
| 676 | public function show(){ |
|---|
| 677 | if(empty($this->image)) return $this; |
|---|
| 678 | |
|---|
| 679 | header('Content-type: '.$this->meta['mime']); |
|---|
| 680 | return $this->process(); |
|---|
| 681 | } |
|---|
| 682 | } |
|---|
| 683 | |
|---|
| 684 | |
|---|
| 685 | |
|---|
| 686 | |
|---|
| 687 | |
|---|
| 688 | |
|---|
| 689 | if (!function_exists('imagecreatefrombmp')) |
|---|
| 690 | { |
|---|
| 691 | |
|---|
| 692 | |
|---|
| 693 | |
|---|
| 694 | function imagecreatefrombmp($filepath) |
|---|
| 695 | { |
|---|
| 696 | |
|---|
| 697 | $filesize = @filesize($filepath); |
|---|
| 698 | if ($filesize < 108 + 4) |
|---|
| 699 | return false; |
|---|
| 700 | |
|---|
| 701 | $read = file_get_contents($filepath); |
|---|
| 702 | if ($file === false) |
|---|
| 703 | return false; |
|---|
| 704 | |
|---|
| 705 | $temp = unpack("H*",$read); |
|---|
| 706 | unset($read); |
|---|
| 707 | $hex = $temp[1]; |
|---|
| 708 | unset($temp); |
|---|
| 709 | $header = substr($hex, 0, 108); |
|---|
| 710 | |
|---|
| 711 | |
|---|
| 712 | |
|---|
| 713 | if (substr($header, 0, 4) == '424d') |
|---|
| 714 | { |
|---|
| 715 | |
|---|
| 716 | $header_parts = str_split($header, 2); |
|---|
| 717 | |
|---|
| 718 | |
|---|
| 719 | $width = hexdec($header_parts[19] . $header_parts[18]); |
|---|
| 720 | |
|---|
| 721 | |
|---|
| 722 | $height = hexdec($header_parts[23] . $header_parts[22]); |
|---|
| 723 | |
|---|
| 724 | |
|---|
| 725 | unset($header_parts); |
|---|
| 726 | } |
|---|
| 727 | |
|---|
| 728 | |
|---|
| 729 | $x = 0; |
|---|
| 730 | $y = 1; |
|---|
| 731 | |
|---|
| 732 | |
|---|
| 733 | $image = imagecreatetruecolor($width, $height); |
|---|
| 734 | if ($image === false) |
|---|
| 735 | return $image; |
|---|
| 736 | |
|---|
| 737 | |
|---|
| 738 | $body = substr($hex, 108); |
|---|
| 739 | unset($hex); |
|---|
| 740 | |
|---|
| 741 | |
|---|
| 742 | |
|---|
| 743 | |
|---|
| 744 | $body_size = strlen($body) / 2; |
|---|
| 745 | $header_size = $width * $height; |
|---|
| 746 | |
|---|
| 747 | |
|---|
| 748 | $usePadding = ($body_size > $header_size * 3 + 4); |
|---|
| 749 | |
|---|
| 750 | |
|---|
| 751 | |
|---|
| 752 | for ($i = 0; $i < $body_size; $i += 3) |
|---|
| 753 | { |
|---|
| 754 | |
|---|
| 755 | if ($x >= $width) |
|---|
| 756 | { |
|---|
| 757 | |
|---|
| 758 | |
|---|
| 759 | if ($usePadding) |
|---|
| 760 | $i += $width % 4; |
|---|
| 761 | |
|---|
| 762 | |
|---|
| 763 | $x = 0; |
|---|
| 764 | |
|---|
| 765 | |
|---|
| 766 | $y++; |
|---|
| 767 | |
|---|
| 768 | |
|---|
| 769 | if ($y > $height) |
|---|
| 770 | break; |
|---|
| 771 | } |
|---|
| 772 | |
|---|
| 773 | |
|---|
| 774 | |
|---|
| 775 | $i_pos = $i * 2; |
|---|
| 776 | $r = hexdec($body[$i_pos+4] . $body[$i_pos+5]); |
|---|
| 777 | $g = hexdec($body[$i_pos+2] . $body[$i_pos+3]); |
|---|
| 778 | $b = hexdec($body[$i_pos] . $body[$i_pos+1]); |
|---|
| 779 | |
|---|
| 780 | |
|---|
| 781 | $color = imagecolorallocate($image, $r, $g, $b); |
|---|
| 782 | if ($color === false) |
|---|
| 783 | { |
|---|
| 784 | imagedestroy($image); |
|---|
| 785 | return false; |
|---|
| 786 | } |
|---|
| 787 | imagesetpixel($image, $x, $height - $y, $color); |
|---|
| 788 | |
|---|
| 789 | |
|---|
| 790 | $x++; |
|---|
| 791 | } |
|---|
| 792 | |
|---|
| 793 | |
|---|
| 794 | unset($body); |
|---|
| 795 | |
|---|
| 796 | |
|---|
| 797 | return $image; |
|---|
| 798 | } |
|---|
| 799 | } |
|---|