Media.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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_Core
  18. * @copyright Copyright (c) 2016-2018 Mageplaza (http://www.mageplaza.com/)
  19. * @license https://www.mageplaza.com/LICENSE.txt
  20. */
  21. namespace Mageplaza\Core\Helper;
  22. use Magento\Framework\App\Filesystem\DirectoryList;
  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\Framework\UrlInterface;
  28. use Magento\MediaStorage\Model\File\UploaderFactory;
  29. use Magento\Store\Model\StoreManagerInterface;
  30. use Psr\Log\LoggerInterface;
  31. /**
  32. * Class Media
  33. * @package Mageplaza\Core\Helper
  34. */
  35. class Media extends AbstractData
  36. {
  37. const TEMPLATE_MEDIA_PATH = 'mageplaza';
  38. /**
  39. * @var \Magento\Framework\Filesystem\Directory\ReadInterface
  40. */
  41. protected $mediaDirectory;
  42. /**
  43. * @var \Magento\MediaStorage\Model\File\UploaderFactory
  44. */
  45. protected $uploaderFactory;
  46. /**
  47. * @var \Magento\Framework\Image\AdapterFactory
  48. */
  49. protected $imageFactory;
  50. /**
  51. * Media constructor.
  52. * @param Context $context
  53. * @param ObjectManagerInterface $objectManager
  54. * @param StoreManagerInterface $storeManager
  55. * @param Filesystem $filesystem
  56. * @param UploaderFactory $uploaderFactory
  57. * @param AdapterFactory $imageFactory
  58. * @throws \Magento\Framework\Exception\FileSystemException
  59. */
  60. public function __construct(
  61. Context $context,
  62. ObjectManagerInterface $objectManager,
  63. StoreManagerInterface $storeManager,
  64. Filesystem $filesystem,
  65. UploaderFactory $uploaderFactory,
  66. AdapterFactory $imageFactory
  67. )
  68. {
  69. parent::__construct($context, $objectManager, $storeManager);
  70. $this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
  71. $this->uploaderFactory = $uploaderFactory;
  72. $this->imageFactory = $imageFactory;
  73. }
  74. /**
  75. * @param $data
  76. * @param string $fileName
  77. * @param string $type
  78. * @param null $oldImage
  79. * @return $this
  80. * @throws \Magento\Framework\Exception\FileSystemException
  81. */
  82. public function uploadImage(&$data, $fileName = 'image', $type = '', $oldImage = null)
  83. {
  84. if (isset($data[$fileName]) && isset($data[$fileName]['delete']) && $data[$fileName]['delete']) {
  85. if ($oldImage) {
  86. $this->removeImage($oldImage, $type);
  87. }
  88. $data['image'] = '';
  89. } else {
  90. try {
  91. $uploader = $this->uploaderFactory->create(['fileId' => $fileName]);
  92. $uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
  93. $uploader->setAllowRenameFiles(true);
  94. $uploader->setFilesDispersion(true);
  95. $uploader->setAllowCreateFolders(true);
  96. $path = $this->getBaseMediaPath($type);
  97. $image = $uploader->save(
  98. $this->mediaDirectory->getAbsolutePath($path)
  99. );
  100. if ($oldImage) {
  101. $this->removeImage($oldImage, $type);
  102. }
  103. $data['image'] = $this->_prepareFile($image['file']);
  104. } catch (\Exception $e) {
  105. $data['image'] = isset($data['image']['value']) ? $data['image']['value'] : '';
  106. }
  107. }
  108. return $this;
  109. }
  110. /**
  111. * @param $file
  112. * @param $type
  113. * @return $this
  114. * @throws \Magento\Framework\Exception\FileSystemException
  115. */
  116. public function removeImage($file, $type)
  117. {
  118. $image = $this->getMediaPath($file, $type);
  119. if ($this->mediaDirectory->isFile($image)) {
  120. $this->mediaDirectory->delete($image);
  121. }
  122. return $this;
  123. }
  124. /**
  125. * @param $file
  126. * @param string $type
  127. * @return string
  128. */
  129. public function getMediaPath($file, $type = '')
  130. {
  131. return $this->getBaseMediaPath($type) . '/' . $this->_prepareFile($file);
  132. }
  133. /**
  134. * @param string $type
  135. * @return string
  136. */
  137. public function getBaseMediaPath($type = '')
  138. {
  139. return trim(static::TEMPLATE_MEDIA_PATH . '/' . $type, '/');
  140. }
  141. /**
  142. * @param string $file
  143. * @return string
  144. */
  145. protected function _prepareFile($file)
  146. {
  147. return ltrim(str_replace('\\', '/', $file), '/');
  148. }
  149. /**
  150. * @param $file
  151. * @param $size
  152. * @param string $type
  153. * @param bool $keepRatio
  154. * @return string
  155. */
  156. public function resizeImage($file, $size, $type = '', $keepRatio = true)
  157. {
  158. $image = $this->getMediaPath($file, $type);
  159. if (!($imageSize = $this->correctImageSize($size))) {
  160. return $this->getMediaUrl($image);
  161. }
  162. list($width, $height) = $imageSize;
  163. $resizeImage = $this->getMediaPath($file, ($type ? $type . '/' : '') . 'resize/' . $width . 'x' . $height);
  164. /** @var \Magento\Framework\Filesystem\Directory\WriteInterface $mediaDirectory */
  165. $mediaDirectory = $this->getMediaDirectory();
  166. if (!$mediaDirectory->isFile($resizeImage)) {
  167. try {
  168. $imageResize = $this->imageFactory->create();
  169. $imageResize->open($mediaDirectory->getAbsolutePath($image));
  170. $imageResize->constrainOnly(true);
  171. $imageResize->keepTransparency(true);
  172. $imageResize->keepFrame(false);
  173. $imageResize->keepAspectRatio($keepRatio);
  174. $imageResize->resize($width, $height);
  175. $imageResize->save($mediaDirectory->getAbsolutePath($resizeImage));
  176. $image = $resizeImage;
  177. } catch (\Exception $e) {
  178. $this->objectManager->get(LoggerInterface::class)->critical($e->getMessage());
  179. }
  180. } else {
  181. $image = $resizeImage;
  182. }
  183. return $this->getMediaUrl($image);
  184. }
  185. /**
  186. * @param $size
  187. * @return array|bool
  188. */
  189. protected function correctImageSize($size)
  190. {
  191. if (!$size) {
  192. return false;
  193. }
  194. if (strpos($size, 'x') === false) {
  195. $width = $height = (int)$size;
  196. } else {
  197. list($width, $height) = explode('x', $size);
  198. }
  199. if (!$width && !$height) {
  200. return false;
  201. }
  202. return [(int)$width ?: null, (int)$height ?: null];
  203. }
  204. /**
  205. * @param string $file
  206. * @return string
  207. */
  208. public function getMediaUrl($file)
  209. {
  210. return $this->getBaseMediaUrl() . '/' . $this->_prepareFile($file);
  211. }
  212. /**
  213. * @return string
  214. */
  215. public function getBaseMediaUrl()
  216. {
  217. return rtrim($this->storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_MEDIA), '/');
  218. }
  219. /**
  220. * @return \Magento\Framework\Filesystem\Directory\WriteInterface
  221. */
  222. public function getMediaDirectory()
  223. {
  224. return $this->mediaDirectory;
  225. }
  226. /**
  227. * @param $path
  228. * @return $this
  229. * @throws \Magento\Framework\Exception\FileSystemException
  230. */
  231. public function removePath($path)
  232. {
  233. $pathMedia = $this->mediaDirectory->getRelativePath($path);
  234. if ($this->mediaDirectory->isDirectory($pathMedia)) {
  235. $this->mediaDirectory->delete($path);
  236. }
  237. return $this;
  238. }
  239. }