Skip to content

Add methods to Resizer #13

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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
66 changes: 58 additions & 8 deletions Model/Resizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,27 +114,38 @@ public function __construct(
}

/**
* Resized image and return url
* - Return original image url if no success
* Resize image
*
* @param string $imageUrl
* @param null|int $width
* @param null|int $height
* @param array $resizeSettings
* @return bool|string
* @return self
*/
public function resizeAndGetUrl(string $imageUrl, $width, $height, array $resizeSettings = [])
public function resize(string $imageUrl, $width, $height, array $resizeSettings = [])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to refactor this method. I should get rid of all these init methods that create temporal coupling, which should be avoided. I would try to improve that after merging your PR.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be nice if it would return value objects or something, right?

{
// Set $resultUrl with $fileUrl to return this one in case the resize fails.
$resultUrl = $imageUrl;
$this->initRelativeFilenameFromUrl($imageUrl);
if (!$this->relativeFilename) {
return $resultUrl;
return $this;
}

$this->initSize($width, $height);
$this->initResizeSettings($resizeSettings);

return $this;
}

/**
* Return url
* - Return default value if no success
*
* @param string $default
* @return string
*/
public function getUrl($default = '')
{
$resultUrl = '';

try {
// Check if resized image already exists in cache
$resizedUrl = $this->getResizedImageUrl();
Expand All @@ -150,7 +161,42 @@ public function resizeAndGetUrl(string $imageUrl, $width, $height, array $resize
$this->logger->addError("Staempfli_ImageResizer: could not resize image: \n" . $e->getMessage());
}

return $resultUrl;
return $resultUrl ?: $default;
}

/**
* Return relative path of resized image
*
* @return string
*/
public function getRelativePath()
{
return $this->getRelativePathResizedImage();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just make the original method public. IMHO it does not make much sense to wrap it inside another method, just to make it public

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I just thought this was a better public method name for it. This way the internal one could get deprecated in time.

}

/**
* Return absolute path of resized image
*
* @return string
*/
public function getAbsolutePath()
{
return $this->getAbsolutePathResized();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as before. Just make the original method public

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I just thought this was a better public method name for it. This way the internal one could get deprecated in time.

}

/**
* Resize image and return url
* - Return original image url if no success
*
* @param string $imageUrl
* @param null|int $width
* @param null|int $height
* @param array $resizeSettings
* @return bool|string
*/
public function resizeAndGetUrl(string $imageUrl, $width, $height, array $resizeSettings = [])
{
return $this->resize($imageUrl, $width, $height, $resizeSettings)->getUrl($imageUrl);
}

/**
Expand Down Expand Up @@ -216,6 +262,7 @@ protected function getResizeSubFolderName()
$subPath .= "_" . $this->subPathSettingsMapping[$key];
}
}

return sprintf('%s_%s',$subPath, $this->resizeSettings['quality']);
}

Expand All @@ -235,6 +282,7 @@ protected function getRelativePathResizedImage()
$this->getResizeSubFolderName(),
$pathInfo['basename']
];

return implode(DIRECTORY_SEPARATOR, $relativePathParts);
}

Expand Down Expand Up @@ -269,6 +317,7 @@ protected function getResizedImageUrl()
if ($this->mediaDirectoryRead->isFile($relativePath)) {
return $this->storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_MEDIA) . $relativePath;
}

return false;
}

Expand All @@ -293,6 +342,7 @@ protected function resizeAndSaveImage()
$imageAdapter->quality($this->resizeSettings['quality']);
$imageAdapter->resize($this->width, $this->height);
$imageAdapter->save($this->getAbsolutePathResized());

return true;
}
}