NotReadableException in AbstractDecoder.php line 302: Image source not readable - php

I trying to follow tutorial about insert image and resize it, but i facing one problem showing image source not readable.
I am using PHP, Laravel 5 framework and mysql. When I run my code i stop on Image::make
Here is my controller code:
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Requests\FoodRequest;
use App\Http\Controllers\Controller;
use App\Photo;
use Image;
use App\Restaurant;
use Symfony\Component\HttpFoundation\File\UploadedFile;
public function addPhoto($zip, $street, Request $request)
{
$this->validate($request, [
'photo' => 'required|mimes:jpg,jpeg,png,bmp'
]);
$photo = $this->makePhoto($request->file('photo'));
Restaurant::locatedAt($zip, $street)->addPhoto($photo);
}
protected function makePhoto(UploadedFile $file)
{
return Photo::named($file->getClientOriginalName())
->move($file);
}
Here is Photo Code:
public static function named($name)
{
return (new static)->saveAs($name);
}
protected function saveAs($name)
{
$this->name = sprintf("%s-%s", time(), $name);
$this->path = sprintf("%s-%s", $this->baseDir, $this->name);
$this->thumbnail_path = sprintf("%s/tn-%s", $this->baseDir, $this->name);
return $this;
}
public function move(UploadedFile $file)
{
$file->move($this->baseDir, $this->name);
$this->makeThumbnail();
return $this;
}
protected function makeThumbnail()
{
Image::make($this->path)
->fit(200)
->save($this->thumbnail_path);
}

I did the same tutorial , you should do this:
Image::make($this->path.$this->name)->resize(128,
128)->save($this->thumbnail_path.$this->name);
instead of doing this:
Image::make($this->path)->fit(200)->save($this->thumbnail_path);

This is an example from my own code where I write the path to my pics
$destinationpath = 'img/' . $propertyid;
$frontpage = 'img/' . $propertyid. '/frontpage/' ;
$gallery = 'img/' . $propertyid. '/gallery/' ;
$thumbpath = 'img/' .$propertyid .'/thumbnails/';
move the image file to a place at which the Intervention Manipulation
code will process the image, change size etc. We will save the results
of the processing in their respective folders and then delete this image.
$image->move($destinationpath, $filename );
$dbImg = new Picture;
$dbImg->property_id = $propertyid;
$dbImg->name = $filename;
$dbImg->save();

Related

I got the message error "Can't write image data to path (C:\\xampp\\htdocs\\webnsi\\public/assets/img/reward)" from laravel websi

I am using Laravel for webiste and i want to resize or customize the size that i want but I am not sure why I am getting this problem when everything else seems to be right. here is the code:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Session;
use App;
use App\AllData;
use App\AwardCertification;
use Illuminate\Support\Str;
use App\Banner;
use Intervention\Image\ImageManagerStatic as Image;
....
public function store(Request $request){
$award = new AwardCertification;
$kd = Str::random(15);
$award->name = $request->name;
// store image
if($request->hasFile('file')){
$image = $request->file('file');
$img = Image::make($image);
$img->resize(500, 500, function ($constraint) {
$constraint->aspectRatio();
});
$img->save(public_path().'/assets/img/reward', $kd . "." . $image->getClientOriginalExtension());
$award->image = $kd . "." . $image->getClientOriginalExtension();
}
// save
if($award->save()) {
return $this->response(0, 'Data Created Successfull');
} else {
return $this->response(1, 'Failed Created Data');
}
}
i want to upload file and customize the size and save to '/assets/img/reward'
If reward is your folder, then you should add / to the end
$img->save(public_path().'/assets/img/reward/' . $kd . "." . $image->getClientOriginalExtension());
^^^
and make sure the directory exists and permission is set
mkdir(public_path().'/assets/img/reward', 0777, true);

File exist but laravel can't find it (FileNotFoundException)

I'm trying to check if the file exists to delete it when the post is deleted but it's never finding it.
If I change the Storage::exists() for Storage::get() just to check, I get the File Not Found Exception with the path C:/xampp/htdocs/cms/blog/public/images/apple.jpg which I can see the picture if I put in the browser.
Store function on PostController
public function store(CreatePostRequest $request)
{
$input = $request->all();
if ($file = $request->file('file')) {
//
$name = $file->getClientOriginalName();
$file->move(public_path('images/'), $name);
$input['path'] = $name;
}
$new_post = Post::create($input);
return redirect(route('post.show', $new_post->id));
}
Destroy function on PostController
public function destroy($id)
{
$post = Post::findOrFail($id);
if (Storage::exists(public_path('images/') . $post->path))
Storage::delete(public_path('images/') . $post->path);
$post->delete();
return redirect(route('posts.index'));
}
I also have this on my filesystems.php
'links' => [
public_path('storage') => storage_path('app/public'),
public_path('images') => storage_path('app/images'),
],
I can easily show the image in blade with just src="{{'/images/' . $post->path}}"
You could try using unlink.
$image_path = $post->path;
unlink($image_path);
The second option is to use the File Facade.
use Illuminate\Support\Facades\File;
$filename = $post->path;
File::delete($filename);
Make sure that the image path is correct.
I Had to use the Illuminate\Support\Facades\File sugested by Aless
Fixed destroy funcion on PostController
public function destroy($id)
{
$post = Post::findOrFail($id);
$imagePath = public_path('images/') . $post->path;
if (File::isFile($imagePath))
File::delete($imagePath);
$post->delete();
return redirect(route('posts.index'));
}

Trying to Get the name of an uploaded Image

I am trying to get the name of an image and save it instead of saving it as laravel default hashing.
i.e if an image name is go.jpg it should save as go.jpg instead of randomly generated numbers
Here is my controller
private function storeImage($news)
{
if (request()->has('image')){
$news->update([
'image' => request()->image->store('uploads', 'public'),
]);
$image = Image::make(public_path('storage/'. $news->image))->resize(600, 600);
$image->save();
}
}
You can use this method: getClientOriginalName()
if ($request->hasFile('image')) {
return $request->file('image')->getClientOriginalName();
} else {
return 'no file!'
}
http://api.symfony.com/3.0/Symfony/Component/HttpFoundation/File/UploadedFile.html#method_getClientOriginalName
getClientOriginalName use this method.
use Illuminate\Support\Facades\Input;
private function storeImage($news)
{
if (request()->has('image')){
$file = Input::file('image');
$img= $file->getClientOriginalName().'.'.$file->getClientOriginalExtension();
$news->update([
'image' => $img,
]);
$image = Image::make(public_path('storage/'. $news->image))-
>resize(600, 600);
$image->save();
}
}

How to upload Image in Database in Laravel 5.7?

I'm making an app in Laravel 5.7 . I want to upload image in database through it and I want to show it from database.
I have tried different methods around the Internet as I was getting issues in
Intervention\Image\Facades\Image
I followed many advices from Internet make changes in config.app
made changes in Composer
At the end used
use Intervention\Image\Facades\Image as Image;
So I get resolved from issue "Undefined class Image"
but now I' m getting issues as "Undefined class File",
Method getClientOriginalExtension not found.
Method Upsize, make not found.
My code is
<?php
namespace App\Http\Controllers;
use File;
use Intervention\Image\Facades\Image as Image;
use App\User;
use Illuminate\Http\Request;
class UserController extends Controller
{
//
protected $user;
/**
* [__construct description]
* #param Photo $photo [description]
*/
public function __construct(
User $user )
{
$this->user = $user;
}
/**
* Display photo input and recent images
* #return view [description]
*/
public function index()
{
$users = User::all();
return view('profile', compact('users'));
}
public function uploadImage(Request $request)
{
$request->validate([
'image' => 'required',
'image.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'
]);
//check if image exist
if ($request->hasFile('image')) {
$images = $request->file('image');
//setting flag for condition
$org_img = $thm_img = true;
// create new directory for uploading image if doesn't exist
if( ! File::exists('images/originals/')) {
$org_img = File::makeDirectory('images/originals/', 0777, true);
}
if ( ! File::exists('images/thumbnails/')) {
$thm_img = File::makeDirectory('images/thumbnails', 0777, true);
}
// loop through each image to save and upload
foreach($images as $key => $image) {
//create new instance of Photo class
$newPhoto = new $this->user;
//get file name of image and concatenate with 4 random integer for unique
$filename = rand(1111,9999).time().'.'.$image->getClientOriginalExtension();
//path of image for upload
$org_path = 'images/originals/' . $filename;
$thm_path = 'images/thumbnails/' . $filename;
$newPhoto->image = 'images/originals/'.$filename;
$newPhoto->thumbnail = 'images/thumbnails/'.$filename;
//don't upload file when unable to save name to database
if ( ! $newPhoto->save()) {
return false;
}
// upload image to server
if (($org_img && $thm_img) == true) {
Image::make($image)->fit(900, 500, function ($constraint) {
$constraint->upsize();
})->save($org_path);
Image::make($image)->fit(270, 160, function ($constraint) {
$constraint->upsize();
})->save($thm_path);
}
}
}
return redirect()->action('UserController#index');
}
}
Please suggest me any Image Upload code without updating repositories or suggest me how can I remove issues from this code.
The beginning of time read below link because laravel handled create directory and hash image and put directory
laravel file system
then read file name when stored on directory and holds name on table field when need image retrieve name field and call physical address on server
$upload_id = $request->file('FILENAME');
$file_name = time().$upload_id->getClientOriginalName();
$destination =
$_SERVER["DOCUMENT_ROOT"].'/adminbusinessplus/storage/uploads';
$request->file('FILENAME')->move($destination, $file_name);
$string="123456stringsawexs";
$extension = pathinfo($upload_id, PATHINFO_EXTENSION);
$path = $destination.'/'.$file_name;
$public =1;
$user_id = $request->logedin_user_id;
$hash = str_shuffle($string);
$request->user_id = $request->logedin_user_id;
$request->name = $file_name;
$request->extension = $extension;
$request->path = $path;
$request->public = $public;
$request->hash = $hash;
//$request INSERT INTO MODEL uploads
$file_id = Module::insert("uploads", $request);

The image path in the database is not saved

I want to upload a photo along with a text
But the photo path is not saved inside the table, but the photo is uploaded to the directory
Controller code
namespace App\Http\Controllers;
use App\Http\Requests\singlereq;
use App\infouser;
class singleupload extends Controller
{
public function uploadform()
{
return view('singleupload.upload_form');
}
public function uploadSubmit(singlereq $request)
{
$file = $request->file('imgs');
$file->move('img', $file->getClientOriginalName());
$product = infouser::create($request->all());
return 'OK Upload successful!';
}
}
Used below code. to get the image name and set the table column (your_file) your is column name in your table.
$file = $request->file('imgs');
$file->move('img', $file->getClientOriginalName());
$input = $request->all();
$name = $file->getClientOriginalName();
$input['your_file'] = $name;
$product = infouser::create($input);
return 'OK Upload successful!';

Categories