1
0
mirror of https://github.com/e107inc/e107.git synced 2025-01-17 04:38:27 +01:00

Thumbnail tests added for cropping and PNG files.

This commit is contained in:
Cameron 2020-12-06 08:03:03 -08:00
parent 68a0c8940c
commit 1f38b3b3dd
4 changed files with 212 additions and 28 deletions

View File

@ -54,6 +54,13 @@ class e_thumbnail
public function init($pref)
{
$this->parseRequest();
if(!empty($this->_request['noinit']))
{
return null;
}
$this->_watermark = array(
'activate' => vartrue($pref['watermark_activate'], false),
'text' => vartrue($pref['watermark_text']),
@ -68,7 +75,8 @@ class e_thumbnail
$this->_thumbQuality = vartrue($pref['thumbnail_quality'],65);
$this->parseRequest();
}
/**
@ -289,7 +297,6 @@ class e_thumbnail
elseif(!empty($this->_request['ah']))
{
//Typically gives a better result with images of people than adaptiveResize().
//TODO TBD Add Pref for Top, Bottom, Left, Right, Center?
$thumb->adaptiveResizeQuadrant((integer) vartrue($this->_request['aw'], 0), (integer) vartrue($this->_request['ah'], 0), 'T');
}
else
@ -318,28 +325,21 @@ class e_thumbnail
$this->_watermark['font'] = $tp->createConstants($this->_watermark['font'], 'mix');
$this->_watermark['font'] = realpath($tp->replaceConstants($this->_watermark['font'],'rel'));
$thumb->watermarkText($this->_watermark);
// $thumb->WatermarkText($this->_watermark); // failing due to phpThumb::
}
// echo "hello";
//exit;
// set cache
$thumb->save($cache_filename);
$this->_request = array();
$this->_request = array(); // reset the request.
if($this->_debug === true)
if($this->_debug === true) // return the cache file path for testing.
{
return $cache_filename;
}
// show thumb
$thumb->show();

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

View File

@ -77,50 +77,65 @@
'ah' => 200,
),
// default image size
6 => array (
'src' => 'e_PLUGIN/gallery/images/butterfly.jpg',
'w' => 0,
'h' => 0,
),
// TODO Find a way to test that the images have been cropped correctly. (see below)
/*
7 => array (
'src' => 'e_PLUGIN/gallery/images/butterfly.jpg',
'aw' => '600',
'ah' => '200',
'aw' => 600,
'ah' => 200,
'c' => 't', // crop from top
),
8 => array (
'src' => 'e_PLUGIN/gallery/images/butterfly.jpg',
'aw' => '600',
'ah' => '200',
'aw' => 600,
'ah' => 200,
'c' => 'c', // crop at center
),
9 => array (
'src' => 'e_PLUGIN/gallery/images/butterfly.jpg',
'aw' => '600',
'ah' => '200',
'aw' => 600,
'ah' => 200,
'c' => 'b', // crop at bottom
),
10 => array (
'src' => 'e_PLUGIN/gallery/images/butterfly.jpg',
'aw' => '200',
'ah' => '400',
'aw' => 200,
'ah' => 400,
'c' => 'l', // crop left
),
11 => array (
'src' => 'e_PLUGIN/gallery/images/butterfly.jpg',
'aw' => '200',
'ah' => '400',
'aw' => 200,
'ah' => 400,
'c' => 'r', // crop right
),
*/
// PNG at default size.
12 => array (
'src' => 'e_IMAGE/logo.png',
'w' => 0,
'h' => 0,
// 'c' => 'r', // crop right
),
// Resize up a PNG
13 => array (
'src' => 'e_IMAGE/logo.png',
'w' => 400,
'h' => 0,
// 'c' => 'r', // crop right
),
);
@ -130,14 +145,183 @@
$this->thm->setRequest($val);
$this->thm->checkSrc();
list($file,$ext) = explode(".",$val['src']);
$generatedImage = $this->thm->sendImage();
$storedImage = $this->thumbPath."image_".$index.".".$ext;
$actual = getimagesize($generatedImage);
$expected = getimagesize($this->thumbPath."image_".$index.".jpg");
$compareMachine = new compareImages($storedImage);
$diff = $compareMachine->compareWith($generatedImage);
$this->assertSame($expected,$actual, "Image Index #".$index." failed the check");
$status = ($diff < 5);
// $actual = getimagesize($generatedImage);
// $expected = getimagesize($storedImage);
$this->assertTrue($status, "Image Index #".$index." failed the image-comparison check");
}
}
}
/**
* @author ThaoNv - 2016
* Fast PHP compare images
* https://github.com/nvthaovn/CompareImage
* ---------------------------
* @todo Move this class to an appropriate location.
* */
class compareImages
{
public $source = null;
private $hasString = '';
function __construct($source)
{
$this->source = $source;
}
private function mimeType($i)
{
/*returns array with mime type and if its jpg or png. Returns false if it isn't jpg or png*/
$mime = getimagesize($i);
$return = array($mime[0], $mime[1]);
switch ($mime['mime']) {
case 'image/jpeg':
$return[] = 'jpg';
return $return;
case 'image/png':
$return[] = 'png';
return $return;
default:
return false;
}
}
private function createImage($i)
{
/*retuns image resource or false if its not jpg or png*/
$mime = $this->mimeType($i);
if ($mime[2] == 'jpg') {
return imagecreatefromjpeg($i);
} else
if ($mime[2] == 'png') {
return imagecreatefrompng($i);
} else {
return false;
}
}
private function resizeImage($source)
{
/*resizes the image to a 8x8 squere and returns as image resource*/
$mime = $this->mimeType($source);
$t = imagecreatetruecolor(8, 8);
$source = $this->createImage($source);
imagecopyresized($t, $source, 0, 0, 0, 0, 8, 8, $mime[0], $mime[1]);
return $t;
}
private function colorMeanValue($i)
{
/*returns the mean value of the colors and the list of all pixel's colors*/
$colorList = array();
$colorSum = 0;
for ($a = 0; $a < 8; $a++) {
for ($b = 0; $b < 8; $b++) {
$rgb = imagecolorat($i, $a, $b);
$colorList[] = $rgb & 0xFF;
$colorSum += $rgb & 0xFF;
}
}
return array($colorSum / 64, $colorList);
}
private function bits($colorMean)
{
/*returns an array with 1 and zeros. If a color is bigger than the mean value of colors it is 1*/
$bits = array();
foreach ($colorMean[1] as $color) {
$bits[] = ($color >= $colorMean[0]) ? 1 : 0;
}
return $bits;
}
public function compareWith($tagetImage)
{
$tagetString = $this->hasString($tagetImage);
if ($tagetString) {
return $this->compareHash($tagetString);
}
return 100;
}
/**
* Hash String from image. You can save this string to database for reuse
* @param $image
* @return String 64 character
*/
private function hasString($image)
{
$i1 = $this->createImage($image);
if (!$i1) {
return false;
}
$i1 = $this->resizeImage($image);
imagefilter($i1, IMG_FILTER_GRAYSCALE);
$colorMean1 = $this->colorMeanValue($i1);
$bits1 = $this->bits($colorMean1);
$result = '';
for ($a = 0; $a < 64; $a++) {
$result .= $bits1[$a];
}
return $result;
}
/**
* Get current image hash String
* */
public function getHasString()
{
if ($this->hasString == '') {
$this->hasString = $this->hasString($this->source);
}
return $this->hasString;
}
/**
* Get hash String from image url
* ex: $imageHash = $this->hasStringImage('http://media.com/image.jpg');
* @param $image
* @return false|String
*/
public function hasStringImage($image)
{
return $this->hasString($image);
}
/**
* Compare current image with an image hash String
* @param $imageHash
* @return int different rates . if different rates < 10 => duplicate image
*/
public function compareHash($imageHash)
{
$sString = $this->getHasString();
if (strlen($imageHash) == 64 && strlen($sString) == 64) {
$diff = 0;
$sString = str_split($sString);
$imageHash = str_split($imageHash);
for($a = 0; $a < 64; $a++) {
if ($imageHash[$a] != $sString[$a]) {
$diff++;
}
}
return $diff;
}
return 64;
}
}