AwadGorg's avatar

Image upload error when uploadig .png images but upload .jpg images

Hello I have multi images upload system, it works fine when I upload images with ext of JPG but when I try to upload .png images it throw back an error

Intervention\Image\Exception\NotReadableException
Unsupported image type. GD driver is only able to decode JPG, PNG, GIF or WebP files. 

and am sure that the images are in .png format and I get the same error when upload mix between .jpg and PNG images here is the code I am using, and I am using Traits to upload images

public function uploadImagesThumbs($request, $imFolder, $game){
        foreach ($request->file('g_high_img') as $imgThumb) {  
            $imgFullName = $imgThumb->getClientOriginalName();
            $explodeImgFullName = explode('.', $imgFullName);
            // get the image name without the ext
            $imgThumbName = current($explodeImgFullName);
            $imgThumbName = preg_replace('/[^a-zA-Z0-9]/', '-', strtolower($imgThumbName));
            //the file name that well be saved
            $imgThumbNameToStore = $imgThumbName.'_'.rand().'.webp';
            Storage::put('public/'.$imFolder.'/'.$game->g_page_link.'/thumbnails/'.$imgThumbNameToStore,
            fopen($imgThumb, 'r+'));
            $image = Image::make($imgThumb);
            $image->resize(700,400)->save(storage_path('app/public/'.$imFolder.'/'.
            $game->g_page_link.'/thumbnails/'. $imgThumbNameToStore));
            $image->resize(100,100)->save(storage_path('app/public/'.$imFolder.'/'.
            $game->g_page_link.'/thumbnails/sm_'. $imgThumbNameToStore));
            $this->imgThumbNameArray[] = $imgThumbNameToStore;
        } 
    }

And I am allowing the user to upload .png images

0 likes
4 replies
Snapey's avatar

You save the image with a webp extension (even though it might contain jpg and png) and then try to reload it with intervention?

Why not just load it straight into intervention rather than saving to disk first with the wrong extension?

AwadGorg's avatar

Hi, I do you mean I should remove

Storage::put('public/'.$imFolder.'/'.$game->g_page_link.'/thumbnails/'.$imgThumbNameToStore,
            fopen($imgThumb, 'r+'));

any way I removed the code above and still the same error

Intervention\Image\Exception\NotReadableException
Unsupported image type. GD driver is only able to decode JPG, PNG, GIF or WebP files. 

the error only appear when I upload images with .png ext and covert these images to WebP but when I upload JPG images and covert to WebP everything works fine

Snapey's avatar

You are not converting them to webp - you are only changing the file extension.

malco's avatar

I had a similar error uploading .png, I solved it by expanding: -post_max_size -upload_max_filesize -memory_limit -max_execution_time

Please or to participate in this conversation.