I want to crop/resize an image in Laravel (4.2). But it doen't seem to work very well...
So I Want to be able to upload an image, but resize it to a maximum width and height.
How should I do this?
The code I have now is:
//save the image
$destinationPath = 'public/pictures/news';
if (Input::hasFile('img'))
{
$file = Input::file('img');
$file->move('public/pictures/news', $file->getClientOriginalName());
}
So, I want a code in the if() statement, that crops the image. to a specified max-width and max-height.
If someone could help me out, I would be very happy!
Kindes regards,
Robin
use image intervention package it is providing an easier and expressive way to create, edit, and compose images.
https://packagist.org/packages/intervention/image
Related
I'm using the Intervention Image package for manipulating the uploaded image before saving it to the server. I have successfully set the dimension of the image, and it's working fine, but I want to fix the image size, i.e., the image generated should be less than equals to 30KB and should have a resolution of 300 dpi only.
Here is the code, I'm using to resize the uploaded image:
$file = $post->file('profile_image');
$filename = Carbon::now()->timestamp.'_'.$file->getClientOriginalName();
\Image::make($file->getRealPath())->resize(160, 160)->save('uploads/profile/'.$filename);
If my concern is feasible with the Intervention Image package, please give me a solution.
Thanks in advance.
you can try somthing like this
$size = 160;
$img = \Image::make($file->getRealPath())->resize($size, $size)->save('uploads/profile/'.$filename);
while($img->filesize() < "someAmount"){
$size = (int)$size - 20; // adjust based on your need
$img = \Image::make($file->getRealPath())->resize($size, $size)->save('uploads/profile/'.$filename);
}
NOTE this is not correct answer but it is idea to solve this problem
I want to resize an image twice using Intervention.
I have this currently:
$img = Image::make($image_url);
$img_path = public_path() . '/images/';
$img->fit(500, 250);
$img->save($img_path . '/img_250.jpg');
$img = Image::make($image_url);
$img->fit(100, 100);
$img->save($img_path . '/img_100.jpg');
As you can see, I first want to resize the original image to 500x250, and then I want to again resize the original image (not the 500x250 image) to 100x100.
Is there a way to do this without calling Image::make() twice?
Here's the answer:
http://image.intervention.io/api/reset
// create an image
$img = Image::make('public/foo.jpg');
// backup status
$img->backup();
// perform some modifications
$img->resize(320, 240);
$img->invert();
$img->save('public/small.jpg');
// reset image (return to backup state)
$img->reset();
// perform other modifications
$img->resize(640, 480);
$img->invert();
$img->save('public/large.jpg');
I'm posting this to help others who might come across a similar issue. While we can implement #user6421733's answer... There's a better way of handling different sizes of images.
Consider using Intervention's imagecache optional package. You could implement it simply too. http://image.intervention.io/use/url
It can allow you to use urls such as this http://yourhost.com/{route-name}/original/{file-name} and with little or less effort:
When i write a code to display image in blade file that time a image thumbnails made and i can give the every time height,width that time i need image that kind of image thumbnails generates so i can generate multiple thumb of same image in single site.
{{$page->image,100,100)}}
Or
<td><?php if ($page->image) { ?><img src="{{ url('/upload/pages/'.$page->image,100,200) }}"/><?php } ?></td>
Please Give the suggestion how to make this kind of Thumbnails Thanks In advance.
A rational way to do this would be:
Create a controller and a method (or a method on existing controller) that accepts image name, width & height parameters. This method could use Intervention package that was mentioned in the comments. Logic should be - first check if image of specified dimensions exists, if it doesn't - create it (using Intervention, very simple). Then - output the image contents (don't forget to add correct header).
Add this controller/method to routes.php, eg:
Route::get('thumbnails/{image}', 'Controller#getThumbnail');
In your Blade templates you would simply refer to images like '/path/image.jpg?width=200&height=100'. No need to care whether the file with these dimensions already exists or not.
PS. Facebook serves images this way. It's basically like a little proxy server (your method works as a proxy) between user and original image.
you need to require Intervention, then put something like this in your upload controller :
...
$file = $request->file('files');
$extension = $file->getClientOriginalExtension();
Storage::disk('local')->put('/'.$file->getFilename().$extension, File::get($file));
$thumb1 = ImageManagerStatic::make($file->getRealPath())->resize(200, 200, function ($constraint) {
$constraint->aspectRatio(); //maintain image ratio
})->save('thumb1'.$extension);
$thumb2 = ImageManagerStatic::make($file->getRealPath())->resize(400, 400, function ($constraint) {
$constraint->aspectRatio(); //maintain image ratio
})->save('thumb2'.$extension);
...
You may resize that image on the fly with a microservice and cache the thumbnails in a CDN. Please have a look here
<img src="{{ imgProxy('https://your-microservice.com/your-image.jpg', 100, 200) }}"/>
Just for the record, if you are using Laravel, a good option is to use intervention/image and intervention/imagecache packages.
What you need is described on the "URL based image manipulation" section of Intervention Image Docs.
I am getting Image URL from the DB like this "image01-v2-70-70.jpg".
It is already scaled to 70-70, so I am getting it as smaller image.
I want to scale this image to 120-120 from its default 70-70
Thanks in Advance
You can do this with the help of a php extension php image magick
header('Content-type: image/jpeg');
$image = new Imagick('image01-v2-70-70.jpg');
$image->adaptiveResizeImage(120,120);
echo $image;
But this is highly not recomended as you are going from a smaller resolution to a larger resolution which will make the image blurry.
A piece of advice try to make a copy 120X120 resolution from the original image
I have a site where users can upload images. I process these images directly and resize them into 5 additional formats using the CodeIgniter Image Manipulation class. I do this quite efficiently as follow:
I always resize from the previous format, instead of from the original
I resize using an image quality of 90% which about halves the file size of jpegs
The above way of doing things I implemented after advise I got from another question I asked. My test case is a 1.6MB JPEG in RGB mode with a high resolution of 3872 x 2592. For that image, which is kind of borderline case, the resize process in total takes about 2 secs, which is acceptable to me.
Now, only one challenge remains. I want the original file to be compressed using that 90% quality but without resizing it. The idea being that that file too will take half the file size. I figured I could simply resize it to its' current dimensions, but that doesn't seem to do anything to the file or its size. Here's my code, somewhat simplified:
$sourceimage = "test.jpg";
$resize_settings['image_library'] = 'gd2';
$resize_settings['source_image'] = $sourceimage;
$resize_settings['maintain_ratio'] = false;
$resize_settings['quality'] = '90%';
$this->load->library('image_lib', $resize_settings);
$resize_settings['width'] = $imagefile['width'];
$resize_settings['height'] = $imagefile['height'];
$resize_settings['new_image'] = $filename;
$this->image_lib->initialize($resize_settings);
$this->image_lib->resize();
The above code works fine for all formats except the original. I tried debugging into the CI class to see why nothing happens and I noticed that the script detects that the dimensions did not change. Next, it simply makes a copy of that file without processing it at all. I commented that piece of code to force it to resize but now still nothing happens.
Does anybody know how to compress an image (any image, not just jpegs) to 90% using the CI class without changing the dimensions?
I guess you could do something like this:
$original_size = getimagesize('/path/to/original.jpg');
And then set the following options like this:
$resize_settings['width'] = $original_size[0];
$resize_settings['height'] = $original_size[1];
Ok, so that doesn't work due to CI trying to be smart, the way I see it you've three possible options:
Rotate the Image by 360ยบ
Watermark the Image (with a 1x1 Transparent Image)
Do It Yourself
The DIY approach is really simple, I know you don't want to use "custom" functions but take a look:
ImageJPEG(ImageCreateFromString(file_get_contents('/path/to/original.jpg')), '/where/to/save/optimized.jpg', 90);
As you can see, it's even more simpler than using CI.
PS: The snippet above can open any type of image (GIF, PNG and JPEG) and it always saves the image as JPEG with 90% of quality, I believe this is what you're trying to archive.