コード例 #1
0
ファイル: Show.php プロジェクト: kidaa30/magento2-platformsh
 /**
  * Image upload action in iframe
  *
  * @return string
  */
 public function execute()
 {
     try {
         $uploader = $this->uploaderFactory->create(['fileId' => 'datafile']);
         $uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
         /** @var \Magento\Framework\Image\Adapter\AdapterInterface $imageAdapter */
         $imageAdapter = $this->adapterFactory->create();
         $uploader->addValidateCallback('catalog_product_image', $imageAdapter, 'validateUploadFile');
         $uploader->setAllowRenameFiles(true);
         $uploader->setFilesDispersion(true);
         /** @var \Magento\Framework\Filesystem\Directory\Read $mediaDirectory */
         $mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
         $config = $this->config;
         $result = $uploader->save($mediaDirectory->getAbsolutePath($config->getBaseTmpMediaPath()));
         $this->_eventManager->dispatch('swatch_gallery_upload_image_after', ['result' => $result, 'action' => $this]);
         unset($result['tmp_name']);
         unset($result['path']);
         $result['url'] = $this->config->getTmpMediaUrl($result['file']);
         $result['file'] = $result['file'] . '.tmp';
         $newFile = $this->swatchHelper->moveImageFromTmp($result['file']);
         $this->swatchHelper->generateSwatchVariations($newFile);
         $fileData = ['swatch_path' => $this->swatchHelper->getSwatchMediaUrl(), 'file_path' => $newFile];
         $this->getResponse()->setBody(json_encode($fileData));
     } catch (\Exception $e) {
         $result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
         $this->getResponse()->setBody(json_encode($result));
     }
 }
コード例 #2
0
 public function testGenerateSwatchVariations()
 {
     $this->mediaDirectoryMock->expects($this->atLeastOnce())->method('getAbsolutePath')->willReturn('attribute/swatch/e/a/earth.png');
     $image = $this->getMock('\\Magento\\Framework\\Image', ['resize', 'save', 'keepTransparency', 'constrainOnly', 'keepFrame', 'keepAspectRatio', 'backgroundColor', 'quality'], [], '', false);
     $this->imageFactoryMock->expects($this->any())->method('create')->willReturn($image);
     $this->generateImageConfig();
     $image->expects($this->any())->method('resize')->will($this->returnSelf());
     $this->mediaHelperObject->generateSwatchVariations('/e/a/earth.png');
 }