Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MediaGallery.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Mtf\Fixture\DataSource;
10 
18 class MediaGallery extends DataSource
19 {
25  protected $mediaPathTmp = '/pub/media/tmp/catalog/product';
26 
33  public function __construct(array $params, $data = [])
34  {
35  if (isset($data['images']) && is_array($data['images']) && count($data['images'])) {
36  foreach ($data['images'] as $media) {
37  if (isset($media['file'])) {
38  $this->createTestImage($media['file']);
39  }
40  }
41  }
42  $this->data = $data;
43  }
44 
51  protected function createTestImage($filename)
52  {
53  $filename = $this->getFullPath($filename);
54  if (!file_exists($filename)) {
55  // Create an image with the specified dimensions
56  $image = imageCreate(300, 200);
57 
58  // Create a color (this first call to imageColorAllocate
59  // also automatically sets the image background color)
60  $colorYellow = imageColorAllocate($image, 255, 255, 0);
61 
62  // Draw a rectangle
63  imageFilledRectangle($image, 50, 50, 250, 150, $colorYellow);
64 
65  $directory = dirname($filename);
66  if (!file_exists($directory)) {
67  mkdir($directory, 0777, true);
68  }
69  imageJpeg($image, $filename);
70 
71  // Release memory
72  imageDestroy($image);
73  }
74  }
75 
82  protected function getFullPath($filename)
83  {
84  return BP . $this->mediaPathTmp . $filename;
85  }
86 }
const BP
Definition: autoload.php:14
mkdir($pathname, $mode=0777, $recursive=false, $context=null)
Definition: ioMock.php:25
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18