Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ImageBuilder.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
10 use Magento\Catalog\Helper\ImageFactory as HelperFactory;
12 
18 {
22  protected $imageFactory;
23 
27  protected $helperFactory;
28 
32  protected $product;
33 
37  protected $imageId;
38 
42  protected $attributes = [];
43 
48  public function __construct(
49  HelperFactory $helperFactory,
51  ) {
52  $this->helperFactory = $helperFactory;
53  $this->imageFactory = $imageFactory;
54  }
55 
62  public function setProduct(Product $product)
63  {
64  $this->product = $product;
65  return $this;
66  }
67 
74  public function setImageId($imageId)
75  {
76  $this->imageId = $imageId;
77  return $this;
78  }
79 
86  public function setAttributes(array $attributes)
87  {
88  $this->attributes = $attributes;
89  return $this;
90  }
91 
97  protected function getCustomAttributes()
98  {
99  $result = [];
100  foreach ($this->attributes as $name => $value) {
101  $result[] = $name . '="' . $value . '"';
102  }
103  return !empty($result) ? implode(' ', $result) : '';
104  }
105 
112  protected function getRatio(\Magento\Catalog\Helper\Image $helper)
113  {
114  $width = $helper->getWidth();
115  $height = $helper->getHeight();
116  if ($width && $height) {
117  return $height / $width;
118  }
119  return 1;
120  }
121 
130  public function create(Product $product = null, string $imageId = null, array $attributes = null)
131  {
135  return $this->imageFactory->create($product, $imageId, $attributes);
136  }
137 }
$helper
Definition: iframe.phtml:13
create(Product $product=null, string $imageId=null, array $attributes=null)
getRatio(\Magento\Catalog\Helper\Image $helper)
$value
Definition: gender.phtml:16
__construct(HelperFactory $helperFactory, ImageFactory $imageFactory)
if(!isset($_GET['name'])) $name
Definition: log.php:14