Skip to content

Instantly share code, notes, and snippets.

@bantawa04
Last active June 16, 2022 09:50
Show Gist options
  • Save bantawa04/114d952ee48be4d40d5339703bdd42e1 to your computer and use it in GitHub Desktop.
Save bantawa04/114d952ee48be4d40d5339703bdd42e1 to your computer and use it in GitHub Desktop.
Image Kit in Laravel
<?php
namespace App\Traits;
use Intervention\Image\Facades\Image;
use ImageKit\ImageKit;
trait ImageKitUtility
{
//Upload to imagekit
protected function uploadToImageKit($payload, $fileName, $folder, $height, $width, $isPath = null)
{
try {
//if $isPath is true image is fetched from local storage (public)
//else image is fetched from form then converted to base 64
$isPath ? $img = $payload : $img = $this->toBase64($payload, $width, $height);
$toImageKit = $this->init();
$uploadFile = $toImageKit->upload(array(
'file' => $img,
'fileName' => $fileName,
"folder" => $folder,
)); //code...
return $uploadFile;
} catch (\Exception $e) {
return $e->getMessage();
}
}
//convert image to base64
private function toBase64($file, $width, $height)
{
if ($width && $height) {
$base64 = Image::make($file)
->resize($width, $height, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
})->encode('data-url');
}
$base64 = Image::make($file)
->encode('data-url');
return $base64;
}
//Delete remote image
protected function deleteImage($fileId)
{
try {
$toImageKit = $this->init();
return $toImageKit->deleteFile($fileId);
} catch (\Exception $e) {
return $e;
}
}
//initialize remote ImageKit bucket
private function init()
{
return new ImageKit(
env('IMAGE_KIT_PUBLIC'),
env('IMAGE_KIT_PRIVATE'),
env('IMAGE_KIT_URL')
);
}
}
<?php
namespace App\Traits;
use Intervention\Image\Facades\Image;
use Spatie\LaravelImageOptimizer\Facades\ImageOptimizer;
trait LocalUpload
{
protected function uploadThumbnail($file, $localPath,$type, $width, $height)
{
$fileName = $type.$file->getClientOriginalName().'_'.time().'.'.$file->getClientOriginalExtension();
$thumbPath = $localPath. $fileName;
Image::make($file)
->resize($width, $height, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
})
->save($thumbPath);
ImageOptimizer::optimize($thumbPath);
return $thumbPath;
}
}
<?php
use App\Traits\ImageKitUtility;
use App\Traits\LocalUpload;
use App\Media;
class MediaController extends Controller
{
use ImageKitUtility;
use LocalUpload;
private $path = "img/";
public anyFunction(Request $request){
//Upload image from Form
$response = $this->uploadToImageKit($request->file('photo'), 'med_.jpg', 'media', null, null, false);
//upload image from local storage(public folder) after refactering
$path = $this->uploadThumbnail($request->photo, $this->path, 'car_thumb_', 1920, 1080);
//response after upload
$response = $this->uploadToImageKit($path, 'car_.jpg', 'carousel', null, null, true);
@unlink($path); //if local file is not needed
return Media::create([
'thumb' => $thumbnail,
'url' => $response->success->url,
'fileID' => $response->success->fileId,
]);
}
}
@luqman-v1
Copy link

thankyou bro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment