Skip to content

Instantly share code, notes, and snippets.

@egin10
Last active January 19, 2020 04:48
Show Gist options
  • Save egin10/eb0ee07cf7b5b9793ae55d445ac81fbe to your computer and use it in GitHub Desktop.
Save egin10/eb0ee07cf7b5b9793ae55d445ac81fbe to your computer and use it in GitHub Desktop.
Storing Image Laravel API
<?php
namespace App\Repositories;
use Illuminate\Support\Str;
class Image {
public function actionImage($nameOldImage = null, $method = null)
{
if($method = "DELETE_IMAGE"){
if(file_exists(public_path('img/' . $nameOldImage))){
@unlink(public_path('img/' . $nameOldImage));
}
}
if(request()->hasFile('image')){
@unlink(public_path('img/' . $nameOldImage));
$extension = request()->file('image')->getClientOriginalExtension();
$fileName = Str::random(9) . '.' . $extension;
request()->file('image')->move(public_path("img/"), $fileName);
return $fileName;
}
}
}
#http://image.intervention.io/getting_started/installation#laravel
<?php
namespace App\Repositories;
use Image;
use File;
class Images {
public function actionImage($nameOldImage = null, $method = null)
{
if($method = "DELETE_IMAGE"){
if(file_exists(public_path('img/menus/' . $nameOldImage))){
@unlink(public_path('img/menus/' . $nameOldImage));
}
}
if(request()->hasFile('image')){
@unlink(public_path('img/menus/' . $nameOldImage));
$extension = request()->file('image')->getClientOriginalExtension();
$fileName = str_random(8) . '.' . $extension;
$canvas = Image::canvas(1200, 630);
$resizeImage = Image::make(request()->file('image'))->resize(1200, 620, function($constraint) {
$constraint->aspectRatio();
});
$canvas->insert($resizeImage, 'center');
$canvas->save(public_path("img/menus/").$fileName);
// request()->file('image')->move(public_path("img/menus/"), $fileName);
return $fileName;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment