Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • Take the uploaded image from Input::file('avatarfull')
  • Use intervention package to grab the image Image::make(Input::file('avatarfull')
  • Resize the image to desired width and height ->fit()
  • Save the image to desired path ->save
  • And encode all the images to jpg format ->encode

This is all from documentation from intervention website.

  • fit() should crop and resize image to desired width and height
  • save() will move the image and save to desired path
  • encode() should convert any image (bmp, png, gif) to jpg format.

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 ?

like image 927
lonerunner Avatar asked Jan 20 '15 01:01

lonerunner


1 Answers

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

like image 85
cecilozaur Avatar answered Nov 17 '22 05:11

cecilozaur