Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

SergioGregorutti's avatar

[Laravel Intervention Image] Image source not readable

Hi all,

I'm trying to import images from an external server with the Intervention Image package and if the image exists and it's readable it works fine.

But some images are not readable and I get this error:

NotReadableException in AbstractDecoder.php line 302:
Image source not readable

I know that if the image doesn't exists I can't create it, but I want to say to the script "Hey, if the images is not readable continuous with the next image".

This is my script:

public function importCommerceImage() {
        $commerces = DB::table('am_avisos')->where('Estado', 'Habilitado')->where('FotoImportada', null)->take
        (5)->get();

        foreach ($commerces as $oldCommerce) {
            $commerce = Commerce::find($oldCommerce->Id);

            // Path:
            $path = 'sites/test/'.'images/commerces/'.$commerce->id.'/';

            // Set Image name:
            $imageName = time().'-'.$oldCommerce->Foto;

            // Create Image (normal size):
            $image = Image::make('http://www.mundohurlingham.com/imagenes/avisos/'.$oldCommerce->Foto)->resize(1000, null, function ($constraint) {
                $constraint->aspectRatio();
            });
            $image = $image->stream();
            Storage::disk('s3')->put($path.$imageName, $image->__toString());

            // Create Image (thumbnail):
            $image = Image::make('http://www.mundohurlingham.com/imagenes/avisos/'.$oldCommerce->Foto)->resize(200, null, function ($constraint) {
                $constraint->aspectRatio();
            });
            $image = $image->stream();
            Storage::disk('s3')->put($path.'thumbnail/'.$imageName, $image->__toString());

            // Create Image (thumbnail square):
            $image = Image::make('http://www.mundohurlingham.com/imagenes/avisos/'.$oldCommerce->Foto)->fit(150);
            $image = $image->stream();
            Storage::disk('s3')->put($path.'thumbnail_square/'.$imageName, $image->__toString());

            // Update Commerce:
            $exists = Storage::disk('s3')->exists($path.$imageName);
            if ($exists) {
                $image = $this->getFileFullPath($path.$imageName);
                $imageSrc = $imageName;
                $imageThumbnail = $this->getFileFullPath($path.'thumbnail/'.$imageName);
                $imageThumbnailSquare = $this->getFileFullPath($path.'thumbnail_square/'.$imageName);

                $commerce->image = $image;
                $commerce->image_src = $imageSrc;
                $commerce->image_thumbnail = $imageThumbnail;
                $commerce->image_thumbnail_square = $imageThumbnailSquare;
                $commerce->save();

                DB::table('am_avisos')
                    ->where('Id', $oldCommerce->Id)
                    ->update(['FotoImportada' => 'Si']);
            }
        }

        flash()->success('Las fotos de los negocios han sido importadas exitosamente.');
        return redirect(route('admin.configuration'));
    }

How can I manage the exception to continuous with the foreach loop?

Thanks in advance!

0 likes
2 replies
ajxu's avatar

You can do something like this:

use Intervention\Image\Exception\NotReadableException;

foreach($commerces as $oldCommerce) {
    try 
    {
        $image = Image::make('http://www.mundohurlingham.com/imagenes/avisos/'.$oldCommerce->Foto);
    }
    catch(NotReadableException $e)
    {
        // If error, stop and continue looping to next iteration
        continue;
    }
    // If no error ...
    $image->resize(200, null, function ($constraint) 
    {
        $constraint->aspectRatio();
    });
}
2 likes
nikssa23's avatar

Hi, I have the same problem. When I change image driver everything works fine.

Try to change image driver from app/config/packages/intervention/image/config.php from GD to Imagick

return array(

/*
|--------------------------------------------------------------------------
| Image Driver
|--------------------------------------------------------------------------
|
| Intervention Image supports "GD Library" and "Imagick" to process images
| internally. You may choose one of them according to your PHP
| configuration. By default PHP's "GD Library" implementation is used.
|
| Supported: "gd", "imagick"
|
*/

'driver' => 'imagick'

);

1 like

Please or to participate in this conversation.