Gerald Fullam - 2011-05-06 23:15:15 -
In reply to message 1 from Michael Schulz
Here is how I worked with it:
Step 1: Include the class in your main PHP file
<? include 'img_class.php'; ?>
Step 2: Input your images dynamically and/or inline
// Dynamic image input gets img path from query string
// ex. "?src=IMAGEPATH.jpg"
$testImg = $_GET['src'];
// Invoke image class with one inline image
// (which is the image I am always comparing to in my script)
// and the dynamic image path
$diff = new imagediff('Mugshot_view.gif', $testImg);
// Do something with the comparison result
// You can print it
print ($diff->diff() * 100 ).'%';
// Or store it in a variable for use elsewhere
$similarity = ($diff->diff() * 100).'%'
/======================================================/
I modified the class to work for my needs. One problem I found was that I needed to use a URL of an image, not just an local path. I modified the class by removing the realpath() functions and the Exception:
function __construct($img1, $img2)
{
$this->image1['path'] = $img1;
$this->image2['path'] = $img2;
/* BYPASS EXCEPTION
if($this->image1['path'] === false || $this->image2['path'] === false)
{
throw new Exception('Image "'.htmlspecialchars( $this->image1 ? $img2 : $img1 ).'" not found!');
}
else
{
$this->image1['type'] = $this->imagetyte($this->image1['path']);
$this->image2['type'] = $this->imagetyte($this->image2['path']);
}
*/
/* TRY THIS INSTEAD OF EXCEPTION */
$this->image1['type'] = $this->imagetyte($this->image1['path']);
$this->image2['type'] = $this->imagetyte($this->image2['path']);
/* END BYPASS */
}
/======================================================/
Another problem was that the image URLs I was using were dynamically generated images that did not have file extensions. Looking for the mime type is the more reliable alternative: I invoked the getimagesize() function which returns the mime type as well. See changes below:
private function imagetyte($imgname)
{
$file_info = getimagesize($imgname);
if(!empty ($file_info['mime']))
{
$filetype = preg_replace('/image\//','',$file_info['mime']);
$func = 'imagecreatefrom' . $filetype;