如何从Imagick对象获取sha1哈希?

Kalle H.在大门口

该网站有点像画廊。但是要防止重复输入。我要匹配他们。这不会是100%防弹的图像匹配,但是对我来说,它是绝对完美的解决方案。

唯一的问题是,我不知道Imagick对象获取sha1的正确方法 $image

这就是我现在所拥有的,并且确实会产生哈希。但这与我服务器中的不匹配。在服务器中,将图像优化到最小缩略图的过程相同。除此以外,file_put_contents($root, $image);每个图像处理块的末尾都有。但是我不认为问题出在哪里,我认为问题可能出在$image函数内部的对象中,我缺少了一些东西sha1()sha1($image->rendercurrentimage())..这样的东西

<?
$img_url = 'someimgfile.jpg';

# Step 1 = Original file hash - This is all ok
$source_hash = sha1_file($img_url);

$image = new Imagick($img_url);
# file_put_contents($source_root, $image);

$image->gaussianBlurImage(0, 0.05);
$image->setCompression(Imagick::COMPRESSION_JPEG);
$image->setCompressionQuality(90);
$image->setImageFormat('jpeg');
$image->scaleImage(215, 0);
# file_put_contents($thumbnail_root, $image);

# Step 2 = Get the thumbnail hash - results in a non matching hash vs. DB hash
$thumbnail_hash = sha1($image);

$image->setCompressionQuality(75); 
$image->cropThumbnailImage(102, 102);
# file_put_contents($smallthumbnail_root, $image);

# Step 3 = Get the even smaller thumbnail hash - results in a non matching hash vs. DB hash
$smallthumbnail_hash = sha1($image);

# now query to DB to check against all 3 hashes: $source_hash | $thumbnail_hash | $smallthumbnail_hash
# DB has lets say 1000 images, with source hash, thumbnail hash and small thumbnail hash saved in them

# NOTE: The process of scaling images as they enter the DB, is exactly the same, expect there are file_put_contents($root, $image); in between them.. I put them in and commented out, to show you the locations

正如我上面所说。我在服务器3种方式中都具有针对匹配的哈希值。因此,原始的缩略图甚至更小的缩略图。那些是用sha1_file()功能创建的我想基本上模仿空洞过程,但不要将文件保存在$ root中,以防它重复并且在那里被拒绝并重定向到相匹配的条目。

如果您想知道为什么我要匹配缩略图。这是因为,我的测试表明,如果原始文件的大小等可能不同。则创建的缩略图也可以很好地匹配。还是我错了?如果我有相同的图像,请使用3种不同的尺寸。我将它们缩小到100px的宽度。他们的哈希值会一样吗?

结论我不得不稍微重写一下原始的图像处理程序。但是基本上我认为我的代码中仍然缺少一段内容$image->stripImage();或者其他的东西。虽然开始获得更好的结果。似乎在服务器中保留哈希值的最佳方法是:

$hash = sha1(base64_encode($image->getImageBlob()));

我的测试还确认,file_put_contents($thumbnail_root, $image);通过哈希获取sha1_file($image_root);并不会更改哈希值。

从缩小到拇指大小的较大图像中,我还获得了更多匹配结果。

大卫·朗(David Long)

因为您的问题是您不想为要执行的每个步骤在文件系统上创建文件,所以我建议您抓住这些步骤的blob内容并为其创建哈希。例如:

<?php
//quick and dirty image creation to demonstrate my point
$image = new Imagick();
$image->newImage(100, 100, new ImagickPixel('red'));
$image->setImageFormat('png');

//base64 encode our blob and then generate a sha1 hash
$thumbnail = base64_encode( $image->getImageBlob() );
echo sha1($thumbnail);

如果您要使两个不同(原始)尺寸的图像相互匹配,则可能会遇到重采样问题。例如,我有一只猴子的图片,该猴子的像素为200px正方形,另一幅看似相同的猴子为400px的正方形,如果我重新采样到200px,则图像将不会总是匹配。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章