Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Cloud Storage #4565

Merged
merged 18 commits into from
Apr 5, 2020
Merged
Changes from 1 commit
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
Next Next commit
Support for Cloud Storage
Support for Cloud Storage (Google Bucket/Amazon S3) without breaking local and public storage
  • Loading branch information
faizananwerali authored Nov 15, 2019
commit a0de68b37590133b8d8cc37850a13d75d7cb1511
77 changes: 60 additions & 17 deletions src/Http/Controllers/VoyagerMediaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,12 @@ public function upload(Request $request)
'image/svg+xml',
];
if (in_array($request->file->getMimeType(), $imageMimeTypes)) {
$image = Image::make($realPath.$file);
try {
$image = Image::make($realPath.$file);
} catch (Exception $e) {
$content = Storage::disk($this->filesystem)->get($realPath.$file);
$image = Image::make($content);
}

faizananwerali marked this conversation as resolved.
Show resolved Hide resolved
if ($request->file->getClientOriginalExtension() == 'gif') {
copy($request->file->getRealPath(), $realPath.$file);
Expand Down Expand Up @@ -302,13 +307,16 @@ function ($constraint) use ($thumbnail_data) {
$thumbnail = $this->addWatermarkToImage($thumbnail, $details->watermark);
}
$thumbnail->save($realPath.$request->upload_path.$name.'-'.($thumbnail_data->name ?? 'thumbnail').'.'.$extension, ($details->quality ?? 90));
//$path = $realPath.$request->upload_path.$name.'-'.($thumbnail_data->name ?? 'thumbnail').'.'.$extension;
faizananwerali marked this conversation as resolved.
Show resolved Hide resolved
//Storage::disk($this->filesystem)->put($path, (string) $thumbnail->encode(null, ($details->quality ?? 90)));
}
}
// Add watermark to image
if (property_exists($details, 'watermark') && property_exists($details->watermark, 'source')) {
$image = $this->addWatermarkToImage($image, $details->watermark);
}
$image->save($realPath.$file, ($details->quality ?? 90));
//Storage::disk($this->filesystem)->put($realPath.$file, (string) $image->encode(null, ($details->quality ?? 90)));
faizananwerali marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -339,37 +347,72 @@ public function crop(Request $request)

$realPath = Storage::disk($this->filesystem)->getDriver()->getAdapter()->getPathPrefix();
$originImagePath = $realPath.$request->upload_path.'/'.$request->originImageName;
$originImagePath = str_replace('\\', '/', $originImagePath);
$originImagePath = preg_replace('#/+#', '/', $originImagePath);

if ($createMode) {
// create a new image with the cpopped data
$fileNameParts = explode('.', $request->originImageName);
array_splice($fileNameParts, count($fileNameParts) - 1, 0, 'cropped_'.time());
$newImageName = implode('.', $fileNameParts);
$destImagePath = $realPath.$request->upload_path.'/'.$newImageName;
} else {
// override the original image
$destImagePath = $originImagePath;
}
$destImagePath = str_replace('\\', '/', $destImagePath);
$destImagePath = preg_replace('#/+#', '/', $destImagePath);

try {
if ($createMode) {
// create a new image with the cpopped data
$fileNameParts = explode('.', $request->originImageName);
array_splice($fileNameParts, count($fileNameParts) - 1, 0, 'cropped_'.time());
$newImageName = implode('.', $fileNameParts);
$destImagePath = $realPath.$request->upload_path.'/'.$newImageName;
} else {
// override the original image
$destImagePath = $originImagePath;
}

faizananwerali marked this conversation as resolved.
Show resolved Hide resolved
Image::make($originImagePath)->crop($width, $height, $x, $y)->save($destImagePath);
$image = null;
$success = false;

try {
$image = Image::make($originImagePath)->crop($width, $height, $x, $y);
$image->save($destImagePath);
$success = true;
$message = __('voyager::media.success_crop_image');
} catch (Exception $e) {
$success = false;
$message = $e->getMessage();
}

if (!$success) {
try {
$content = Storage::disk($this->filesystem)->get($originImagePath);
$image = Image::make($content)->crop($width, $height, $x, $y);
Storage::disk($this->filesystem)->put($destImagePath, (string) $image->encode());
$success = true;
$message = __('voyager::media.success_crop_image');
} catch (Exception $e) {
$message = $e->getMessage();
}
}

return response()->json(compact('success', 'message'));
}

private function addWatermarkToImage($image, $options)
private function addWatermarkToImage(\Intervention\Image\Image $image, $options)
faizananwerali marked this conversation as resolved.
Show resolved Hide resolved
{
$watermark = Image::make(Storage::disk($this->filesystem)->path($options->source));
$watermark = null;
try {
$watermark = Image::make(Storage::disk($this->filesystem)->path($options->source));
$success = true;
} catch (Exception $e) {
$success = false;
}

if (!$success){
try {
$content = Storage::disk($this->filesystem)->get($options->source);
faizananwerali marked this conversation as resolved.
Show resolved Hide resolved
$watermark = Image::make($content);
} catch (Exception $e) {
return $image;
}
}

// Resize watermark
$width = $image->width() * (($options->size ?? 15) / 100);
$watermark->resize($width, null, function ($constraint) {
var_dump($constraint);
$constraint->aspectRatio();
});

Expand Down