ImageFile.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * @package Grav\Common\Page
  4. *
  5. * @copyright Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common\Page\Medium;
  9. use Grav\Common\Grav;
  10. use Gregwar\Image\Exceptions\GenerationError;
  11. use Gregwar\Image\Image;
  12. use Gregwar\Image\Source;
  13. use RocketTheme\Toolbox\Event\Event;
  14. class ImageFile extends Image
  15. {
  16. public function __destruct()
  17. {
  18. $this->getAdapter()->deinit();
  19. }
  20. /**
  21. * Clear previously applied operations
  22. */
  23. public function clearOperations()
  24. {
  25. $this->operations = [];
  26. }
  27. /**
  28. * This is the same as the Gregwar Image class except this one fires a Grav Event on creation of new cached file
  29. *
  30. * @param string $type the image type
  31. * @param int $quality the quality (for JPEG)
  32. * @param bool $actual
  33. * @param array $extras
  34. *
  35. * @return string
  36. */
  37. public function cacheFile($type = 'jpg', $quality = 80, $actual = false, $extras = [])
  38. {
  39. if ($type === 'guess') {
  40. $type = $this->guessType();
  41. }
  42. if (!$this->forceCache && !count($this->operations) && $type === $this->guessType()) {
  43. return $this->getFilename($this->getFilePath());
  44. }
  45. // Computes the hash
  46. $this->hash = $this->getHash($type, $quality, $extras);
  47. // Seo friendly image names
  48. $seofriendly = Grav::instance()['config']->get('system.images.seofriendly', false);
  49. if ($seofriendly) {
  50. $mini_hash = substr($this->hash, 0, 4) . substr($this->hash, -4);
  51. $cacheFile = "{$this->prettyName}-{$mini_hash}";
  52. } else {
  53. $cacheFile = "{$this->hash}-{$this->prettyName}";
  54. }
  55. $cacheFile .= '.' . $type;
  56. // If the files does not exists, save it
  57. $image = $this;
  58. // Target file should be younger than all the current image
  59. // dependencies
  60. $conditions = array(
  61. 'younger-than' => $this->getDependencies()
  62. );
  63. // The generating function
  64. $generate = function ($target) use ($image, $type, $quality) {
  65. $result = $image->save($target, $type, $quality);
  66. if ($result !== $target) {
  67. throw new GenerationError($result);
  68. }
  69. Grav::instance()->fireEvent('onImageMediumSaved', new Event(['image' => $target]));
  70. };
  71. // Asking the cache for the cacheFile
  72. try {
  73. $perms = Grav::instance()['config']->get('system.images.cache_perms', '0755');
  74. $perms = octdec($perms);
  75. $file = $this->getCacheSystem()->setDirectoryMode($perms)->getOrCreateFile($cacheFile, $conditions, $generate, $actual);
  76. } catch (GenerationError $e) {
  77. $file = $e->getNewFile();
  78. }
  79. // Nulling the resource
  80. $this->getAdapter()->setSource(new Source\File($file));
  81. $this->getAdapter()->deinit();
  82. if ($actual) {
  83. return $file;
  84. }
  85. return $this->getFilename($file);
  86. }
  87. /**
  88. * Gets the hash.
  89. * @param string $type
  90. * @param int $quality
  91. * @param [] $extras
  92. * @return null
  93. */
  94. public function getHash($type = 'guess', $quality = 80, $extras = [])
  95. {
  96. if (null === $this->hash) {
  97. $this->generateHash($type, $quality, $extras);
  98. }
  99. return $this->hash;
  100. }
  101. /**
  102. * Generates the hash.
  103. * @param string $type
  104. * @param int $quality
  105. * @param array $extras
  106. */
  107. public function generateHash($type = 'guess', $quality = 80, $extras = [])
  108. {
  109. $inputInfos = $this->source->getInfos();
  110. $datas = array(
  111. $inputInfos,
  112. $this->serializeOperations(),
  113. $type,
  114. $quality,
  115. $extras
  116. );
  117. $this->hash = sha1(serialize($datas));
  118. }
  119. }