Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hashing an image from a remote location in PHP

Tags:

php

Using a PHP script, I want to compare two images. One of the images is located on my server, and one is located on an external website. I have tried to compare the hashes of the two images to each other. Unfortunately, this only works when the two images are saved on my server. How can I make this work?

<?php

$localimage = sha1_file('image.jpg');

$imagelink = file_get_contents('http://www.google.com/image.jpg');  
$ext_image = sha1_file($imagelink);

if($localimage == $ext_image){
    //Do something
}

?>
like image 758
P.Yntema Avatar asked Dec 19 '22 00:12

P.Yntema


1 Answers

If you are using php 5.1+ (which I hope) you can just write :

<?php

$localimage = sha1_file('image.jpg');
$ext_image = sha1_file('http://www.google.com/image.jpg');

if($localimage == $ext_image){
    //Do something
}

?>

As sha1_file will work on remote wrappers.

Quote from PHP doc at https://secure.php.net/manual/en/function.sha1-file.php

5.1.0 Changed the function to use the streams API. It means that you can use it with wrappers, like sha1_file('http://example.com/..')

like image 183
Flunch Avatar answered Jan 01 '23 15:01

Flunch