Skip to content
Closed
Show file tree
Hide file tree
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
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand All @@ -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

Expand Down Expand Up @@ -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:

Expand All @@ -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',
]
````

Expand Down Expand Up @@ -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"
php artisan stapler:refresh TestPhoto --attachments="foo, bar, baz, etc"
4 changes: 2 additions & 2 deletions src/Codesleeve/Stapler/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
56 changes: 28 additions & 28 deletions src/Codesleeve/Stapler/File/Image/Resizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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('/^(?<width>\d+)?(?:x(?<height>\d+))?(?<resizing>[#!])?(?:@(?<quality>\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];
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/config/stapler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down