/** * @param Asset $asset * @return string */ public function handleSave(Asset $asset) { $newImage = new \Imagick($asset->getUploadedFile()->getRealPath()); $asset->setHeight($newImage->getImageHeight()); $asset->setWidth($newImage->getImageWidth()); /** @var array $requiredImage */ foreach ($this->requiredImages as $requiredImage) { $newImage = new \Imagick($asset->getUploadedFile()->getRealPath()); $newFilename = pathinfo($asset->getFilename(), PATHINFO_FILENAME) . '-' . $requiredImage['name'] . '.' . pathinfo($asset->getFilename(), PATHINFO_EXTENSION); $imageWidth = $newImage->getImageWidth(); $imageHeight = $newImage->getImageHeight(); $isLandscapeFormat = $imageWidth > $imageHeight; $orientation = $newImage->getImageOrientation(); $newImage->setCompression(\Imagick::COMPRESSION_JPEG); $newImage->setImageCompressionQuality($requiredImage['quality']); if ($isLandscapeFormat) { $desiredWidth = $requiredImage['long']; $newImage->resizeImage($desiredWidth, 0, \Imagick::FILTER_LANCZOS, 1); if (isset($requiredImage['short']) && boolval($requiredImage['crop']) == true) { $newImage->cropImage($desiredWidth, $requiredImage['short'], 0, 0); } else { $newImage->resizeImage(0, $requiredImage['short'], \Imagick::FILTER_LANCZOS, 1); } } else { $desiredHeight = $requiredImage['long']; $newImage->resizeImage(0, $desiredHeight, \Imagick::FILTER_LANCZOS, 1); if (isset($requiredImage['short']) && boolval($requiredImage['crop']) == true) { $newImage->cropImage($requiredImage['short'], $desiredHeight, 0, 0); } else { $newImage->resizeImage($requiredImage['short'], 0, \Imagick::FILTER_LANCZOS, 1); } } /** * This unfortunately kills the orientation. Leave EXIF-Info for now. * * $newImage->stripImage(); * $newImage->setImageOrientation($orientation); */ $this->assetStorage->uploadFile($newFilename, $newImage); $subAsset = new SubAsset(); $subAsset->setFilename($newFilename); $subAsset->setType($requiredImage['name']); $subAsset->setHeight($newImage->getImageHeight()); $subAsset->setWidth($newImage->getImageWidth()); $asset->addSubAsset($subAsset); } }
/** * @param Asset $asset * @return string */ public function handleSave(Asset $asset) { /** @var SubAsset $subAsset */ foreach ($asset->getSubAssets() as $subAsset) { $this->entityManager->remove($subAsset, false); } $this->entityManager->flush(); /** If it's the same field, skip initializing and uploading it. */ if ($asset->getOriginalFileName() !== $asset->getUploadedFile()->getClientOriginalName()) { $asset->setOriginalFileName($asset->getUploadedFile()->getClientOriginalName()); $ext = $asset->getUploadedFile()->guessExtension(); $asset->setFileType($ext); $key = substr(sha1(uniqid()), 0, 15); $newFilename = $key . "." . $ext; $asset->setFilename($newFilename); $this->assetStorage->uploadAsset($asset); } }
/** * @param Asset $asset * @return ResultInterface */ public function uploadAsset(Asset $asset) { $asset->setPath($this->basePath . "/"); return $this->s3Client->upload($this->bucket, $asset->getPath() . $asset->getFilename(), file_get_contents($asset->getUploadedFile()->getRealPath())); }
/** * @param Asset $asset * @return bool */ public function uploadAsset(Asset $asset) { $asset->getUploadedFile()->move($this->basePath . $asset->getPath(), $asset->getFilename()); return true; }