Created
February 22, 2023 05:19
-
-
Save naagaraa/483f843b01cb9f395f715610c8eec649 to your computer and use it in GitHub Desktop.
example how to use laravel intervention at laravel 8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Modules\Admin\Http\Controllers\Achievement; | |
use Ramsey\Uuid\Uuid; | |
use Illuminate\Http\Request; | |
use Illuminate\Routing\Controller; | |
use Intervention\Image\Facades\Image; | |
use App\Models\AchievementRewardModel; | |
use App\Models\Images; | |
use Illuminate\Database\QueryException; | |
use Illuminate\Support\Facades\Storage; | |
use Illuminate\Support\Facades\Validator; | |
class ExampleController extends Controller | |
{ | |
public function update_cover(Request $request) | |
{ | |
$validator = Validator::make($request->all(), [ | |
'id' => 'required', | |
'cover' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048', | |
]); | |
if ($validator->fails()) { | |
return redirect() | |
->back() | |
->withErrors($validator) | |
->withInput(); | |
} | |
// get data | |
$reward = AchievementRewardModel::where("id", "=", $request->id)->first(); | |
$image = $request->file('cover'); | |
$file_name = $reward->reward_uniqid . '.' . $image->getClientOriginalExtension(); | |
$target = public_path('storage/achievement/') .$file_name; //folder public | |
// save image to storage | |
Storage::putFileAs( | |
config("directory.achievement"), | |
$request->file('cover'), | |
$file_name, | |
); | |
if (file_exists($target)) { | |
// Image exists, you can now use it | |
// compress image at public folder and will update image at storage link via symlink | |
Image::make($target)->fit(800, 600)->encode('jpg', 80)->save($target); | |
} | |
// save to database | |
$reward->image = $file_name; | |
$reward->save(); | |
$exiting_image = Images::where("picture_id", "=", $request->id)->first(); | |
if($exiting_image){ | |
// update data to t_image | |
$exiting_image->descritption = "reward"; | |
$exiting_image->name = $file_name; | |
$exiting_image->save(); | |
}else{ | |
// create new data to t_image | |
$image = new Images; | |
$image->picture_id = $request->id; | |
$image->descritption = "reward"; | |
$image->name = $file_name; | |
$image->save(); | |
} | |
return redirect() | |
->back() | |
->with('success','Image Upload successful'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment