diff --git a/README.md b/README.md index 19fbc7d..d50fd13 100644 --- a/README.md +++ b/README.md @@ -163,6 +163,7 @@ The following configuration settings apply to stapler in general. is always stored within the 'original' style, however the default_style can be set to point to any of the defined syles within the styles array. * **styles**: An array of image sizes defined for the file attachment. Stapler will attempt to use to format the file upload into the defined style. +* **default_image_quality**: The default image quality used when generating defined image styles. * **keep_old_files**: Set this to true in order to prevent older file uploads from being deleted from the file system when a record is updated. * **preserve_old_files**: Set this to true in order to prevent an attachment's file uploads from being deleted from the file system when an the attachment object is destroyed (attachment's are destroyed when their corresponding mondels are deleted/destroyed from the database). @@ -172,6 +173,7 @@ Default values: * **default_url**: '/:attachment/:style/missing.png' * **default_style**: 'original' * **styles**: [] +* **default_image_quality**: 75 * **keep_old_files**: false * **preserve_old_files**: false @@ -239,7 +241,7 @@ Stapler makes use of the [imagine image](https://packagist.org/packages/imagine/ * **xheight**: A style that defines a heigh only (portrait). Width automagically selected to preserve aspect ratio. * **widthxheight#**: Resize then crop. * **widthxheight!**: Resize by exacty width and height. Width and height emphatically given, original aspect ratio will be ignored. -* **widthxheight**: Auto determine both width and height when resizing. This will resize as close as possible to the given dimensions while still preserving the original aspect ratio. +* **widthxheight@95**: Auto determine both width and height when resizing. This will resize as close as possible to the given dimensions while still preserving the original aspect ratio. Image quality is set to 95%. To create styles for an attachment, simply define them (you may use any style name you like: foo, bar, baz, etc) inside the attachment's styles array using a combination of the directives defined above: @@ -250,7 +252,8 @@ To create styles for an attachment, simply define them (you may use any style na 'landscape' => '150', 'portrait' => 'portrait' => 'x150', 'foo' => '75x75', - 'fooCropped' => '75x75#' + 'fooCropped' => '75x75#', + 'fooQuality' => '75x75@100', ] ```` @@ -454,4 +457,4 @@ Reprocess only the photo attachment on the ProfilePicture model: php artisan stapler:refresh TestPhoto --attachments="photo" Reprocess a list of attachments on the ProfilePicture model: -php artisan stapler:refresh TestPhoto --attachments="foo, bar, baz, etc" \ No newline at end of file +php artisan stapler:refresh TestPhoto --attachments="foo, bar, baz, etc" diff --git a/src/Codesleeve/Stapler/Attachment.php b/src/Codesleeve/Stapler/Attachment.php index c0bb7e3..8b3f5a1 100644 --- a/src/Codesleeve/Stapler/Attachment.php +++ b/src/Codesleeve/Stapler/Attachment.php @@ -507,8 +507,8 @@ protected function flushWrites() { foreach ($this->queuedForWrite as $style) { - if ($style->value && $this->uploadedFile->isImage()) { - $file = $this->resizer->resize($this->uploadedFile, $style); + if ($style->value && $this->uploadedFile->isImage()) { + $file = $this->resizer->resize($this->uploadedFile, $style, $this->default_image_quality); } else { $file = $this->uploadedFile->getRealPath(); diff --git a/src/Codesleeve/Stapler/File/Image/Resizer.php b/src/Codesleeve/Stapler/File/Image/Resizer.php index 9de125f..cac2dbf 100644 --- a/src/Codesleeve/Stapler/File/Image/Resizer.php +++ b/src/Codesleeve/Stapler/File/Image/Resizer.php @@ -26,21 +26,23 @@ function __construct($imagine) { * * @param UploadedFile $file * @param stdClass $style + * @param int $defaultQuality * @return void */ - public function resize($file, $style) + public function resize($file, $style, $defaultQuality = null) { $filePath = tempnam(sys_get_temp_dir(), 'STP') . '.' . $file->getFilename(); - list($width, $height, $option) = $this->parseStyleDimensions($style); + $defaultQuality = is_numeric($defaultQuality) ? $defaultQuality : 75; + list($width, $height, $option, $quality) = $this->parseStyleDimensions($style, $defaultQuality); $method = "resize" . ucfirst($option); if ($method == 'resizeCustom') { $this->resizeCustom($file, $style->value) - ->save($filePath); + ->save($filePath, array('quality' => $quality)); } - else { - $this->$method($file, $width, $height) - ->save($filePath); + else { + $this->$method($file, $width, $height) + ->save($filePath, array('quality' => $quality)); } return $filePath; @@ -53,52 +55,50 @@ public function resize($file, $style) * perform any necessary image resizing for a given style. * * @param stdClass $style + * @param int $defaultQuality * @return array */ - protected function parseStyleDimensions($style) - { + protected function parseStyleDimensions($style, $defaultQuality) + { if (is_callable($style->value)) { - return [null, null, 'custom']; + return [null, null, 'custom', $defaultQuality]; } - if (strpos($style->value, 'x') === false) + preg_match('/^(?\d+)?(?:x(?\d+))?(?[#!])?(?:@(?\d+))?$/i', $style->value, $matches); + + $width = empty($matches['width']) ? null : $matches['width']; + $height = empty($matches['height']) ? null : $matches['height']; + + $qualityOption = empty($matches['quality']) ? $defaultQuality : $matches['quality']; + + if (!$height) { // Width given, height automagically selected to preserve aspect ratio (landscape). - $width = $style->value; - - return [$width, null, 'landscape']; + return [$width, null, 'landscape', $qualityOption]; } - $dimensions = explode('x', $style->value); - $width = $dimensions[0]; - $height = $dimensions[1]; - - if (empty($width)) - { + if (!$width) + { // Height given, width automagically selected to preserve aspect ratio (portrait). - return [null, $height, 'portrait']; + return [null, $height, 'portrait', $qualityOption]; } - $resizingOption = substr($height, -1, 1); + $resizingOption = empty($matches['resizing']) ? false : $matches['resizing']; if ($resizingOption == '#') { // Resize, then crop. - $height = rtrim($height, '#'); - - return [$width, $height, 'crop']; + return [$width, $height, 'crop', $qualityOption]; } if ($resizingOption == '!') { // Resize by exact width/height (does not preserve aspect ratio). - $height = rtrim($height, '!'); - - return [$width, $height, 'exact']; + return [$width, $height, 'exact', $qualityOption]; } // Let the script decide the best way to resize. - return [$width, $height, 'auto']; + return [$width, $height, 'auto', $qualityOption]; } /** diff --git a/src/config/stapler.php b/src/config/stapler.php index 7143573..f0b494c 100644 --- a/src/config/stapler.php +++ b/src/config/stapler.php @@ -78,6 +78,17 @@ 'styles' => [], + /* + |-------------------------------------------------------------------------- + | Stapler Default Image Quality + |-------------------------------------------------------------------------- + | + | The default image quality used when generating defined image styles. + | + */ + + 'default_image_quality' => 75, + /* |-------------------------------------------------------------------------- | Keep Old Files Flag