Image.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace Magefan\Blog\Helper;
  3. use Magento\Framework\App\Area;
  4. use Magento\Framework\App\Helper\AbstractHelper;
  5. use Magento\Framework\App\Filesystem\DirectoryList;
  6. /**
  7. * Blog image helper
  8. * @SuppressWarnings(PHPMD.TooManyFields)
  9. */
  10. class Image extends AbstractHelper
  11. {
  12. /**
  13. * Default quality value (for JPEG images only).
  14. *
  15. * @var int
  16. */
  17. protected $_quality = 100;
  18. protected $_keepAspectRatio = true;
  19. protected $_keepFrame = true;
  20. protected $_keepTransparency = true;
  21. protected $_constrainOnly = true;
  22. protected $_backgroundColor = [255, 255, 255];
  23. protected $_baseFile;
  24. protected $_newFile;
  25. public function __construct(
  26. \Magento\Framework\App\Helper\Context $context,
  27. \Magento\Framework\Image\Factory $imageFactory,
  28. \Magento\Framework\Filesystem $filesystem,
  29. \Magento\Store\Model\StoreManagerInterface $storeManager
  30. ) {
  31. $this->_imageFactory = $imageFactory;
  32. $this->_mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
  33. $this->_storeManager = $storeManager;
  34. parent::__construct($context);
  35. }
  36. public function init($baseFile)
  37. {
  38. $this->_newFile = '';
  39. $this->_baseFile = $baseFile;
  40. return $this;
  41. }
  42. public function resize($width, $height = null)
  43. {
  44. if ($this->_baseFile){
  45. $path = 'blog/cache/' . $width . 'x' . $height;
  46. $this->_newFile = $path. '/' . $this->_baseFile;
  47. if (true || !$this->fileExists($this->_newFile)) {
  48. $this->resizeBaseFile($width, $height);
  49. }
  50. }
  51. return $this;
  52. }
  53. protected function resizeBaseFile($width, $height)
  54. {
  55. if (!$this->fileExists($this->_baseFile)) {
  56. $this->_baseFile = null;
  57. return $this;
  58. }
  59. $processor = $this->_imageFactory->create(
  60. $this->_mediaDirectory->getAbsolutePath($this->_baseFile)
  61. );
  62. $processor->keepAspectRatio($this->_keepAspectRatio);
  63. $processor->keepFrame($this->_keepFrame);
  64. $processor->keepTransparency($this->_keepTransparency);
  65. $processor->constrainOnly($this->_constrainOnly);
  66. $processor->backgroundColor($this->_backgroundColor);
  67. $processor->quality($this->_quality);
  68. $processor->resize($width, $height);
  69. $newFile = $this->_mediaDirectory->getAbsolutePath($this->_newFile);
  70. $processor->save($newFile);
  71. unset($processor);
  72. return $this;
  73. }
  74. protected function fileExists($filename)
  75. {
  76. return $this->_mediaDirectory->isFile($filename);
  77. }
  78. public function __toString()
  79. {
  80. $url = "";
  81. if ($this->_baseFile){
  82. $url = $this->_storeManager->getStore()->getBaseUrl(
  83. \Magento\Framework\UrlInterface::URL_TYPE_MEDIA
  84. ) . $this->_newFile;
  85. }
  86. return $url;
  87. }
  88. }