paulgoughbooks_old
This repository has no backups
This repository's network speed is throttled to 100KB/sec
Upgrade your account to fix these warnings, or use backups.vc for automated backups
@ 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 |
<?php /** * CodeIgniter * * An open source application development framework for PHP * * This content is released under the MIT License (MIT) * * Copyright (c) 2014-2019 British Columbia Institute of Technology * Copyright (c) 2019-2020 CodeIgniter Foundation * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * * @package CodeIgniter * @author CodeIgniter Dev Team * @copyright 2019-2020 CodeIgniter Foundation * @license https://opensource.org/licenses/MIT MIT License * @link https://codeigniter.com * @since Version 4.0.0 * @filesource */ namespace CodeIgniter\Images\Handlers; use CodeIgniter\Images\Exceptions\ImageException; /** * Class ImageMagickHandler * * To make this library as compatible as possible with the broadest * number of installations, we do not use the Imagick extension, * but simply use the command line version. * * hmm - the width & height accessors at the end use the imagick extension. * * FIXME - This needs conversion & unit testing, to use the imagick extension * * @package CodeIgniter\Images\Handlers */ class ImageMagickHandler extends BaseHandler { /** * Stores image resource in memory. * * @var string */ protected $resource; //-------------------------------------------------------------------- /** * Constructor. * * @param \Config\Images $config * @throws ImageException */ public function __construct($config = null) { parent::__construct($config); // We should never see this, so can't test it // @codeCoverageIgnoreStart if (! (extension_loaded('imagick') || class_exists('Imagick'))) { throw ImageException::forMissingExtension('IMAGICK'); } // @codeCoverageIgnoreEnd } //-------------------------------------------------------------------- /** * Handles the actual resizing of the image. * * @param boolean $maintainRatio * * @return ImageMagickHandler * @throws \Exception */ public function _resize(bool $maintainRatio = false) { $source = ! empty($this->resource) ? $this->resource : $this->image()->getPathname(); $destination = $this->getResourcePath(); $escape = '\\'; if (stripos(PHP_OS, 'WIN') === 0) { $escape = ''; } $action = $maintainRatio === true ? ' -resize ' . ($this->width ?? 0) . 'x' . ($this->height ?? 0) . ' "' . $source . '" "' . $destination . '"' : ' -resize ' . ($this->width ?? 0) . 'x' . ($this->height ?? 0) . "{$escape}! \"" . $source . '" "' . $destination . '"'; $this->process($action); return $this; } //-------------------------------------------------------------------- /** * Crops the image. * * @return boolean|\CodeIgniter\Images\Handlers\ImageMagickHandler * @throws \Exception */ public function _crop() { $source = ! empty($this->resource) ? $this->resource : $this->image()->getPathname(); $destination = $this->getResourcePath(); $extent = ' '; if ($this->xAxis >= $this->width || $this->yAxis > $this->height) { $extent = ' -background transparent -extent ' . ($this->width ?? 0) . 'x' . ($this->height ?? 0) . ' '; } $action = ' -crop ' . ($this->width ?? 0) . 'x' . ($this->height ?? 0) . '+' . ($this->xAxis ?? 0) . '+' . ($this->yAxis ?? 0) . $extent . escapeshellarg($source) . ' ' . escapeshellarg($destination); $this->process($action); return $this; } //-------------------------------------------------------------------- /** * Handles the rotation of an image resource. * Doesn't save the image, but replaces the current resource. * * @param integer $angle * * @return $this * @throws \Exception */ protected function _rotate(int $angle) { $angle = '-rotate ' . $angle; $source = ! empty($this->resource) ? $this->resource : $this->image()->getPathname(); $destination = $this->getResourcePath(); $action = ' ' . $angle . ' ' . escapeshellarg($source) . ' ' . escapeshellarg($destination); $this->process($action); return $this; } //-------------------------------------------------------------------- /** * Flattens transparencies, default white background * * @param integer $red * @param integer $green * @param integer $blue * * @return $this * @throws \Exception */ public function _flatten(int $red = 255, int $green = 255, int $blue = 255) { $flatten = "-background 'rgb({$red},{$green},{$blue})' -flatten"; $source = ! empty($this->resource) ? $this->resource : $this->image()->getPathname(); $destination = $this->getResourcePath(); $action = ' ' . $flatten . ' ' . escapeshellarg($source) . ' ' . escapeshellarg($destination); $this->process($action); return $this; } //-------------------------------------------------------------------- /** * Flips an image along it's vertical or horizontal axis. * * @param string $direction * * @return $this * @throws \Exception */ public function _flip(string $direction) { $angle = $direction === 'horizontal' ? '-flop' : '-flip'; $source = ! empty($this->resource) ? $this->resource : $this->image()->getPathname(); $destination = $this->getResourcePath(); $action = ' ' . $angle . ' ' . escapeshellarg($source) . ' ' . escapeshellarg($destination); $this->process($action); return $this; } //-------------------------------------------------------------------- /** * Get driver version * * @return string */ public function getVersion(): string { $result = $this->process('-version'); // The first line has the version in it... preg_match('/(ImageMagick\s[\S]+)/', $result[0], $matches); return str_replace('ImageMagick ', '', $matches[0]); } //-------------------------------------------------------------------- /** * Handles all of the grunt work of resizing, etc. * * @param string $action * @param integer $quality * * @return array Lines of output from shell command * @throws \Exception */ protected function process(string $action, int $quality = 100): array { // Do we have a vaild library path? if (empty($this->config->libraryPath)) { throw ImageException::forInvalidImageLibraryPath($this->config->libraryPath); } if ($action !== '-version') { $this->supportedFormatCheck(); } if (! preg_match('/convert$/i', $this->config->libraryPath)) { $this->config->libraryPath = rtrim($this->config->libraryPath, '/') . '/convert'; } $cmd = $this->config->libraryPath; $cmd .= $action === '-version' ? ' ' . $action : ' -quality ' . $quality . ' ' . $action; $retval = 1; // exec() might be disabled if (function_usable('exec')) { @exec($cmd, $output, $retval); } // Did it work? if ($retval > 0) { throw ImageException::forImageProcessFailed(); } return $output; } //-------------------------------------------------------------------- /** * Saves any changes that have been made to file. If no new filename is * provided, the existing image is overwritten, otherwise a copy of the * file is made at $target. * * Example: * $image->resize(100, 200, true) * ->save(); * * @param string|null $target * @param integer $quality * * @return boolean */ public function save(string $target = null, int $quality = 90): bool { $original = $target; $target = empty($target) ? $this->image()->getPathname() : $target; // If no new resource has been created, then we're // simply copy the existing one. if (empty($this->resource) && $quality === 100) { if ($original === null) { return true; } $name = basename($target); $path = pathinfo($target, PATHINFO_DIRNAME); return $this->image()->copy($path, $name); } $this->ensureResource(); // Copy the file through ImageMagick so that it has // a chance to convert file format. $action = escapeshellarg($this->resource) . ' ' . escapeshellarg($target); $result = $this->process($action, $quality); unlink($this->resource); return true; } //-------------------------------------------------------------------- /** * Get Image Resource * * This simply creates an image resource handle * based on the type of image being processed. * Since ImageMagick is used on the cli, we need to * ensure we have a temporary file on the server * that we can use. * * To ensure we can use all features, like transparency, * during the process, we'll use a PNG as the temp file type. * * @return resource|boolean * @throws \Exception */ protected function getResourcePath() { if (! is_null($this->resource)) { return $this->resource; } $this->resource = WRITEPATH . 'cache/' . time() . '_' . bin2hex(random_bytes(10)) . '.png'; $name = basename($this->resource); $path = pathinfo($this->resource, PATHINFO_DIRNAME); $this->image()->copy($path, $name); return $this->resource; } //-------------------------------------------------------------------- /** * Make the image resource object if needed * * @throws \Exception */ protected function ensureResource() { $this->getResourcePath(); $this->supportedFormatCheck(); } /** * Check if given image format is supported * * @throws ImageException */ protected function supportedFormatCheck() { switch ($this->image()->imageType) { case IMAGETYPE_WEBP: if (! in_array('WEBP', \Imagick::queryFormats())) { throw ImageException::forInvalidImageCreate(lang('images.webpNotSupported')); } break; } } //-------------------------------------------------------------------- /** * Handler-specific method for overlaying text on an image. * * @param string $text * @param array $options * * @throws \Exception */ protected function _text(string $text, array $options = []) { $cmd = ''; // Reverse the vertical offset // When the image is positioned at the bottom // we don't want the vertical offset to push it // further down. We want the reverse, so we'll // invert the offset. Note: The horizontal // offset flips itself automatically if ($options['vAlign'] === 'bottom') { $options['vOffset'] = $options['vOffset'] * -1; } if ($options['hAlign'] === 'right') { $options['hOffset'] = $options['hOffset'] * -1; } // Font if (! empty($options['fontPath'])) { $cmd .= " -font '{$options['fontPath']}'"; } if (isset($options['hAlign']) && isset($options['vAlign'])) { switch ($options['hAlign']) { case 'left': $xAxis = $options['hOffset'] + $options['padding']; $yAxis = $options['vOffset'] + $options['padding']; $gravity = $options['vAlign'] === 'top' ? 'NorthWest' : 'West'; if ($options['vAlign'] === 'bottom') { $gravity = 'SouthWest'; $yAxis = $options['vOffset'] - $options['padding']; } break; case 'center': $xAxis = $options['hOffset'] + $options['padding']; $yAxis = $options['vOffset'] + $options['padding']; $gravity = $options['vAlign'] === 'top' ? 'North' : 'Center'; if ($options['vAlign'] === 'bottom') { $yAxis = $options['vOffset'] - $options['padding']; $gravity = 'South'; } break; case 'right': $xAxis = $options['hOffset'] - $options['padding']; $yAxis = $options['vOffset'] + $options['padding']; $gravity = $options['vAlign'] === 'top' ? 'NorthEast' : 'East'; if ($options['vAlign'] === 'bottom') { $gravity = 'SouthEast'; $yAxis = $options['vOffset'] - $options['padding']; } break; } $xAxis = $xAxis >= 0 ? '+' . $xAxis : $xAxis; $yAxis = $yAxis >= 0 ? '+' . $yAxis : $yAxis; $cmd .= " -gravity {$gravity} -geometry {$xAxis}{$yAxis}"; } // Color if (isset($options['color'])) { list($r, $g, $b) = sscanf("#{$options['color']}", '#%02x%02x%02x'); $cmd .= " -fill 'rgba({$r},{$g},{$b},{$options['opacity']})'"; } // Font Size - use points.... if (isset($options['fontSize'])) { $cmd .= " -pointsize {$options['fontSize']}"; } // Text $cmd .= " -annotate 0 '{$text}'"; $source = ! empty($this->resource) ? $this->resource : $this->image()->getPathname(); $destination = $this->getResourcePath(); $cmd = " '{$source}' {$cmd} '{$destination}'"; $this->process($cmd); } //-------------------------------------------------------------------- /** * Return the width of an image. * * @return integer */ public function _getWidth() { return imagesx(imagecreatefromstring(file_get_contents($this->resource))); } /** * Return the height of an image. * * @return integer */ public function _getHeight() { return imagesy(imagecreatefromstring(file_get_contents($this->resource))); } //-------------------------------------------------------------------- /** * Reads the EXIF information from the image and modifies the orientation * so that displays correctly in the browser. This is especially an issue * with images taken by smartphones who always store the image up-right, * but set the orientation flag to display it correctly. * * @param boolean $silent If true, will ignore exceptions when PHP doesn't support EXIF. * * @return $this */ public function reorient(bool $silent = false) { $orientation = $this->getEXIF('Orientation', $silent); switch ($orientation) { case 2: return $this->flip('horizontal'); case 3: return $this->rotate(180); case 4: return $this->rotate(180) ->flip('horizontal'); case 5: return $this->rotate(90) ->flip('horizontal'); case 6: return $this->rotate(90); case 7: return $this->rotate(270) ->flip('horizontal'); case 8: return $this->rotate(270); default: return $this; } } } |
Commits for paulgoughbooks_old/trunk/system/Images/Handlers/ImageMagickHandler.php
Revision | Author | Commited | Message |
---|---|---|---|
2 | tporter | Tue 03 Nov, 2020 08:50:21 +0000 | Migration of Paul Goughs Books site to Codeignitor 4 |