Call to a member function move() on string Larael base64 encoded - laravel

I'm getting photo from Android phone using API and base64encoded format.
After getting photo i must to resize it. But i'm getting error. Please help to solve it.
$image = $request->photo; // my base64 encoded
$image = str_replace('data:image/jpg;base64,', '', $image);
$image = str_replace(' ', '+', $image);
$imagename = 'prsn-'.time().'.jpg';
$destinationPath = public_path('/thumbnail');
$img = Image::make($image);
$img->resize(150, 150, function ($constraint)
{
$constraint->aspectRatio();
})->save($destinationPath.'/'.$imagename);
$destinationPath = storage_path('local');
$image->move($destinationPath, $imagename); /*** <<<<<<< getting error on this line ***/
$input = $request->all();
$input['photo'] = $imagename;
Contact::create($input);

Problem is solved using belowed code
$image = $request->photo; // my base64 encoded
$image = str_replace('data:image/jpg;base64,', '', $image);
$image = str_replace(' ', '+', $image);
$imagename = 'prsn-'.time().'.jpg';
$destinationPath = public_path('/thumbnail');
$img = Image::make($image);
$img->resize(150, 150, function ($constraint)
{
$constraint->aspectRatio();
})->save($destinationPath.'/'.$imagename);
$destinationPath = public_path('/images');
Image::make($image)->save($destinationPath.'/'.$imagename);
$input = $request->all();
$input['photo'] = $imagename;
Contact::create($input);
Thanks for all who want to help, special thank to: porloscerros-Ψ for advice and suggestion

Related

Unable to save base 64 Image in Laravel 8

I am trying to save base64 image that is coming from the ajax post (blade file). Below is the code that I am using to save the data but it is giving 500 error.
public function add_ref_images_first(Request $request){
$fileName = "";
$end_url = "";
$count = 0;
$folder_name = 'PUBP' . time();
foreach ($request->images as $data){
$image_64 = $data['src']; //your base64 encoded data
$extension = explode('/', explode(':', substr($image_64, 0, strpos($image_64, ';')))[1])[1]; // .jpg .png .pdf
$replace = substr($image_64, 0, strpos($image_64, ',')+1);
//
// // find substring fro replace here eg: data:image/png;base64,
//
$image = str_replace($replace, '', $image_64);
$image = str_replace(' ', '+', $image);
$ref_image_id = 'PUBR'.time().$count++.'.'.$extension;
$fileName = base64_decode($image)->storeAs($folder_name, $ref_image_id , ['disk' => 'my_uploaded_files']);
if($imageName){
$end_url = $end_url.$imageName.',';
}
}
return response()->json(['url' => $end_url, 'id' => '1']);
}
Is there issue with the code?
instead of the line
$fileName = base64_decode($image)->storeAs($folder_name, $ref_image_id , ['disk'
=> 'my_uploaded_files']);
you can do :
use Illuminate\Support\Facades\Storage;
Storage::put($ref_image_id, base64_decode($image), 'local');
because basically you were trying storeAs on a string value, storeAs works on $request->file('nameFromForm')
reference: https://laravel.com/docs/8.x/filesystem
https://laravel.com/docs/8.x/filesystem#specifying-a-file-name

some images gets duplicated after the uploading - laravel

i am using a function that uploads multiple images and it was working perfectly locally but after deployment i am having this duplication issue .
i upload img1 and img2 and img3 but in the database i find only one of them like img1.jpg and the same thing in the folder .
public function _articleGalleryUpload(Request $request)
{
$article = Blog::find($request->id);
$img = $request->Otitle;
//dd($img);
$request->validate([
'images'=> 'required',
'images.*'=> 'image|mimes:jpg,png,jpeg|max:4000'
]);
if(!File::isDirectory('assets/images/blogs/gallery/'.$img)){
File::makeDirectory('assets/images/blogs/gallery/'.$img);
}
$images = $request->file('images');
if($request->hasFile('images'))
{
foreach( $images as $image )
{
//$extension = $file->getClientOriginalExtension();
$ImageName = '_' . time() .'.' . $image->getClientOriginalExtension();
$path = $image->move(('assets/images/blogs/gallery/'.$img), $ImageName);
$imageP = Image::make($path)->resize(600, null, function ($constraint) {
$constraint->aspectRatio();
});
$galerie = new blogsGallery;
$galerie->Img = $ImageName;
$galerie->idart = $request->id;
$galerie->alt = $img;
$imageP->save();
$galerie->save();
}
return back()->with('success', 'les images ont été téléchargées');
}
}
i tried using array to store the names of the images but it didn't work, it uploads one image instead of multiple .
Your code has one issue.
$path = $image->move(('assets/images/blogs/gallery/'.$img), $ImageName);
this code is error.
$img is one name for several images.

Image not saving in Database in Laravel

I am trying to store image into database after it has been converted to base64 and also decoded. The image stores inside the Storage path but does not save into mysql database.
What am i doing wrong?
public function updateProfileImage(Request $request)
{
$user = auth('api')->user();
$image = $request->input('image'); // image base64 encoded
preg_match("/data:image\/(.*?);/",$image,$image_extension); // extract the image extension
$image = preg_replace('/data:image\/(.*?);base64,/','',$image); // remove the type part
$image = str_replace(' ', '+', $image);
$imageName = 'profile' . time() . '.' . $image_extension[1]; //generating unique file name;
Storage::disk('public')->put($imageName,base64_decode($image));
$user->update($request->all());
}
Try this:
$user = auth('api')->user();
if ($request['image']) {
$data = $request['image'];
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$image = base64_decode($data);
$photoName = 'profile' . time() . '.' . $image_extension[1];
$request['image'] = $photoName;
Storage::disk('public')->put($photoName, $image);
$user->update($request->all());
}
I had to do this
public function updateProfileImage(Request $request)
{
$user = auth('api')->user();
$image = $request->input('image'); // image base64 encoded
preg_match("/data:image\/(.*?);/",$image,$image_extension); // extract the image extension
$image = preg_replace('/data:image\/(.*?);base64,/','',$image); // remove the type part
$image = str_replace(' ', '+', $image);
$imageName = 'profile' . time() . '.' . $image_extension[1]; //generating unique file name;
Storage::disk('public')->put($imageName,base64_decode($image));
$user->update($request->except('image') + [
'profilePicture' => $imageName
]);
}
and it worked
I recommend you to use uploader packages like:
https://github.com/spatie/laravel-medialibrary
or
https://github.com/alaaelgndy/FileUploader
to help you in media management without writing all these lines of code in every place you want to upload files.
enjoy them.

Intervention / Image Image source not readable

I have tried to solve the problem by looking at the answers for the same problem, but their answer didnt' solve my problem
I have this error when i run this:
$image = Image::make(public_path("storage/{$imagePath}"))->fit(1200, 1200);
when I dd the path, this is wht i have:
"C:\Users\USER\MziSpoort\public\storage/uploads/V9SGmLlbg0r21h7y3pVT6w2IvJwQhTD6nkDtvAYO.jpeg"
thanks for your help,
$image = $request->file('image');
$slug = str_slug($request->title);
if (isset($image))
{
$currentDate = Carbon::now()->toDateString();
$imagename = $slug.'-'.$currentDate.'-'. uniqid() .'.'. $image->getClientOriginalExtension();
$image_resize = Image::make($image->getRealPath());
$image_resize->resize(1600,1066);
if (!file_exists('storage/uploads/post'))
{
mkdir('storage/uploads/post',0777,true);
}
//$image->move('storage/uploads/post',$imagename);
$image_resize->save('storage/uploads/post/'.$imagename);
}else{
$imagename = "default.png";
}
// just use this code, i hope your problem solve

Laravel upload gifs

i am uploading gif for my posts in laravel but gif is like an image its not moving or something like this
<?php
if($request->hasFile('gif')){
$gif = $request->file('gif');
$gif_filename = time() . '.' . $gif->getClientOriginalName();
$gif_location = public_path('/images/' . $gif_filename);
Image::make($gif)->save($gif_location);
}
$post->gif = $gif_filename;
$post->save();
?>
here is the code what I am using I think everything is kinda correct
I use this method
if(Input::hasFile('imagen')) {
$time = Carbon::now()->format('Y-m-d');
$image = $request->file('imagen');
$extension = $image->getClientOriginalExtension();
$name = $image->getClientOriginalName();
$fileName = $time."-".$name;
$image->move(storage_path(),$fileName);
}
Please try this and let me know how it works :)
An easy way to update and save is to do it like this:
public function store(Request $request)
{
$imgLocation = Storage::disk('public')->put(time() . '.' . $request->file('image')->getClientOriginalName(), $request->gif), $request->file('gif'));
// This would save it to the gifs table if you need something like it, otherwise skip this creation
$gif= Gif::create([
'name' => $request->name,
'path' => $imgLocation
]);
if ($gif) {
return response()->json("Success!");
}
return response()->json("Error!"); // or you return redirect()...
}
$image = $request->file('image');
if(isset($image)) {
if($image->getClientOriginalExtension()=='gif'){
$image = $request->file('image');
$extension = $image->getClientOriginalExtension();
$name = $image->getClientOriginalName();
$fileName = 'exerciseimages'."-".$name;
$image->move('storage/courseimages/',$fileName);
}
else{
$fileName = 'exerciseimages'.'-'.uniqid().'.'.$image->getClientOriginalExtension();
if(!Storage::disk('public')->exists('courseimages')){
Storage::disk('public')->makeDirectory('courseimages');
}
$amenitiesimg = Image::make($image)->resize(250,250)->stream();
Storage::disk('public')->put('courseimages/'.$fileName, $amenitiesimg);
}
}
else {
$fileName = 'default.png';
}

Resources