lonerunner
lonerunner

Reputation: 1322

laravel and image intervention, how to convert all images to jpeg and save from Input::file?

I have read the documentations on image intervention implementation on laravel and i did it successfully i have also read the documentations on how to use the intervention but i can't seems to figure out how to convert all uploaded images to jpg and to save them to desired path with .jpg extension.

This is my code so far, it's working but with few glitches.

 Image::make(Input::file('avatarfull'))->fit('192', '192')->save(base_path() . '/img/' . $imagename .'.jpg')->encode('jpg', 80);

So what i want to achieve is:

This is all from documentation from intervention website.

But in the proccess something gets wrong so i end up with image with no extension at all in my img folder and images doesn't get converted to jpg format. If i upload png it stays png.

For the record i decided to use random string for uploaded images, so

$imagename = str_random(20);

Because i saw that my images get's saved only with random string name, so with uploaded image i end up with a name like:

MSm72XvEWE8AlCUvpHvX

I decided to add .jpg extension manually as you can see

->save(base_path() . '/img/' . $imagename .'.jpg')

And this saves the image with a random string name and .jpg extension at the end.

But problem with converting images to jpg still exists so when i upload png image i get now MSm72XvEWE8AlCUvpHvX.jpg but when i check the image in browser it have transparent background, when i checked properties the image is still png, it just have forced .jpg name because i added that to the save().

Is there a way so all the images gets converted to jpg and also to automatically write the image name with .jpg extension or i have to name that manually ?

Upvotes: 12

Views: 34653

Answers (2)

Good Muyis
Good Muyis

Reputation: 147

The save() method accepts 3 parameters according to the API Documentation here

save([string $path, [int $quality], [string $format]])

So you can call it this way

->save(base_path().'/img/'.$imagename.'.jpg', 'jpg')

You can also specify image quality this way (probably to reduce size)

->save(base_path().'/img/'.$imagename.'.jpg', '85', 'jpg')

Upvotes: 0

cecilozaur
cecilozaur

Reputation: 705

I think you need to call encode() before you call save() as save actually saves the image to disk.

$jpg = (string) Image::make('public/foo.png')->encode('jpg', 75);

Documentation: http://image.intervention.io/api/encode

Upvotes: 22

Related Questions