Skip to content

[Catalog] [MediaStorage] Fix watermark in media application #21338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 4, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 48 additions & 7 deletions app/code/Magento/MediaStorage/Service/ImageResize.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
use Magento\Framework\App\Filesystem\DirectoryList;

/**
* Image resize service.
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ImageResize
Expand Down Expand Up @@ -123,7 +125,8 @@ public function __construct(
}

/**
* Create resized images of different sizes from an original image
* Create resized images of different sizes from an original image.
*
* @param string $originalImageName
* @throws NotFoundException
*/
Expand All @@ -141,7 +144,8 @@ public function resizeFromImageName(string $originalImageName)
}

/**
* Create resized images of different sizes from themes
* Create resized images of different sizes from themes.
*
* @param array|null $themes
* @return \Generator
* @throws NotFoundException
Expand Down Expand Up @@ -169,7 +173,8 @@ public function resizeFromThemes(array $themes = null): \Generator
}

/**
* Search the current theme
* Search the current theme.
*
* @return array
*/
private function getThemesInUse(): array
Expand All @@ -187,7 +192,8 @@ private function getThemesInUse(): array
}

/**
* Get view images data from themes
* Get view images data from themes.
*
* @param array $themes
* @return array
*/
Expand All @@ -211,7 +217,8 @@ private function getViewImages(array $themes): array
}

/**
* Get unique image index
* Get unique image index.
*
* @param array $imageData
* @return string
*/
Expand All @@ -223,7 +230,8 @@ private function getUniqueImageIndex(array $imageData): string
}

/**
* Make image
* Make image.
*
* @param string $originalImagePath
* @param array $imageParams
* @return Image
Expand All @@ -241,7 +249,8 @@ private function makeImage(string $originalImagePath, array $imageParams): Image
}

/**
* Resize image
* Resize image.
*
* @param array $viewImage
* @param string $originalImagePath
* @param string $originalImageName
Expand All @@ -257,9 +266,41 @@ private function resize(array $viewImage, string $originalImagePath, string $ori
]
);

if (isset($imageParams['watermark_file'])) {
if ($imageParams['watermark_height'] !== null) {
$image->setWatermarkHeight($imageParams['watermark_height']);
}

if ($imageParams['watermark_width'] !== null) {
$image->setWatermarkWidth($imageParams['watermark_width']);
}

if ($imageParams['watermark_position'] !== null) {
$image->setWatermarkPosition($imageParams['watermark_position']);
}

if ($imageParams['watermark_image_opacity'] !== null) {
$image->setWatermarkImageOpacity($imageParams['watermark_image_opacity']);
}

$image->watermark($this->getWatermarkFilePath($imageParams['watermark_file']));
}

if ($imageParams['image_width'] !== null && $imageParams['image_height'] !== null) {
$image->resize($imageParams['image_width'], $imageParams['image_height']);
}
$image->save($imageAsset->getPath());
}

/**
* Returns watermark file absolute path
*
* @param string $file
* @return string
*/
private function getWatermarkFilePath($file)
{
$path = $this->imageConfig->getMediaPath('/watermark/' . $file);
return $this->mediaDirectory->getAbsolutePath($path);
}
}