diff --git a/changelog.json b/changelog.json index ce842f1..a3ea526 100644 --- a/changelog.json +++ b/changelog.json @@ -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", diff --git a/imageresizer/ImageResizerPlugin.php b/imageresizer/ImageResizerPlugin.php index a1d5d65..bb45023 100644 --- a/imageresizer/ImageResizerPlugin.php +++ b/imageresizer/ImageResizerPlugin.php @@ -14,7 +14,7 @@ public function getName() public function getVersion() { - return '1.0.1'; + return '1.0.2'; } public function getSchemaVersion() diff --git a/imageresizer/services/ImageResizer_CropService.php b/imageresizer/services/ImageResizer_CropService.php index b81dc09..9bc87b9 100644 --- a/imageresizer/services/ImageResizer_CropService.php +++ b/imageresizer/services/ImageResizer_CropService.php @@ -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); @@ -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; @@ -72,4 +81,4 @@ private function _cropWithPath($path, $x1, $x2, $y1, $y2) } } -} \ No newline at end of file +} diff --git a/imageresizer/templates/settings/index.html b/imageresizer/templates/settings/index.html index 84759c3..78b84aa 100644 --- a/imageresizer/templates/settings/index.html +++ b/imageresizer/templates/settings/index.html @@ -98,7 +98,7 @@

{{ "{label} Asset Source Settings" | t({ label: item.label }) }}

{{ 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,