-
Notifications
You must be signed in to change notification settings - Fork 33
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 = []) | ||
{ | ||
// 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(); | ||
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as before. Just make the original method public There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
|
||
/** | ||
|
@@ -216,6 +262,7 @@ protected function getResizeSubFolderName() | |
$subPath .= "_" . $this->subPathSettingsMapping[$key]; | ||
} | ||
} | ||
|
||
return sprintf('%s_%s',$subPath, $this->resizeSettings['quality']); | ||
} | ||
|
||
|
@@ -235,6 +282,7 @@ protected function getRelativePathResizedImage() | |
$this->getResizeSubFolderName(), | ||
$pathInfo['basename'] | ||
]; | ||
|
||
return implode(DIRECTORY_SEPARATOR, $relativePathParts); | ||
} | ||
|
||
|
@@ -269,6 +317,7 @@ protected function getResizedImageUrl() | |
if ($this->mediaDirectoryRead->isFile($relativePath)) { | ||
return $this->storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_MEDIA) . $relativePath; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
|
@@ -293,6 +342,7 @@ protected function resizeAndSaveImage() | |
$imageAdapter->quality($this->resizeSettings['quality']); | ||
$imageAdapter->resize($this->width, $this->height); | ||
$imageAdapter->save($this->getAbsolutePathResized()); | ||
|
||
return true; | ||
} | ||
} |
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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?