thequang
thequang

Reputation: 21

Laravel optimize image by spatie/image - Class 'League\Glide\ServerFactory' not found when using save() method

I used laravel spaie/image to optimize images, but it shows issue: Class 'League\Glide\ServerFactory' not found. This occur when I used save() method. I tried many ways to fix it but no effect.

public static function optimize(string $url, ?string $format = null, ?int $quality = null): array
    {
        try {
            // Download the image from the URL.
            $contents = file_get_contents($url);
            
            // Create a temporary file to save the image to.
            $tempFile = tempnam(sys_get_temp_dir(), "sw-optimized-image-". Carbon::now()->timestamp. "-". Str::random(5));
            file_put_contents($tempFile, $contents);
            
            // Create an optimizer chain to optimize the image.
            $optimizerChain = OptimizerChainFactory::create();
            $optimizerChain->optimize($tempFile);
            
            // Get the MIME type of the optimized image.
            $mimeType = mime_content_type($tempFile);
            
            // If a format was specified, convert the image to that format.
            if ($format) { // && $format !== pathinfo($url, PATHINFO_EXTENSION)
                $image = \Spatie\Image\Image::load($tempFile)->format(Manipulations::FORMAT_WEBP)->save('hahaha.webp');
                // $image->manipulate(function($manipulations) use ($quality, $format) {
                //     $manipulations->format($format);
                //     $manipulations->quality($quality);
                // });
                // $image->save();
                dd($image);
                // Update the MIME type to match the new format.
                $mimeType = "image/{$format}";
            }
            
            // Read the optimized image data from the temporary file.
            $optimizedContents = file_get_contents($tempFile);
            
            $finfo = new finfo(FILEINFO_MIME_TYPE);
            $mimeType = $finfo->buffer($optimizedContents);

            dd($mimeType);
            // Get information about the optimized image.
            $optimizedInfo = getimagesizefromstring($optimizedContents);
            $optimizedWidth = $optimizedInfo[0];
            $optimizedHeight = $optimizedInfo[1];
            $optimizedSize = strlen($optimizedContents);

            // Delete the temporary file.
            unlink($tempFile);
            
            // Return information about the optimized image.
            return [
                'format' => $format,
                'mimeType' => $mimeType,
                'width' => $optimizedWidth,
                'height' => $optimizedHeight,
                'size' => $optimizedSize,
                'dataUri' => 'data:' . $mimeType . ';base64,' . base64_encode($optimizedContents),
            ];
        } catch (\Exception $exception) {
            dd($exception);
            app('sentry')->captureException($exception);
            return [];
        }
    }

I tried reinstalling the spatie/image package I tried updating it to the latest version

Upvotes: 0

Views: 364

Answers (2)

I fixed the error by first cleaning the composer cache like this:

composer clear-cache

Then, I run:

composer update

Upvotes: 0

Khang Tran
Khang Tran

Reputation: 2315

It's strange. If you installed spatie/image, League\Glide\ServerFactory should exist because league/glide package required in composer.json of spatie/image package.

"require": {
        "php": "^8.0",
        "ext-exif": "*",
        "ext-mbstring": "*",
        "ext-json": "*",
        "league/glide": "^2.2.2",
        "spatie/image-optimizer": "^1.1",
        "spatie/temporary-directory": "^1.0|^2.0",
        "symfony/process": "^3.0|^4.0|^5.0|^6.0"
    } 

Try checking if league/glide folder exists in vendor or not. If not, try install it:

composer require league/glide

If it exists, try:

php artisan cache:clear

Upvotes: 0

Related Questions