[Laravel Intervention Image] Image source not readable

Posted 3 years ago by SergioGregorutti

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

        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) {
            $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) {
            $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;

                    ->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!

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.