Search code examples
phplaravelintervention

Intervention Image encode file extension


I'm trying to change all images uploaded to png format. I'm using the Intervention Image package for Laravel, and calling the encode function. Image Files are not changing to .png

Here is my upload script: (Everything is uploading, resizing and appears to be compressing. Just not converting to a png file)

if($request->hasFile('listing_image')){
    $classifiedImg = $request->file('listing_image');
    $filename = 'listing'.'-'.uniqid().'.'.$classifiedImg->getClientOriginalExtension();

    Image::make($classifiedImg)->encode('png', 65)->resize(760, null, function ($c) {
        $c->aspectRatio();
        $c->upsize();
        })->save(public_path('/images/users/listing-images/' . $filename));

    $classified->listing_image = $filename;
    $classified->save();
}else{
    $classified->save();
}

Am I doing something wrong in this section:

Image::make($classifiedImg)->encode('png', 65)->resize(760, null, function ($c)...

OR is this causing the issue:

getClientOriginalExtension();

Solution

  • Thanks BagusTesa, you were correct. This was causing the issue.

    getClientOriginalExtension();
    

    To get the extension to convert. I needed to add the extension to the file name.

    Change this line:

    $filename = 'listing'.'-'.uniqid().'.'.$classifiedImg->getClientOriginalExtension()
    

    To This:

    $filename = 'listing' . '-' . uniqid() . '.png';