Image.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * Copyright © 2015-2017 Ihor Vansach (ihor@magefan.com). All rights reserved.
  4. * See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
  5. *
  6. * Glory to Ukraine! Glory to the heroes!
  7. */
  8. namespace Magefan\Blog\Model;
  9. use Magefan\Blog\Model\Url;
  10. use Magefan\Blog\Helper\Image as ImageHelper;
  11. /**
  12. * Image model
  13. *
  14. * @method string getFile()
  15. * @method $this setFile(string $value)
  16. */
  17. class Image extends \Magento\Framework\DataObject
  18. {
  19. /**
  20. * @var \Magefan\Blog\Model\Url
  21. */
  22. protected $url;
  23. /**
  24. * @var \Magefan\Blog\Helper\Image
  25. */
  26. protected $imageHelper;
  27. /**
  28. * Initialize dependencies.
  29. *
  30. * @param \Magento\Framework\Model\Context $context
  31. * @param \Magento\Framework\Registry $registry
  32. * @param \Magefan\Blog\Model\Url $url
  33. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  34. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  35. * @param array $data
  36. */
  37. public function __construct(
  38. Url $url,
  39. ImageHelper $imageHelper,
  40. array $data = []
  41. ) {
  42. parent::__construct($data);
  43. $this->url = $url;
  44. $this->imageHelper = $imageHelper;
  45. }
  46. /**
  47. * Retrieve image url
  48. * @return string
  49. */
  50. public function getUrl()
  51. {
  52. if ($this->getFile()) {
  53. return $this->url->getMediaUrl($this->getFile());
  54. }
  55. return null;
  56. }
  57. /**
  58. * Resize image
  59. * @param int $width
  60. * @param int $height
  61. * @return string
  62. */
  63. public function resize($width, $height = null)
  64. {
  65. return $this->imageHelper->init($this->getFile())
  66. ->resize($width, $height);
  67. }
  68. /**
  69. * Retrieve image url
  70. * @return string
  71. */
  72. public function __toString()
  73. {
  74. return $this->getUrl();
  75. }
  76. }