Media.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Mageplaza
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Mageplaza.com license that is
  8. * available through the world-wide-web at this URL:
  9. * https://www.mageplaza.com/LICENSE.txt
  10. *
  11. * DISCLAIMER
  12. *
  13. * Do not edit or add to this file if you wish to upgrade this extension to newer
  14. * version in the future.
  15. *
  16. * @category Mageplaza
  17. * @package Mageplaza_Search
  18. * @copyright Copyright (c) 2017 Mageplaza (http://www.mageplaza.com/)
  19. * @license https://www.mageplaza.com/LICENSE.txt
  20. */
  21. namespace Mageplaza\Search\Helper;
  22. use Magento\Catalog\Helper\ImageFactory;
  23. use Magento\Framework\App\Helper\Context;
  24. use Magento\Framework\Filesystem;
  25. use Magento\Framework\Image\AdapterFactory;
  26. use Magento\Framework\ObjectManagerInterface;
  27. use Magento\MediaStorage\Model\File\UploaderFactory;
  28. use Magento\Store\Model\StoreManagerInterface;
  29. use Mageplaza\Core\Helper\Media as CoreMedia;
  30. /**
  31. * Class Media
  32. * @package Mageplaza\Search\Helper
  33. */
  34. class Media extends CoreMedia
  35. {
  36. const TEMPLATE_MEDIA_PATH = 'mageplaza/search';
  37. /**
  38. * @var \Magento\Catalog\Helper\ImageFactory
  39. */
  40. protected $imageFactory;
  41. /**
  42. * Media constructor.
  43. * @param \Magento\Framework\App\Helper\Context $context
  44. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  45. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  46. * @param \Magento\Framework\Filesystem $filesystem
  47. * @param \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory
  48. * @param \Magento\Framework\Image\AdapterFactory $adapterFactory
  49. * @param \Magento\Catalog\Helper\ImageFactory $imageFactory
  50. */
  51. public function __construct(
  52. Context $context,
  53. ObjectManagerInterface $objectManager,
  54. StoreManagerInterface $storeManager,
  55. Filesystem $filesystem,
  56. UploaderFactory $uploaderFactory,
  57. AdapterFactory $adapterFactory,
  58. ImageFactory $imageFactory
  59. )
  60. {
  61. parent::__construct($context, $objectManager, $storeManager, $filesystem, $uploaderFactory, $adapterFactory);
  62. $this->imageFactory = $imageFactory;
  63. }
  64. /**
  65. * Retrieve product image
  66. *
  67. * @param \Magento\Catalog\Model\Product $product
  68. * @param string $imageId
  69. * @return string
  70. */
  71. public function getProductImage($product, $imageId = 'mpsearch_image')
  72. {
  73. $imageUrl = $this->imageFactory->create()
  74. ->init($product, $imageId)
  75. ->getUrl();
  76. $baseMediaUrl = $this->getSearchMediaUrl();
  77. if (strpos($imageUrl, $baseMediaUrl) === 0) {
  78. $imageUrl = substr($imageUrl, strlen($baseMediaUrl));
  79. }
  80. return $imageUrl;
  81. }
  82. /**
  83. * @return string
  84. */
  85. public function getSearchMediaUrl()
  86. {
  87. return $this->getBaseMediaUrl() . '/catalog/product/';
  88. }
  89. /**
  90. * @param $fileName
  91. * @param $content
  92. */
  93. public function createJsFile($fileName, $content)
  94. {
  95. try {
  96. $this->mediaDirectory->writeFile($fileName, $content);
  97. } catch (\Exception $e) {
  98. $this->_logger->critical($e->getMessage());
  99. }
  100. }
  101. /**
  102. * @return $this
  103. */
  104. public function removeJsPath()
  105. {
  106. $this->removePath(self::TEMPLATE_MEDIA_PATH);
  107. return $this;
  108. }
  109. }