Search code examples
phplaravelimage-optimization

Error on uploading images with size greater than 1MB


I used to upload images on the server without any problem, but after I installed spatie larave-image-optimizer I ran into some problems. I can upload any image that is less then 1MB, but anything over will break it

I tried to console log or dump an error so that I can see what's going on, but I don't understand what the error message is telling me.

$validate = Validator::make($request->all(), [
        'docs.*' => [
            'sometimes',
            'image',
            'mimes:jpeg,png,jpg,gif,svg',
            'max:51200',
        ],
    ]);

    if ($validate->fails()) {
        return response(['error' => 'Error!'], 422);
    }

    if ($request->file('docs')) {
        foreach ($request->file('docs') as $doc => $image) {
            $image->storeAs(
                '/folder/' . $id , //I get this id from the params
                $image->getClientOriginalName(),
                'public'
            );
        }
    }

This route goes through the image optimizer middleware

    Route::middleware('optimizeImages')->group(function () {
        Route::post("url", "...Controller@store");
    });`

I get this error:

    exception: "InvalidArgumentException"
    file: "{app_path}\vendor\spatie\image-> optimizer\src\Image.php"
    line: 14
    message: "`` does not exist"

Why does the 1MB image uplod, but one that is 2MB+ not? And how do I fix this behavior?

Keep in mind that before installing this package everything worked. I uploaded many images that are 4MB+, but I need them opimized so I use my server's disk efficiently.

EDIT #1 Error if i don't use the middleware:

    exception: "RuntimeException"
    file: "{app_path}\vendor\symfony\var-dumper\Server\Connection.php"
    line: 63
    message: "stream_socket_sendto(): A request to send or receive data was 
    disallowed because the socket is not connected and (when sending on a 
    datagram socket using a sendto call) no address was supplied.↵↵"

Solution

  • go to your php.ini file and change those

    upload_max_filesize = 10M
    post_max_size = 10M
    

    after that make sure you restart your apache server