Thumbnail.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Cms\Controller\Adminhtml\Wysiwyg\Images;
  8. use Magento\Backend\App\Action;
  9. class Thumbnail extends \Magento\Cms\Controller\Adminhtml\Wysiwyg\Images
  10. {
  11. /**
  12. * @var \Magento\Framework\Controller\Result\RawFactory
  13. */
  14. protected $resultRawFactory;
  15. /**
  16. * @param Action\Context $context
  17. * @param \Magento\Framework\Registry $coreRegistry
  18. * @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
  19. */
  20. public function __construct(
  21. Action\Context $context,
  22. \Magento\Framework\Registry $coreRegistry,
  23. \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
  24. ) {
  25. $this->resultRawFactory = $resultRawFactory;
  26. parent::__construct($context, $coreRegistry);
  27. }
  28. /**
  29. * Generate image thumbnail on the fly
  30. *
  31. * @return \Magento\Framework\Controller\Result\Raw
  32. */
  33. public function execute()
  34. {
  35. $file = $this->getRequest()->getParam('file');
  36. $file = $this->_objectManager->get(\Magento\Cms\Helper\Wysiwyg\Images::class)->idDecode($file);
  37. $thumb = $this->getStorage()->resizeOnTheFly($file);
  38. /** @var \Magento\Framework\Controller\Result\Raw $resultRaw */
  39. $resultRaw = $this->resultRawFactory->create();
  40. if ($thumb !== false) {
  41. /** @var \Magento\Framework\Image\Adapter\AdapterInterface $image */
  42. $image = $this->_objectManager->get(\Magento\Framework\Image\AdapterFactory::class)->create();
  43. $image->open($thumb);
  44. $resultRaw->setHeader('Content-Type', $image->getMimeType());
  45. $resultRaw->setContents($image->getImage());
  46. return $resultRaw;
  47. } else {
  48. // todo: generate some placeholder
  49. }
  50. }
  51. }