Skip to content

Commit 90022af

Browse files
Merge pull request #32 from websNL/craft-2
Crop Non-destructive functionality improved
2 parents 1e27857 + d6df466 commit 90022af

File tree

4 files changed

+36
-19
lines changed

4 files changed

+36
-19
lines changed

changelog.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
[
2+
{
3+
"version": "1.0.2",
4+
"downloadUrl": "https://github.com/engram-design/ImageResizer/archive/1.0.2.zip",
5+
"date": "2017-11-24T00:00:00+10:00",
6+
"notes": [
7+
"[Improved] Non-destructive on Cropping now copies the original asset, instead of just the file."
8+
]
9+
},
210
{
311
"version": "1.0.1",
412
"downloadUrl": "https://github.com/engram-design/ImageResizer/archive/1.0.1.zip",

imageresizer/ImageResizerPlugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function getName()
1414

1515
public function getVersion()
1616
{
17-
return '1.0.1';
17+
return '1.0.2';
1818
}
1919

2020
public function getSchemaVersion()

imageresizer/services/ImageResizer_CropService.php

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,32 @@ public function crop($asset, $x1, $x2, $y1, $y2)
1515
} else {
1616
$path = $sourceType->getImageSourcePath($asset);
1717
}
18-
19-
$folder = $asset->folder;
18+
19+
$folder = $asset->folder;
2020
$fileName = $asset->filename;
2121

22+
// Check to see if we shouldn't overwrite the original image
23+
$settings = craft()->imageResizer->getSettings();
24+
if ($settings->nonDestructiveCrop) {
25+
26+
// Determine cropped name
27+
$cropFilename = basename($path);
28+
$cropFilename = explode('.', $cropFilename);
29+
$cropFilename[ count($cropFilename) - 2 ] .= '_cropped';
30+
$cropFilename = implode('.', $cropFilename);
31+
32+
// To make sure we don't trigger resizing in the below `assets.onBeforeUploadAsset` hook
33+
craft()->httpSession->add('ImageResizer_CropElementAction', true);
34+
35+
// Copy original to cropped version
36+
craft()->assets->insertFileByLocalPath($path, $cropFilename, $folder->id, AssetConflictResolution::Replace);
37+
38+
// Change path / filename for cropped version to be cropped
39+
$sourceFilename = basename($path);
40+
$path = str_replace($sourceFilename, $cropFilename, $path);
41+
$fileName = $cropFilename;
42+
}
43+
2244
// Perform the actual cropping
2345
$this->_cropWithPath($path, $x1, $x2, $y1, $y2);
2446

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

46-
// Check to see if we should make a copy of our original image first?
47-
if ($settings->nonDestructiveCrop) {
48-
$folderPath = str_replace($filename, '', $path) . 'originals/';
49-
IOHelper::ensureFolderExists($folderPath);
50-
51-
$filePath = $folderPath . $filename;
52-
53-
// Only copy the original if there's not already one created
54-
if (!IOHelper::fileExists($filePath)) {
55-
IOHelper::copyFile($path, $filePath);
56-
}
57-
}
58-
5968
// Make sure that image quality isn't messed with for cropping
6069
$image->setQuality(craft()->imageResizer->getImageQuality($filename, 100));
6170

6271
// Do the cropping
6372
$image->crop($x1, $x2, $y1, $y2);
64-
73+
6574
craft()->imageResizer->saveAs($image, $path);
6675

6776
return true;
@@ -72,4 +81,4 @@ private function _cropWithPath($path, $x1, $x2, $y1, $y2)
7281
}
7382
}
7483

75-
}
84+
}

imageresizer/templates/settings/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ <h2>{{ "{label} Asset Source Settings" | t({ label: item.label }) }}</h2>
9898
<div class="field">
9999
{{ forms.lightswitchField({
100100
label: 'Non-destructive' | t,
101-
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,
101+
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,
102102
id: 'nonDestructiveCrop',
103103
name: 'nonDestructiveCrop',
104104
on: settings.nonDestructiveCrop,

0 commit comments

Comments
 (0)