delete image and thumbnail image in laravel - php

i try to delete some image file in laravel project. i follow tutorial on website, and its work, but tutorial only deleted image based name in column image. the thumbnail on folder image didnt deleted. how to delete image also the thumbnail image? pls help.
this is the store function.
if ($request->hasFile('image'))
{
$file = $request->file('image');
$image = $file->getClientOriginalName();
$destination = public_path() . '/galeri/';
$successUploaded = $request->file('image')->move($destination, $file->getClientOriginalName());
if($successUploaded)
{
$extension = $file->getClientOriginalExtension();
$thumbnail = str_replace(".{$extension}", "_thumb.{$extension}", $image);
Image::make($destination . '/' . $image)
->resize(250, 170)
->save($destination . '/' . $thumbnail);
}
$photo->image = $image;
}
$photo->save();
and this the destroy function
$photo = Photo::where('id', $id)->first();
File::delete('galeri/'.$photo->image);
if($photo){
$photo->delete();
}

Delete a single file
File::delete($filename);
Delete multiple files
File::delete($file1, $file2, $file3);
Delete an array of files
$files = array($file1, $file2);
File::delete($files);

Related

How to resize image before upload in laravel?

I want to resize image beofre upload it into database. I store image in this way
if ($image) {
$image_name = hexdec(uniqid());
$ext = strtolower($image->getClientOriginalExtension());
$image_full_name = $image_name . '.' . $ext;
$upload_path = 'foods/';
$upload_path1 = 'images/foods/';
$image_url = $upload_path . $image_full_name;
$success = $image->move($upload_path1, $image_full_name);
}
Now, I want to resize image before uploading it. I try to use intervation package, I try this
if ($image) {
$image_name = hexdec(uniqid());
$ext = strtolower($image->getClientOriginalExtension());
$image_full_name = $image_name . '.' . $ext;
$upload_path = 'foods/';
$upload_path1 = 'images/foods/';
$image_url = $upload_path . $image_full_name;
$img = Image::make($image)->resize(300, 200);
$img->save($upload_path1, 60, $image_full_name);
}
and I got this error
"Encoding format (1691942233153059.jpg) is not supported."
What is the best way to resize image before upload and also set image with unique name?
1- use getRealPath() inside Image::make()
2- save image in particular path.
if($request->hasFile('image')) {
$image = $request->file('image');
$file_name = $image->getClientOriginalName();
$image_resize = Image::make($image->getRealPath());
$image_resize->resize(300, 100);
$image_resize->save(public_path('img/' .$file_name));
}

Failed to rename image and upload using storeAs in Laravel

I created a form to store article with an image , and generate a resized version as thumbnail from it.
I want the image to be renamed after the article slug and stored in the "public/img/articles-images " directory but i keep receiving : "Image source not readable" error
This is the image upload handler function in my controller :
private function handleRequest($request)
{
$data = $request->all();
if ($request->hasFile('image')) {
$image = $request->file('image');
$fileName = $request->slug;
$successUploaded = $image->storeAs('img/articles-images', $fileName);
if($successUploaded) {
$width = config('cms.image.thumbnail.width');
$height = config('cms.image.thumbnail.height');
$extension = $image->getClientOriginalExtension();
$thumbnail = str_replace(".{$extension}", "_thumb.{$extension}", $fileName);
Image::make('img/articles-images' . '/' . $fileName)
->resize($width, $height)
->save('img/articles-images' . '/' . $thumbnail);
}
$data['image'] = $fileName;
}
return $data;
}
storeAs() method, which receives the path, the file name, and the (optional) disk as its arguments :
$successUploaded = $request->file('image')->storeAs(
'images', $fileName
);
I solved it ! Apparently there was no need to storeAs() method at all , the new code is like below :
if ($request->hasFile('image')) {
$image = $request->file('image');
$fileName = $request->slug.'.' .$image->getClientOriginalExtension();
$destination = $this->uploadPath;
$successUploaded = $image->move($destination, $fileName);
// //

How do I use Image Intervention with Laravel Storage and digital ocean

I am using the Laravel storage feature to store images on DigitalOcean which works fine but I can't seem to add image intervention because I want to use the orientate feature. This is so that images created through the phone doesn't rotate sideways. How do I add image intervention to my existing code?
if ( $request->hasFile('image')) {
$extension = $request->file('image')->extension();
$mimeType = $request->file('image')->getMimeType();
$path = Storage::disk('do')->putFileAs('public/images', $request->file('image'), time() . '.' . $extension);
Storage::disk('do')->setVisibility($path, 'public');
$product->image = $path;
}
So i tried different ways of implementing image intervention haven't had much success but here my updated code. An with that i got a error saying Command (GetRealPath) is not available for driver (Gd).
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('/images/' . $filename);
$extension = $request->file('image')->extension();
//$mimeType = $request->file('image')->getMimeType();
$orientate = Image::make($image)->orientate();
$path = Storage::disk('do')->putFileAs('public/images', $orientate, $filename);
Storage::disk('do')->setVisibility($path, 'public');
$product->image = $path;

Upload image with different name but keep file extension

When i save image with my form i change the file name. Everything works nice except file extension. When i change file name the extension is missing and file is saved without it. Please help me to solve my problem i want to store also image extension.
This is my code where i update image name.
$image = $request->file('image');
if ($image) {
$imgPath = 'public/post-image/';
$imgName = uniqid('img_');
$store = $request->file('image')->storeAs(
$imgPath, $imgName
);
$post->image = $imgName;
}
Try this:
$image = $request->file('image');
if ($image) {
$imgPath = 'public/post-image/';
$imgName = uniqid('img_') . '.' . $image->extension();
$store = $image->storeAs($imgPath, $imgName);
$post->image = $imgName;
}

Saving a remote image from facebook graph api

Any idea what's wrong?
I end up with 1bytes files and they are wrong. Using Intervention Image & PHP. Tried many ways to get it but nothing... If I just want to display it works but I can't save the picture...
$avatar_url = 'http://graph.facebook.com/' . $id . '/picture?type=square&width=140&height=140&redirect=false';
$avatar_pic_url = json_decode(file_get_contents($avatar_url), true)['data']['url'];
dd($avatar_pic_url);
$avatar = Image::make($avatar_pic_url);
$extension = 'jpg';
// save it
$destinationPath = 'uploads/avatars/';
$filename = uniqid(). '.' . $extension;
$path_to_temp_image = $avatar->dirname . '/' . $avatar->filename;
$key = $destinationPath . $filename;
// Upload avatar to remote storage
$uploadSuccess = Storage::put($key, $path_to_temp_image);

Categories