When a user uploads a picture I want to store it in multiple formats.
My code for handling the image:
$img = Image::make($file)->encode('png');
if($img->width()>3000){
$img->resize(3000, null, function ($constraint) {
$constraint->aspectRatio();
});
}
if($img->height()>3000){
$img->resize(null, 3000, function ($constraint) {
$constraint->aspectRatio();
});
}
$uid = Str::uuid();
$fileName = Str::slug($item->name . $uid).'.png';
$high = clone $img;
Storage::put( $this->getUploadPath($bathroom->id, $fileName, "high"), $high);
$med = clone $img;
$med->fit(1000,1000);
Storage::put( $this->getUploadPath($bathroom->id, $fileName, "med"), $med);
$thumb = clone $img;
$thumb->fit(700,700);
Storage::put( $this->getUploadPath($bathroom->id, $fileName, "thumb"), $thumb);
As you see I tried a few variations.
I also tried:
$thumb = clone $img;
$thumb->resize(400, 400, function ($constraint) {
$constraint->aspectRatio();
});
Storage::put( $this->getUploadPath($fileName, "thumb"), $thumb);
The getUploadPath function:
public function getUploadPath($id, $filename, $quality = 'high'){
return 'public/img/bathroom/'.$id.'/'.$quality.'/'.$filename;
}
I want the image to fit in xpx x xpx without scaling or downgrading quality.
The images are created and stored as expected but the image is not resized. How can I make the image resize?
You need to stream it ($thumb->stream();) before saving by Storage facade as below:
$thumb = clone $img;
$thumb->resize(400, 400, function ($constraint) {
$constraint->aspectRatio();
});
$thumb->stream();
Storage::put( $this->getUploadPath($fileName, "thumb"), $thumb);
You need to use the save($img) method to actually create the resized image.
This is what the official docs have to say about this -
To create actually image data from an image object, you can access
methods like encode to create encoded image data or use save to write
an image into the filesystem. It's also possible to send an HTTP
response with current image data.
Image::make('foo.jpg')->resize(300, 200)->save('bar.jpg');
Details on the method in the official docs - http://image.intervention.io/api/save
if ($request->hasFile('image-file')) {
$image = $request->file('image-file');
$fileName = 'IMG'.time() . '.' . $image->getClientOriginalExtension();
$img = Image::make($image->getRealPath());
$img->resize(400, 400, function ($constraint) {
$constraint->aspectRatio();
});
$img->stream();
Storage::disk('local')->put('public/img/bathroom/'.'/'.$fileName, $img, 'public');
}
Hope this works for you!!
Related
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.
I want to crop and resize image and save into multiple folders. But the only a single image is saving in the folder (first one)
public function update(Request $request)
{
$image = $request->file('file');
$name = time().''.mt_rand(100, 200).'_1280x768.'.$image->getClientOriginalExtension();
$name1 = time().''.mt_rand(100, 200).'_320x240.'.$image->getClientOriginalExtension();
$image1= $image->move('public/storage/uploadimage/main', $name);
$image2= $image->move('public/storage/uploadimage/thumb', $name1);
$img = Image::make($imagename);
$img->fit(1280, 768, function ($constraint) {
$constraint->upsize();
});
$img->insert('https://kafesta.com/public/images/kafesta-10.png', 'center');
$img->save($imagename);
$img1 = Image::make($imagename1);
$img1->fit(320, 240, function ($constraint) {
$constraint->upsize();
});
$img1->insert('https://kafesta.com/public/images/kafesta-10.png', 'center');
$img1->save($imagename1);
}
I have Upload File Controller method and it is slow on live server, I don't know why? Can you review if my code is structured okay? Is it in the right order? How to optimize my code pls?
$id = Auth::user()->id;
$image=$request->file('file');
$fileName=time().uniqid(rand()).'.'.$image->getClientOriginalExtension();
//Resize image here
$background = Image::canvas(500, 500);
$background->fill('#fff');
$lphoto = Image::make($image->getRealpath());
$lphoto->orientate();
$lphoto->resize(500, 500, function ($constraint) {
$constraint->upsize();
$constraint->aspectRatio();
});
$lphoto->encode();
$sphoto = Image::make($image->getRealpath());
$sphoto->orientate();
$sphoto->fit(186,180);
$sphoto->encode(null, 90);
$background->insert($lphoto, 'center');
$background->encode();
Storage::disk('public')->put( 'images/'.$year.'/'.$month."/".$day."/".$id."/".$fileName, $background);
Storage::disk('public')->put( 'images/'.$year.'/'.$month."/".$day."/".$id.'/small-'.$fileName, $sphoto);
Following a tutorial I did this:
public function store(Request $request)
{
$file = Input::file('imagen1');
$image = \Image::make(\Input::file('imagen1'));
$path = public_path().'/thumbnails/';
$image->save($path.$file->getClientOriginalName());
$image->resize(null, 300, function ($constraint) {
$constraint->aspectRatio();
});
$image->save($path.'thumb_'.$file->getClientOriginalName());
$thumbnail = new Thumbnail();
$thumbnail->image = $file->getClientOriginalName();
$thumbnail->save();
$request->user()->propiedades()->create($request->all());
return redirect('profile#propiedades');
}
And my problem is that the image is being save in a "temporal" path and not the real one. So when i go to my table 'Propiedades' It just shows this:
The right direction is this one
So my question is how do i make intervention image saves the real path? Thanks in advance
UPDATE
Ok. Now thanks to Nazmul Hasan i am seeing this in my database.
The only thing left is that it saves the name of the file. So i can go to my blade and do {{ $propiedades->imagen1 }}
Thanks!!
UPDATE 2
$file = Input::file('imagen1');
$ext = time() . '.' . $file->getClientOriginalExtension();
$path = public_path('thumbnails/' . $ext);
$image = \Image::make(\Input::file('imagen1'));
$image->save($path.$file->getClientOriginalName());
$image->resize(400, null, function ($constraint) {
$constraint->aspectRatio();
});
$image->save($path.'thumb_'.$file->getClientOriginalName());
$thumbnail = new Thumbnail();
$thumbnail->image = $file->getClientOriginalName();
$thumbnail->save();
$inputs = $request->all();
$inputs['imagen1'] = $path;
$request->user()->propiedades()->create($inputs);
return redirect('profile#propiedades');
AND NOW IT SAVES RIGHT THE IMG PATH BUT THE IMAGE IS NOT BEING SAVE CORRECLTY
Your problem is in this line
$request->user()->propiedades()->create($request->all());
You does not update image upload path in $request variable
For this reason $request->all() save temporary image path
You can try this
$file = Input::file('imagen1');
$image = \Image::make(\Input::file('imagen1'));
$path = public_path().'/thumbnails/';
$image->save($path.$file->getClientOriginalName());
$image->resize(null, 300, function ($constraint) {
$constraint->aspectRatio();
});
$image->save($path.'thumb_'.$file->getClientOriginalName());
$thumbnail = new Thumbnail();
$thumbnail->image = $path.'thumb_'.$file->getClientOriginalName();
$thumbnail->save();
$inputs = $request->all()
$inputs['imagen1'] = $path.$file->getClientOriginalName();
$request->user()->propiedades()->create($inputs);
return redirect('profile#propiedades');
I am trying to use laravel intervention plugin. I installed it without problem but can't use it.
I am trying to make a test function which returns resized image, but without success;
I think the problem may be in image path, please help me fix my code.
function test($img)
{
/* $img = Image::make('public/image1.jpg');
$img->resize(300, 200);
return $img; */
$image = Image::make('http://localhost/cms/digital-cms/public/image1.jpg')->resize(200, 200, function ($c) {
$c->aspectRatio();
$c->upsize();
});
return $image;
//$h=200; $w=200;
//return Image::make(public_path('public/image1.jpg')->resize($h, $w)->response('jpg'));
}
//get image
$image=$request->file('image');
//rename image
$input = time().'.'.$image->getClientOriginalExtension();
//your directory to upload
$destinationPath = 'main/images/company';
//save and resize image
$img = Image::make($image->getRealPath());
$img->resize(20,20, function ($constraint) {
$constraint->aspectRatio();
})->save($destinationPath.'/'.$input);
You should use response function on Image class to return the image
$image = Image::make('http://localhost/cms/digital-cms/public/image1.jpg')->resize(200, 200, function ($c) {
$c->aspectRatio();
$c->upsize();
});
return $image->response();
In my case, I was resizing, cropping (fit) but the final image is still the same as original. Found out that I had to add function encode, to produce the manipulated image
return $image->encode('jpg', 80);