jenssegers / imagehash

🌄 Perceptual image hashing for PHP
https://jenssegers.com
MIT License
1.99k stars 176 forks source link

Unable to init from given binary data to 80.jpg #44

Open mashirou1234 opened 5 years ago

mashirou1234 commented 5 years ago

Hi. I am developing on Laravel 5.5 project using laradock.

When outputting image file names sequentially, an error is output for a specific image. The content of the error is "Unable to init from given binary data." But, hashing a specific image will cause an error.

80.jpg
80_image.jpg
801 ... 809.jpg
801 ... 809_image.jpg

In this way, if the prefix is ​​"80" an error will be output. This phenomenon occurs when outputting images in sequential order.

At present, it is solved by attaching "image_" to the prefix, but I think that it is not the root solution.

If you look at the contents of the error, It is an error in "/vendor/intervention/image/src/Intervention/Image/Gd/Decoder.php"

Decoder.php Error Message

     * Initiates new image from binary data
     *
     * @param  string $data
     * @return \Intervention\Image\Image
     */
    public function initFromBinary($binary)
    {
        $resource = @imagecreatefromstring($binary);

        if ($resource === false) { <--- $resource is false so true
             throw new \Intervention\Image\Exception\NotReadableException(
                "Unable to init from given binary data."
            );
        }

        $image = $this->initFromGdResource($resource);
        $image->mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $binary);

        return $image;
    }

It is an error of the library used by imagehash, so it may not be the contents to ask here. Please pardon.

Laravel Error Message

#60 Intervention\Image\Exception\NotReadableException in /var/www/my-project/vendor/intervention/image/src/Intervention/Image/Gd/Decoder.php:113
#59 Intervention\Image\Gd\Decoder:initFromBinary in /var/www/my-project/vendor/intervention/image/src/Intervention/Image/AbstractDecoder.php:326
#58 Intervention\Image\AbstractDecoder:init in /var/www/my-project/vendor/intervention/image/src/Intervention/Image/AbstractDriver.php:64
#57 Intervention\Image\AbstractDriver:init in /var/www/my-project/vendor/intervention/image/src/Intervention/Image/ImageManager.php:52
#56 Intervention\Image\ImageManager:make in /var/www/my-project/vendor/jenssegers/imagehash/src/ImageHash.php:40
#55 Jenssegers\ImageHash\ImageHash:hash in /var/www/my-project/app/Http/UseCases/EncodeUseCaseImpl.php:27
#54 App\Http\UseCases\EncodeUseCaseImpl:encodeDifferenceHash in /var/www/my-project/app/Http/Controllers/HomeController.php:145
#53 App\Http\Controllers\HomeController:upload in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Routing/Controller.php:54
#52 call_user_func_array in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Routing/Controller.php:54
#51 Illuminate\Routing\Controller:callAction in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php:45
#50 Illuminate\Routing\ControllerDispatcher:dispatch in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Routing/Route.php:212
#49 Illuminate\Routing\Route:runController in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Routing/Route.php:169
#48 Illuminate\Routing\Route:run in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Routing/Router.php:658
#47 Illuminate\Routing\Router:Illuminate\Routing{closure} in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:30
#46 Illuminate\Routing\Pipeline:Illuminate\Routing{closure} in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php:41
#45 Illuminate\Routing\Middleware\SubstituteBindings:handle in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149
#44 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline{closure} in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#43 Illuminate\Routing\Pipeline:Illuminate\Routing{closure} in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php:68
#42 Illuminate\Foundation\Http\Middleware\VerifyCsrfToken:handle in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149
#41 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline{closure} in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#40 Illuminate\Routing\Pipeline:Illuminate\Routing{closure} in /var/www/my-project/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php:49
#39 Illuminate\View\Middleware\ShareErrorsFromSession:handle in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149
#38 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline{closure} in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#37 Illuminate\Routing\Pipeline:Illuminate\Routing{closure} in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php:63
#36 Illuminate\Session\Middleware\StartSession:handle in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149
#35 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline{closure} in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#34 Illuminate\Routing\Pipeline:Illuminate\Routing{closure} in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php:37
#33 Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse:handle in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149
#32 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline{closure} in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#31 Illuminate\Routing\Pipeline:Illuminate\Routing{closure} in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php:66
#30 Illuminate\Cookie\Middleware\EncryptCookies:handle in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149
#29 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline{closure} in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#28 Illuminate\Routing\Pipeline:Illuminate\Routing{closure} in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:102
#27 Illuminate\Pipeline\Pipeline:then in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Routing/Router.php:660
#26 Illuminate\Routing\Router:runRouteWithinStack in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Routing/Router.php:635
#25 Illuminate\Routing\Router:runRoute in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Routing/Router.php:601
#24 Illuminate\Routing\Router:dispatchToRoute in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Routing/Router.php:590
#23 Illuminate\Routing\Router:dispatch in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:176
#22 Illuminate\Foundation\Http\Kernel:Illuminate\Foundation\Http{closure} in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:30
#21 Illuminate\Routing\Pipeline:Illuminate\Routing{closure} in /var/www/my-project/vendor/barryvdh/laravel-debugbar/src/Middleware/InjectDebugbar.php:65
#20 Barryvdh\Debugbar\Middleware\InjectDebugbar:handle in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149
#19 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline{closure} in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#18 Illuminate\Routing\Pipeline:Illuminate\Routing{closure} in /var/www/my-project/vendor/fideloper/proxy/src/TrustProxies.php:56
#17 Fideloper\Proxy\TrustProxies:handle in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149
#16 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline{closure} in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#15 Illuminate\Routing\Pipeline:Illuminate\Routing{closure} in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php:30
#14 Illuminate\Foundation\Http\Middleware\TransformsRequest:handle in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149
#13 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline{closure} in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#12 Illuminate\Routing\Pipeline:Illuminate\Routing{closure} in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php:30
#11 Illuminate\Foundation\Http\Middleware\TransformsRequest:handle in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149
#10 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline{closure} in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#9 Illuminate\Routing\Pipeline:Illuminate\Routing{closure} in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php:27
#8 Illuminate\Foundation\Http\Middleware\ValidatePostSize:handle in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149
#7 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline{closure} in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#6 Illuminate\Routing\Pipeline:Illuminate\Routing{closure} in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php:46
#5 Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode:handle in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149
#4 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline{closure} in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#3 Illuminate\Routing\Pipeline:Illuminate\Routing{closure} in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:102
#2 Illuminate\Pipeline\Pipeline:then in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:151
#1 Illuminate\Foundation\Http\Kernel:sendRequestThroughRouter in /var/www/my-project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:116
#0 Illuminate\Foundation\Http\Kernel:handle in /var/www/my-project/public/index.php:55

use source

<?php

namespace App\Http\UseCases;

use File;
use FFMpeg;
use Storage;
use Jenssegers\ImageHash\ImageHash;
use Jenssegers\ImageHash\Implementations\DifferenceHash;

class EncodeUseCaseImpl implements EncodeUseCase
{
    public function encodeDifferenceHash(array $getImageList) : array
        {
            $hasher = new ImageHash(new DifferenceHash());
            $count = \count($getImageList);
            for ($num = 0; $num < $count; ++$num) {
                $hash[] = $hasher->hash($getImageList[$num])->toHex();
            //error image data 80_image.jpg or 800_image.jpg or 801...809_image.jpg
            }
            return $hash;
        }
...
}

My development environment supported jpeg on GD

SnapCrab_NoName_2019-5-14_21-8-8_No-00

Lucassifoni commented 5 years ago

HI @mashirou1234,
Could you please upload the picture that causes this crash, if it isn't confidential ?

mashirou1234 commented 5 years ago

Hi @Lucassifoni

A crashed image and a set of images. The same phenomenon occurs even when renaming is performed.

80 photos.zip

Thank you in advance.

Lucassifoni commented 5 years ago

Does this one trigger the same problem ?

80.jpg.zip

mashirou1234 commented 5 years ago

Sorry, it's late. I checked the received image data and it was an error.

Lucassifoni commented 5 years ago

Hi @mashirou1234 I tried hashing it with a minimal setup :

{
    "name": "test/imagehash",
    "type": "project",
    "require": {
        "jenssegers/imagehash": "^0.6.0"
    },
}
<?php

require __DIR__ . '/vendor/autoload.php';

use Jenssegers\ImageHash\ImageHash;
use Jenssegers\ImageHash\Implementations\DifferenceHash;

$hasher = new ImageHash(new DifferenceHash());

echo $hasher->hash("80.jpg");
lucas@funghi ~/B/imagehash> php script.php
733939321849317a

The image itself doesn't seem to trigger a bug. Can you try this minimal setup too ? If it still doesn't work, we'll see with your php install.

Thanks

richard-bridgeman commented 1 year ago

Hello, I would just like to add to this that I had the same problem with the exact same error in Decoder.php.

I knew it wasn't the image because the wierd thing was it was working in one project and not the other. I managed to solve it by fixing the GD install for PHP from:

RUN docker-php-ext-configure gd \

To:

RUN docker-php-ext-configure gd --with-jpeg --with-webp \

We're obviously using a docker system here. But that was the issue and now hashing works again.