Skip to content

Commit

Permalink
Update Crop Non-destructive functionality
Browse files Browse the repository at this point in the history
On cropping the original image get's copied as a file and as an asset so you can work with the cropped version in Craft. This is instead of copying the original file into a folder you can only access through ftp. Now you can access the original and cropped version through the assets in Craft.
  • Loading branch information
Webs committed Nov 24, 2017
1 parent 3085a78 commit 1531bc6
Showing 1 changed file with 26 additions and 17 deletions.
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)
}
}

}
}

0 comments on commit 1531bc6

Please sign in to comment.