I am using Image Intervention to merge two images in Controller.
Now I want to show this image in view file but I get this error.
Intervention\Image\Exception\NotReadableException Image source not readable
Here are my codes.
Controller
use App\Models\User;
use InterventionImage;
public function qrcode(User $user)
{
$qrcode_path = ('public/storage/qrcode/' . $user->random . '.png');
$qrcode_img = InterventionImage::make($qrcode_path);
$qrcode_template_path = ('public/storage/home/qrcode_template.png');
$qrcode_template_img = InterventionImage::make($qrcode_template_path);
$qrcode_merged = $qrcode_template_img->insert($qrcode_img, 'center');
return view('test.myqrcode')->with([
'user' => $user,
'qrcode_template_img' => $qrcode_merged
]);
}
view
<img src="{{$qrcode_merged}}">
I appreciate if you could tell me what I'm doing wrong or what I should do. Any help would be appreciated as I have tried multiple methods with no success.
you need to save the image first
and the value of the src attribute must be the path to your image, not an instance of Intervention\Image\Image
make sure you saved the first image in 'your_root_project/storage/app/public/qrcode'
, the second in 'your_root_project/storage/app/public/home'
And already run command "php artisan storage:link"
$qrcode_path = public_path('/storage/qrcode/' . $user->random . '.png');
$qrcode_img = \Intervention\Image\Facades\Image::make($qrcode_path);
$qrcode_template_path = public_path('/storage/home/qrcode_template.png');
$qrcode_template_img = \Intervention\Image\Facades\Image::make($qrcode_template_path);
$your_image_name = "merged_image_name";
$path_show_in_view = 'storage/qrcode/' . $your_image_name . '.png';
$qrcode_merged->save(public_path("/storage/qrcode/" . $your_image_name . ".png"));
return view('test.myqrcode')->with([
'user' => $user,
'path_show_in_view' => $path_show_in_view
]);
view
<img src="{{asset($path_show_in_view)}}">
Hi there, Is it absolutely necessary to save the image? I want to do it without saving the image.
you can encode to base64 if you don't want to save it
$qrcode_path = public_path('/storage/qrcode/' . $user->random . '.png');
$qrcode_img = \Intervention\Image\Facades\Image::make($qrcode_path);
$qrcode_template_path = public_path('/storage/home/qrcode_template.png');
$qrcode_template_img = \Intervention\Image\Facades\Image::make($qrcode_template_path);
$qrcode_merged = $qrcode_template_img->insert($qrcode_img, 'center');
return view('test.myqrcode')->with([
'user' => $user,
'qrcode_merged' => $qrcode_merged
]);
view
<img src="{{$qrcode_merged->encode('data-url')}}"/>
Related
Im trying to save a resized copy of an image that has been uploaded, but Image Interventions doesent seem to know in what root directory to begin in, or maybe it is just me that dosent know how to configure it propertly.
This is my code in AvatarImageController#store
public function store(Request $request, Game $game)
{
if ($request->hasFile('game_image')) {
$request->validate([
'game_image' => 'mimes:jpeg,png|max:1014'
]);
$file = $request->file('game_image');
$filename_thumbnail = "thumb_". $file->hashName();
$path_full = $request->file('game_image')->store('images/test_folder');
$path_thumb = $request->file('game_image')->storeAs('images/test_folder/thumbs', $filename_thumbnail);
// resize image
$path_thumb = Intervention::make("./storage/app/public/" . $path_thumb)->resize(300, 200);
$path_thumb->save();
$image = new Image;
$image->full = basename($path_full);
$image->thumb = $filename_thumbnail;
$game->images()->save($image);
return back()->with('success', "Success!! Image uploaded.");
}else{
return back()->with('success', 'Ooops.. something went wrong.');
}
abort(500, 'Could not upload image :(');
}
The error i receive:
Intervention\Image\Exception\NotReadableException
Image source not readable
I suspect this is because im not in the correct path, because of these lines from above function:
// resize image
$path_thumb = Intervention::make("./storage/app/public/" . $path_thumb)->resize(300, 200);
$path_thumb->save();
And it is a real hassel working with explicit paths when using git as deployment.
You can make image directly like this:
$uploadedFile = $request->file('game_image');
$image = Image::make($uploadedFile);
Resize it:
$image = $image->resize(300, 200);
and save it like this:
$image_data = $image->encode('jpg')->__toString();
$relative_file_path = 'images/test_folder/';
Storage::put($relative_file_path, $image_data)
use the storage() helper?
Intervention::make(storage_path('app/public/") . $path_thumb)
I have made an app where user adds two images from back end one is black & white and the other colored which definitely takes more space on the server now I am writing a function which can convert colored image to gray scale this way the load on the server will reduce now I am stuck in a situation when I use intervention library it always give error that file is empty can anyone help me resolve what I am doing wrong here is my code that I am currently using.
public function download(Request $request) {
$input = Input::all();
$sheet = Sheet::find($request->id);
if ($input['color-type'] == 'color') {
$file = public_path() . "/large/s/" . $sheet->sheet_f_id . '-s.jpg';
return Response::download($file);
} else {
$file = public_path() . "/large/s/" . $sheet->sheet_f_id . '-s.jpg';
$image = Image::make($file);
$grayScale = $image->greyscale();
return Response::download($grayScale);
}
}
What is it that I am doing wrong here.
You need to do
$image = Image::make(public_path($file));
I am using Laravel and Intervention to handle a file upload from the user, I have installed Intervention using Composer but when I try to use some of its functions I get this error message Class 'Intervention\Image\Facades\Image' not found I have checked my app.php file and I have added the correct lines of code to aliases and providers but now I am not sure what is the problem
Here is my function
public function postAvatarUpload(Request $request)
{
$this->validate($request, [
'image' => 'required|image|max:3000|mimes:jpeg,jpg,png',
]);
$user = Auth::user();
$usersname = $user->username;
$file = $request->file('image');
// $ext = $file->getClientOriginalExtension();
$ext= Input::file('image')->getClientOriginalExtension();
$filename = $usersname . '.' . $ext;
if (Storage::disk('public')->has($usersname)) {
Storage::delete($usersname);
}
Storage::disk('public')->put($filename, File::get($file));
$path = public_path('app/public/'. $filename);
Auth::user()->update([
'image' => $path,
]);
$resizedImg = Image::make($path)->resize(200,200);
// $ext = $file->getClientOriginalExtension();
return redirect()->route('profile.index',
['username' => Auth::user()->username]);
}
You also need to import it at the top of the file, after the namespace. Since you say that you've set the facade up, all you need to do is:
use Image;
Add facade and provider as described in the documentation. Then run composer dumpauto -o command.
Then add use Image to your class, or use it like this:
$resizedImg = \Image::make($path)->resize(200,200);
I am trying to store an image in cakephp 3.0. I am only able to save the filename in db, however, unable to store the actual file on the server. Need help
Form:
echo $this->Form->create('User', array('url' => array('action' => 'create'), 'enctype' => 'multipart/form-data'));
echo $this->Form->input('upload', array('type' => 'file'));
Images controller:
*/
public function add()
{
$image = $this->Images->newEntity();
//Check if image has been uploaded
if(!empty($this->request->data['Images']['upload']['name']))
{
$file = $this->request->data['Images']['upload']; //put the data into a var for easy use
$ext = substr(strtolower(strrchr($file['name'], '.')), 1); //get the extension
$arr_ext = array('jpg', 'jpeg', 'gif'); //set allowed extensions
//only process if the extension is valid
if(in_array($ext, $arr_ext))
{
//do the actual uploading of the file. First arg is the tmp name, second arg is
//where we are putting it
move_uploaded_file($file['tmp_name'], WWW_ROOT . 'img' . $file['name']);
//prepare the filename for database entry
$this->data['Images']['image'] = $file['name'];
}
}
if ($this->request->is('post')) {
$image = $this->Images->patchEntity($image, $this->request->data);
if ($this->Images->save($image)) {
$this->Flash->success('The image has been saved.');
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error('The image could not be saved. Please, try again.');
}
}
$this->set(compact('image'));
$this->set('_serialize', ['image']);
}
For those who are looking for the answer just modify this line :
move_uploaded_file($file['tmp_name'], WWW_ROOT . 'img' .DS. $file['name']);
DS is a Directory Separator.
Welcome on stackoverflow!
Please check this question:
cakePHP 3.0 uploading images
This will help you, it's a good plugin for uploading images:
http://cakemanager.org/docs/utils/1.0/behaviors/uploadable/
I am trying to renaming uploaded file to same as ID of table Slider for unique name
public function postAddSlider(){
$title = Input::get('title');
$image = Input::file('image');
$link = Input::get('link');
$description = Input::get('description');
$filename = $image->getClientOriginalName(); //<-- i want to change that filename to be id of table Slider
$uploadSuccess = Input::file('image')->move(base_path().'/public/assets/slider/', $filename);
if($uploadSuccess){
$slider = Slider::create(array(
'image' => $uploadSuccess->getRealPath(),
'title' => $title,
'link' => $link,
'description' => $description
));
if($slider){
return Redirect::route('slider-add-get')
->with('message','Slider Added Successfully');
}
}
else{
return Redirect::route('slider-add-get')
->with('message','File Upload Error');
}
}
or do you have other solution to renaming for unique name?
I have no idea what you're schema is like, but if you want to get the id of a model, you have to save it first.
Perhaps you could do something like this:
$slide = new Slide();
$slide->title = Input::get('title');
$slide->url = $temporaryurl;
...etc...
$slide->save();
//once the slide is saved, we can access its id
$slide->url = 'slide-' . $slide->id . $file->getClientOriginalExtension();
$slide->save(); //updates the new url
Or, if you're just looking to generate unique file names, you could use the original filename + the timestamp... Could possibly result in duplicates... very unlikely though (that 2 images would be uploaded with the same filename within the same second).
Or, perhaps, the slide title (stripped of illegal characters, spaces replaced with dashes, etc), plus the timestamp.