laravel-thumbnails icon indicating copy to clipboard operation
laravel-thumbnails copied to clipboard

Errors when image field is not correctly formatted or empty

Open Alwyn-RM opened this issue 3 years ago • 1 comments

I installed the plugin into an existing project which already has some images set in the tables. When I call $image->thumbnail('image','small') it throws an error ErrorException Undefined offset: 1 on line 197 of drh2so4\thumbnail\src\Traits\Thumbnail.php

this is to be expected of course because there's no check after the explode on "-" character if the length is actually higher than 1. So calling [1] on the split will fail.

I tried to prevent the error by using hasThumbnail but this is using the exact same function so not resolved. When I clear the image column for that particular row, it also doesn't work when calling "hasThumbnail".

Again because there's no check if result of this search is null

Code:

public function hasThumbnail($fieldname = 'image', $size = null)
{
    return $this->imageDetail($fieldname, $size)->property->has_thumbnail;
}

I can get around it for now by checking the column value on the model. Maybe if I have time I'll see if I can push a fix but not a lot of time at the moment.

UPDATE Apparently the issue is because some file in my uploads didn;t follow the correct naming (older upload before adding the library). Still consider this a bug. Also wonder how performant this is but that's another discussion :-)

It's fixed when you add the following to drh2so4\thumbnail\src\Traits\Thumbnail.php (line 197)

if (!isset($image_partition[1])) {
 continue;
};

Alwyn-RM avatar Mar 02 '21 14:03 Alwyn-RM

I installed the plugin into an existing project which already has some images set in the tables. When I call $image->thumbnail('image','small') it throws an error ErrorException Undefined offset: 1 on line 197 of drh2so4\thumbnail\src\Traits\Thumbnail.php

this is to be expected of course because there's no check after the explode on "-" character if the length is actually higher than 1. So calling [1] on the split will fail.

I tried to prevent the error by using hasThumbnail but this is using the exact same function so not resolved. When I clear the image column for that particular row, it also doesn't work when calling "hasThumbnail".

Again because there's no check if result of this search is null

Code:

public function hasThumbnail($fieldname = 'image', $size = null)
{
    return $this->imageDetail($fieldname, $size)->property->has_thumbnail;
}

I can get around it for now by checking the column value on the model. Maybe if I have time I'll see if I can push a fix but not a lot of time at the moment.

UPDATE Apparently the issue is because some file in my uploads didn;t follow the correct naming (older upload before adding the library). Still consider this a bug. Also wonder how performant this is but that's another discussion :-)

It's fixed when you add the following to drh2so4\thumbnail\src\Traits\Thumbnail.php (line 197)

if (!isset($image_partition[1])) {
 continue;
};

thank you for the inspection .... your PR will be appriciated ..

pratiksh404 avatar Mar 10 '21 14:03 pratiksh404