【发布时间】:2014-09-03 10:48:19
【问题描述】:
该网站有点像画廊。但要防止重复条目。我想匹配他们。它不会是 100% 防弹图像匹配,但对于我的需要,它绝对是完美的解决方案。
唯一的问题是,我不知道从 Imagick $image 对象获取 sha1 的正确方法。
这就是我现在所拥有的,它确实会产生一个哈希值。但它与我在服务器中的那些不匹配。在服务器中,将图像优化到最小的缩略图的过程相同。除此之外,每个图像处理块的末尾都有file_put_contents($root, $image);。但我认为问题不存在,我认为问题可能在于我在sha1() 函数内的$image 对象中遗漏了一些东西。比如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); 获取哈希值不会改变哈希值。
我还从缩小到拇指大小的较大图像中获得了更多匹配结果。
【问题讨论】:
-
为什么要努力?如果图像真的是相同,那么只需覆盖它。
-
好吧,如果图像是一样的,那我就不需要它了。但是,虽然原始源图像匹配已经对我有所帮助。我还需要匹配缩略图,因为这个三重检查会得到更好的结果。
-
@Danack,如果它是重复的。我已经有了答案。我的问题是不同的,涉及想象的对象。我坚信,这将在未来帮助搜索者。在发布之前,我花了几个小时寻找解决方案——就像我一直做的那样,正如 SO 指南所建议的那样。
-
你使用的库是一个实现细节——理论是一样的。