Skip to content

Commit

Permalink
Merge pull request #32 from websNL/craft-2
Browse files Browse the repository at this point in the history
Crop Non-destructive functionality improved
  • Loading branch information
svenjungnickel committed Jan 11, 2018
2 parents 1e27857 + d6df466 commit 90022af
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 19 deletions.
8 changes: 8 additions & 0 deletions changelog.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
[
{
"version": "1.0.2",
"downloadUrl": "https://github.com/engram-design/ImageResizer/archive/1.0.2.zip",
"date": "2017-11-24T00:00:00+10:00",
"notes": [
"[Improved] Non-destructive on Cropping now copies the original asset, instead of just the file."
]
},
{
"version": "1.0.1",
"downloadUrl": "https://github.com/engram-design/ImageResizer/archive/1.0.1.zip",
Expand Down
2 changes: 1 addition & 1 deletion imageresizer/ImageResizerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function getName()

public function getVersion()
{
return '1.0.1';
return '1.0.2';
}

public function getSchemaVersion()
Expand Down
43 changes: 26 additions & 17 deletions imageresizer/services/ImageResizer_CropService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,32 @@ public function crop($asset, $x1, $x2, $y1, $y2)
} else {
$path = $sourceType->getImageSourcePath($asset);
}
$folder = $asset->folder;

$folder = $asset->folder;
$fileName = $asset->filename;

// Check to see if we shouldn't overwrite the original image
$settings = craft()->imageResizer->getSettings();
if ($settings->nonDestructiveCrop) {

// Determine cropped name
$cropFilename = basename($path);
$cropFilename = explode('.', $cropFilename);
$cropFilename[ count($cropFilename) - 2 ] .= '_cropped';
$cropFilename = implode('.', $cropFilename);

// To make sure we don't trigger resizing in the below `assets.onBeforeUploadAsset` hook
craft()->httpSession->add('ImageResizer_CropElementAction', true);

// Copy original to cropped version
craft()->assets->insertFileByLocalPath($path, $cropFilename, $folder->id, AssetConflictResolution::Replace);

// Change path / filename for cropped version to be cropped
$sourceFilename = basename($path);
$path = str_replace($sourceFilename, $cropFilename, $path);
$fileName = $cropFilename;
}

// Perform the actual cropping
$this->_cropWithPath($path, $x1, $x2, $y1, $y2);

Expand All @@ -43,25 +65,12 @@ private function _cropWithPath($path, $x1, $x2, $y1, $y2)
$image = craft()->images->loadImage($path);
$filename = basename($path);

// Check to see if we should make a copy of our original image first?
if ($settings->nonDestructiveCrop) {
$folderPath = str_replace($filename, '', $path) . 'originals/';
IOHelper::ensureFolderExists($folderPath);

$filePath = $folderPath . $filename;

// Only copy the original if there's not already one created
if (!IOHelper::fileExists($filePath)) {
IOHelper::copyFile($path, $filePath);
}
}

// Make sure that image quality isn't messed with for cropping
$image->setQuality(craft()->imageResizer->getImageQuality($filename, 100));

// Do the cropping
$image->crop($x1, $x2, $y1, $y2);

craft()->imageResizer->saveAs($image, $path);

return true;
Expand All @@ -72,4 +81,4 @@ private function _cropWithPath($path, $x1, $x2, $y1, $y2)
}
}

}
}
2 changes: 1 addition & 1 deletion imageresizer/templates/settings/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ <h2>{{ "{label} Asset Source Settings" | t({ label: item.label }) }}</h2>
<div class="field">
{{ forms.lightswitchField({
label: 'Non-destructive' | t,
instructions: 'Image Resizer will save a copy of your original image, untouched. This will be in a folder called `originals`, relative to the source image.' | t,
instructions: 'Image Resizer will make a copy of your original image and save it with _cropped after the filename. The asset will also be copied with Cropped after the name and will be available in the folder relative to the source image.' | t,
id: 'nonDestructiveCrop',
name: 'nonDestructiveCrop',
on: settings.nonDestructiveCrop,
Expand Down

0 comments on commit 90022af

Please sign in to comment.