【问题标题】:Hashing images regardless of metadata散列图像而不考虑元数据
【发布时间】:2017-07-12 16:33:43
【问题描述】:

我的系统接受以下格式的图像文件:png、jpg、gif。同一个文件可能带有不同的元数据集到达我的系统,我需要唯一标识它们,无论他们的元数据。

我尝试使用 PHP 模块 Imagick,读取图像,去除元数据,然后对它们进行哈希处理。

// example_image: https://ibb.co/bVkanv
$image_path = ‘example.jpg’; 
$stripped_image_path = ‘stripped_example.jpg’;

$img = new Imagick($image_path);
$img->stripImage();
$img->writeImage($stripped_image_path);

$image_hash = sha1_file($stripped_image_path);

尽管如此,这种方法并不能正常工作,因为哈希值不同:

// example_image: https://ibb.co/bVkanv
$original_image = ‘Example.jpg’;

$image1_path = ‘example1.jpg’; 
$image2_path = ‘example2.jpg’; 

$stripped_image1_path = ‘stripped_example1.jpg’; 
$stripped_image2_path = ‘stripped_example2.jpg’; 

$imagick_1 = new Imagick($original_image);
$imagick_1->setImageProperty('Exif:Make', 'Imagick_1');
$imagick_1->writeImage($image1_path);

$imagick_2 = new Imagick($original_image);
$imagick_2->setImageProperty('Exif:Make', 'Imagick_2');
$imagick_2->writeImage($image2_path);

// Now example1.jpg and example2.jpg SHOULD be the same image with different metadata
    $imagick_1 = new Imagick($image1_path);
    $imagick_1->stripImage();
    $imagick_1->writeImage($stripped_image1_path);

    $imagick_2 = new Imagick($image2_path);
    $imagick_2->stripImage();
    $imagick_2->writeImage($stripped_image2_path);

    $hash = sha1_file($image_path); 
    $stripped_hash1 = sha1_file($stripped_image1_path);
    $stripped_hash2 = sha1_file($stripped_image2_path);

$hash
88f454a5f768d633f9d5b8fbba73cdc9dfa46f59

$stripped_hash1
fd385aa6ebcf8018a725598df945f925d8e05d58

$stripped_hash2
d9c9bca4f4433556d5c4c0d62ce06de06920e69b

我怎样才能实现我的目标?

【问题讨论】:

标签: php image hash imagick


【解决方案1】:

我终于用 Imagick 解决了这个问题。

$image = new \imagick($path);
$hash = $image->getImageSignature();

现在,如果在同一张图片中元数据发生了变化,那么哈希值将是相同的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-24
    • 2023-01-30
    • 2018-09-07
    • 1970-01-01
    相关资源
    最近更新 更多