Image source not readable-Laravel 5.8 - laravel

I am trying to post an image to my front end page but when I click the post button I get an error
Image source not readable
private function handleRequest($request)
{
$data = $request->all();
if ($request->hasFile('image'))
{
$image = $request->file('image');
$fileName = $image->getClientOriginalName();
$destination = $this->uploadPath;
$successUploaded = $image->move(public_path('img'.$fileName));
//$successUploaded = $image->move($destination, $fileName);
if ($successUploaded)
{
$extension = $image->getClientOriginalExtension();
$thumbnail = str_replace(".{$extension}", "_thumb.{$extension}", $fileName);
Image::make($destination . '/' . $fileName)
->resize(250, 170)
->save($destination . '/' . $thumbnail);
}
$data['image'] = $fileName;
}
return $data;
}

Related

Save both original image and conversion to webp in laravel storage

I'm trying to save original jpg image and also a converted webp image to my storage when I'm storing images in my project.
public function store($data)
{
if (!$data->hasFile('fileName')) {
return response()->json(['upload_file_not_found'], 400);
}
$allowedfileExtension = ['jpg', 'jpeg'];
$files = $data->file('fileName');
$errors = [];
$images = [];
foreach ($data->fileName as $mediaFiles) {
$extension = $mediaFiles->getClientOriginalExtension();
$check = in_array($extension, $allowedfileExtension);
if ($check) {
// $path = $mediaFiles->store('public/images');
$name = $mediaFiles->getClientOriginalName();
//store image file into directory and db
$image = new Image();
$image->title = $name;
// $image->path = substr($path, 7);
$image->description = $data->description;
$image->author_id = $data->author_id;
$image->save();
//put image to storage wih unique folder
$path = $mediaFiles->storeAs('public/images/article-images/'.$image->id, $name);
//try to convert to webp and add to storage
$image = InterventionImage::make($mediaFiles)->encode('webp', 90)->resize(200, 250)->save('public/images/' . $name . '.webp');
//
//update images path in database
$image->update(['path' => substr($path, 7)]);
//add to attach the article id
if ($data->article_id) {
$image->articles()->attach($data->article_id);
}
array_push($images, $image);
} else {
return response()->json(['invalid_file_format'], 422);
}
}
return $images;
}
I'm using intervention library but when I try to save converted image to my storage I get the error "message": "Can't write image data to path (public/images/newimage.jpg.webp)",
can someone help with this or have any other suggestion how to do this?
if you want to convert image in to WEBP without any service or package, try this method. work for me. have any question can ask. Thankyou
$post = $request->all();
$file = #$post['file'];
$code = 200;
$extension = $file->getClientOriginalExtension();
$imageName = $file->getClientOriginalName();
$path = 'your_path';
if(!$file->move(public_path($path), $imageName)){
$code = 404;
}
if(in_array($extension,["jpeg","jpg","png"])){
//old image
$webp = public_path().'/'.$path.'/'.$imageName;
$im = imagecreatefromstring(file_get_contents($webp));
imagepalettetotruecolor($im);
// have exact value with WEBP extension
$new_webp = preg_replace('"\.(jpg|jpeg|png|webp)$"', '.webp', $webp);
// set qualityy according to requirement
return imagewebp($im, $new_webp, 50);
}
this will save JPG/PNG and WEBP file both to the move Folder.
If someone ever have the same problem I did it like this
public function newStore($data)
{
if (!$data->hasFile('fileName')) {
return response()->json(['upload_file_not_found'], 400);
}
$allowedfileExtension = ['jpg', 'jpeg'];
$files = $data->file('fileName');
$errors = [];
$images = [];
foreach ($data->fileName as $mediaFiles) {
$extension = $mediaFiles->getClientOriginalExtension();
$check = in_array($extension, $allowedfileExtension);
if ($check) {
// $path = $mediaFiles->store('public/images');
$name = $mediaFiles->getClientOriginalName();
//getting the name of the file without extension
$filename = pathinfo($name, PATHINFO_FILENAME);
//store image file into directory and db
$image = new Image();
$image->title = $name;
// $image->path = substr($path, 7);
$image->description = $data->description;
$image->author_id = $data->author_id;
$image->save();
//put image to storage in unique folder
$path = $mediaFiles->storeAs('public/images/article-images/' . $image->id, $name);
//imege conversion in webp format and move to right folder
$interventionImage = InterventionImage::make($mediaFiles)->stream("webp", 100);
Storage::disk('local')->put($filename . '.webp', $interventionImage, 'public');
Storage::move($filename . '.webp', 'public/images/article-images/' . $image->id . '/' . $filename . '.webp');
//update images path in database
$image->update(['path' => substr($path, 7)]);
//add to attach the article id
if ($data->article_id) {
$image->articles()->attach($data->article_id);
}
array_push($images, $image);
} else {
return response()->json(['invalid_file_format'], 422);
}
}
return $images;
}
and my folder structure looks like this:

move_uploaded_file(): Unable to move 'C:\xampp\tmp\php4496.tmp' to 'public/upload/member_new\BOm3f0T1kL.png' in Laravel 8

I've tried like this.
if ($imageimage) {
if ($member->image != null) {
#unlink($member->image);
}
$image_name = Str::random(10);
$ext = strtolower($imageimage->getClientOriginalExtension());
$image_name = $image_name . '.' . $ext;
$upload_path = 'public/upload/member_new/';
$image_description = $upload_path . $image_name;
$imageimage->move($upload_path, $image_name);
if ($ext=='jpg' || $ext=='png'|| $ext=='jpeg'){
$member->image = $image_description;
dd('success');
}else{
Toastr::error('message', 'Image is not valid!!');
return redirect()->back();
}
}
Could not move the file "C:\xampp\tmp\php4496.tmp" to "public/upload/member_new\BOm3f0T1kL.png" (move_uploaded_file(): Unable to move 'C:\xampp\tmp\php4496.tmp' to 'public/upload/member_new\BOm3f0T1kL.png').
You should use storage laravel.
https://laravel.com/docs/8.x/filesystem#specifying-a-file-name
$request->file->storeAs($path, $file_name, $disk);

my image are not saved in upload folder in laravel

whenever I uploaded an image in laravel, the image is upload to the database but after database uploading, my image didn't save that image in my root folder,
here is my upload code:
public function store(Request $request)
{
$records = $request->all();
if ($records != '') {
if ($request->session()->has('is_user_logged')) {
$UserPostModel = new UserPostModel;
$UserPostModel->uid = $request->session()->get('uid');
$UserPostModel->uname = $request->session()->get('name');
$UserPostModel->msg = $request->input('status');
$UserPostModel->eid = $request->input('event');
if ($request->hasFile('image')) {
if ($request->file('image')->isValid()) {
$fileName = $request->file('image')->getClientOriginalName();
$fileName = time() . "_" . $fileName;
$request->file = move_uploaded_file('uploads',$fileName);
$UserPostModel->image = $fileName;
}
}
$UserPostModel->save();
return redirect('uprofile')->with('message', 'Seccessfully Post !');
} else {
return redirect('login');
}
} else {
return redirect('/uprofile')->with('message', 'Please select image!');
}
}
Use store method and pass the driver as the second argument
$path = $request->file('image')->store($store_path, 'public');
Please replace this code with your image upload condition.
if ($file = $request->file('image')) {
$name = time() . $file->getClientOriginalName();
$file->move('uploads', $name);
$UserPostModel->image = $name;
}
$UserPostModel -> save();

Image store but not show proper path in laravel API

How can i store actually path in database and how can i fetch image in API?
This is output of API in postman
This is database which show store path of image but its wrong..
public function store(Request $request)
{
$event = $request->all();
if ($request->hasFile('file')) {
$destinationPath = public_path().'/public/img/';
$file = $request->file;
$fileName = time() . '.'.$file->clientExtension();
$file->move($destinationPath, $fileName);
$input['e_image'] = $fileName;
}
// $return['success'] = true,
// $return['data'] = $event,
// $return['msg'] = "this is message ";
$success['status'] = true;
$success['data'] = [$event];
$success['message'] ="Event created successfully!";
// $success['event'] = $event;
return response()->json($success);
}
$file = new YOURMODELNAME(); please enter your model name in this line of code
public function store(Request $request)
{
$input = $request->all();
$rules = array(
'e_image' => 'required|mimes:jpeg,png,jpg,doc,docx,pdf,mp4,mov,ogg,qt',
'e_group' => required,
'e_invite'=>required,
);
$validator = Validator::make($input, $rules);
if ($validator->fails()) {
$arr = array("status" => 400, "message" => $validator->errors()->first(), "data" => array());
} else {
try {
$file = $request->file('e_image');
$input['e_image'] = time() . '.' . $file->getClientOriginalExtension();
$destinationPath = public_path('/img/');
$file->move($destinationPath, $input['e_image']);
$file = new YOURMODELNAME();
$file->e_image = $input['e_image'];
$file->e_group = $input['e_group'];
$file->e_invite = $input['e_invite'];
$file->save();
$file->e_image = url('public/img/' . $file->e_image);
$arr = array("status" => 200, "message" => "file upload Successfully", "data" => $file);
} catch (\Exception $ex) {
if (isset($ex->errorInfo[2])) {
$msg = $ex->errorInfo[2];
} else {
$msg = $ex->getMessage();
}
$arr = array("status" => 400, "message" => $msg, "data" => array());
}
}
return \Response::json($arr);
}
please try this one and store image in proper project directory with unique image name .
$img = $request->profile_image;
$old_path = public_path() . "/public/img/";
$image_parts = explode(";base64,", $img);
$image_type_aux = explode("image/", $image_parts[0]);
$image_type = $image_type_aux[1];
$image_base64 = base64_decode($image_parts[1]);
$filename = uniqid() . '.png';
$file = $old_path . $filename;
file_put_contents($file, $image_base64);
if (file_exists(public_path('/public/img/' . $filename))) {
//move image
$new_path = public_path() . "/public/img/newimages/";
if (!is_dir($new_path)) {
mkdir($new_path, 0777, TRUE);
}
$move = File::move($old_path . $filename, $new_path . $filename);
}
//upload image at database
$modalObj = new table();
$modalObj->updateById(\Session::get('table')->id, [
'profile_photo' => $filename,
]);
return response()->json(['code' => '1', 'message' => 'Image Uploaded successfully!!!', 'data' => ['imageName' => url('/public/img/newimages/' . $filename)]]);

In controller its saving the image and how can i resize it to 150,150

its working but how to resize image in it
public function store(Request $request)
{
$input = $request->all();
$image = Input::file('image');
if (isset($image)) {
$directory = public_path() . '/uploads/programs';
$medium_size = public_path() . '/uploads/programs/100X100/';
$thumb_nails = public_path() . '/uploads/programs/100X100/';
if (!file_exists($directory)) File::makeDirectory($directory, 0777, true, true);
$filename = sha1(time() . time()) . ".png";
$filename = $image->getClientOriginalName();
$filename = str_random(40) . "$filename";
$upload_success = $image->move($directory, $filename);
$input['image'] = $filename;
}
product::create($input);
return redirect('home');
}

Resources