Skip to content

Commit

Permalink
Destroy resource after hashing, fixes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
jenssegers committed Feb 28, 2015
1 parent 66b3f18 commit c6ebe6c
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/ImageHash.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,21 @@ public function __construct(Implementation $implementation = null)
*/
public function hash($resource)
{
$resource = is_resource($resource) ? $resource : $this->loadImageResource($resource);
$destroy = false;

if ( ! is_resource($resource))
{
$resource = $this->loadImageResource($resource);
$destroy = true;
}

$hash = $this->implementation->hash($resource);

if ($destroy)
{
imagedestroy($resource);
}

return $hash;
}

Expand All @@ -49,11 +60,8 @@ public function hash($resource)
*/
public function compare($resource1, $resource2)
{
$resource1 = is_resource($resource1) ? $resource1 : $this->loadImageResource($resource1);
$resource2 = is_resource($resource2) ? $resource2 : $this->loadImageResource($resource2);

$hash1 = $this->implementation->hash($resource1);
$hash2 = $this->implementation->hash($resource2);
$hash1 = $this->hash($resource1);
$hash2 = $this->hash($resource2);

return $this->distance($hash1, $hash2);
}
Expand Down Expand Up @@ -99,9 +107,9 @@ public function distance($hash1, $hash2)
*/
protected function loadImageResource($file)
{
if ( ! file_exists($file))
if (is_resource($file))
{
throw new Exception("File was not found: $file");
return $file;
}

try
Expand All @@ -110,7 +118,7 @@ protected function loadImageResource($file)
}
catch (Exception $e)
{
throw new Exception("File is not an image: $file");
throw new Exception("Unable to load file: $file");
}
}

Expand Down

0 comments on commit c6ebe6c

Please sign in to comment.