From e10c3169e544594662c3270932c7aadad055d4e4 Mon Sep 17 00:00:00 2001 From: Scrutinizer Auto-Fixer Date: Sat, 9 Dec 2017 23:36:33 +0100 Subject: [PATCH] Scrutinizer Auto-Fixes (#1533) This commit consists of patches automatically generated for this project on https://scrutinizer-ci.com --- app/Http/Controllers/AdminController.php | 75 +++++++++++++++---- .../views/admin/webtrees1-thumbnails.php | 4 +- vendor/composer/autoload_psr4.php | 1 + vendor/composer/autoload_static.php | 8 ++ vendor/composer/installed.json | 47 ++++++++++++ 5 files changed, 118 insertions(+), 17 deletions(-) diff --git a/app/Http/Controllers/AdminController.php b/app/Http/Controllers/AdminController.php index 241c1e1ea3d..f9a8f5944f5 100644 --- a/app/Http/Controllers/AdminController.php +++ b/app/Http/Controllers/AdminController.php @@ -17,6 +17,7 @@ namespace Fisharebest\Webtrees\Http\Controllers; +use BigV\ImageCompare; use DirectoryIterator; use Fisharebest\Algorithm\MyersDiff; use Fisharebest\Webtrees\Auth; @@ -40,14 +41,16 @@ use Fisharebest\Webtrees\Tree; use Fisharebest\Webtrees\User; use Fisharebest\Webtrees\View; +use Intervention\Image\Image; +use Intervention\Image\ImageManager; use RecursiveDirectoryIterator; use RecursiveIteratorIterator; -use stdClass; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\ResponseHeaderBag; +use stdClass; /** * Controller for the administration pages @@ -1095,7 +1098,7 @@ public function webtrees1ThumbnailsData(Request $request): JsonResponse { $recordsTotal = count($thumbnails); if ($search !== '') { - $thumbnails = array_filter($thumbnails, function(string $thumbnail) use ($search) { + $thumbnails = array_filter($thumbnails, function (string $thumbnail) use ($search) { return stripos($thumbnail, $search) !== false; }); } @@ -1146,10 +1149,10 @@ public function webtrees1ThumbnailsData(Request $request): JsonResponse { return [ - '
' . str_replace('/', '/‌', e($thumbnail_path)), - '
' . str_replace('/', '/‌', e($original_path)), + '', + '', $media, - $status . ' ' . $import . ' ' . $delete, + $status . ' ' . $import . ' ' . $delete . ' ' . @$this->imageDiff($original, $thumbnail), ]; }, $thumbnails); @@ -1824,7 +1827,7 @@ private function findMediaObjectsForMediaFile(string $file): array { /** * Find the original image that corresponds to a (webtrees 1.x) thumbnail file. * - * @param string $thumbanil + * @param string $thumbnail * * @return string */ @@ -1833,7 +1836,7 @@ private function findOriginalFileFromThumbnail(string $thumbnail): string { $original = dirname(dirname($thumbnail)) . '/' . basename($thumbnail); // Second option - a .PNG thumbnail for some other image type - if (substr_compare($original,'.png', -4, 4) === 0) { + if (substr_compare($original, '.png', -4, 4) === 0) { $pattern = substr($original, 0, -3) . '*'; $matches = glob($pattern); if (!empty($matches) && is_file($matches[0])) { @@ -1844,11 +1847,53 @@ private function findOriginalFileFromThumbnail(string $thumbnail): string { return $original; } + /** + * Compare two images, and return a quantified difference. + * + * 0 (different) ... 100 (same) + * + * @param $image1 + * @param $image2 + * + * @return int + */ + private function imageDiff($image1, $image2): int { + $size = 10; + + // Convert images to 10x10 + try { + $manager = new ImageManager; + $image1 = $manager->make($image1)->resize($size, $size); + $image2 = $manager->make($image2)->resize($size, $size); + } catch (\Throwable $ex) { + //var_dump($image1, $image2); + //throw $ex; + return -1; + } + + $max_difference = 0; + // Compare each pixel + for ($x = 0; $x < $size; ++$x) { + for ($y = 0; $y < $size; ++$y) { + // Sum the RGB channels to convert to grayscale. + $pixel1 = $image1->pickColor($x, $y); + $pixel2 = $image2->pickColor($x, $y); + $value1 = $pixel1[0] + $pixel1[1] + $pixel1[2]; + $value2 = $pixel2[0] + $pixel2[1] + $pixel2[2]; + + $max_difference = max($max_difference, abs($value1 - $value2)); + } + } + + // The maximum difference is 3 x 255 = 765 (black versus white). + + return 100 - (int) ($max_difference * 100 / 765); + } /** * Does the thumbnail file appear to be custom generated. - * If yes, we should probably import it. - * If no, we should probably delete it. + * If yes (true), we should probably import it. + * If no (false), we should probably delete it. * * @param string $original * @param string $thumbnail @@ -1874,11 +1919,6 @@ private function isCustomWebtrees1Thumbnail(string $original, string $thumbnail) return true; } - // The same size image? - if ($original_attributes[3] === $thumbnail_attributes[3]) { - return false; - } - // Different aspect ratio? Use exact same algorithm as webtrees 1.x $original_width = $original_attributes[0]; $original_height = $original_attributes[1]; @@ -1886,11 +1926,14 @@ private function isCustomWebtrees1Thumbnail(string $original, string $thumbnail) $thumbnail_height = $thumbnail_attributes[1]; $calculated_height = round($original_height * ($thumbnail_width / $original_width)); - if ($calculated_height !== $thumbnail_height) { + if (abs($calculated_height - $thumbnail_height) > 1) { return true; } - return true; + // Do a pixel-by-pixel comparison + $image_compare = new ImageCompare; + + return $image_compare->compare($original, $thumbnail) >= 10; } /** diff --git a/resources/views/admin/webtrees1-thumbnails.php b/resources/views/admin/webtrees1-thumbnails.php index fea41770ba6..c689293777f 100644 --- a/resources/views/admin/webtrees1-thumbnails.php +++ b/resources/views/admin/webtrees1-thumbnails.php @@ -9,9 +9,11 @@
+
+

- +
diff --git a/vendor/composer/autoload_psr4.php b/vendor/composer/autoload_psr4.php index 87d6580b4fe..7661c058d8d 100644 --- a/vendor/composer/autoload_psr4.php +++ b/vendor/composer/autoload_psr4.php @@ -25,4 +25,5 @@ 'Fisharebest\\Localization\\' => array($vendorDir . '/fisharebest/localization/src'), 'Fisharebest\\ExtCalendar\\' => array($vendorDir . '/fisharebest/ext-calendar/src'), 'Fisharebest\\Algorithm\\' => array($vendorDir . '/fisharebest/algorithm/src'), + 'BigV\\' => array($vendorDir . '/vajiral/php-image-compare/src'), ); diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index f8bacbf5899..e726f9fdc13 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -63,6 +63,10 @@ class ComposerStaticInit6d3b4e102d83b25f563035091d204a43 'Fisharebest\\ExtCalendar\\' => 24, 'Fisharebest\\Algorithm\\' => 22, ), + 'B' => + array ( + 'BigV\\' => 5, + ), ); public static $prefixDirsPsr4 = array ( @@ -142,6 +146,10 @@ class ComposerStaticInit6d3b4e102d83b25f563035091d204a43 array ( 0 => __DIR__ . '/..' . '/fisharebest/algorithm/src', ), + 'BigV\\' => + array ( + 0 => __DIR__ . '/..' . '/vajiral/php-image-compare/src', + ), ); public static $classMap = array ( diff --git a/vendor/composer/installed.json b/vendor/composer/installed.json index d8cc4fe3dc6..28c0a9cde3c 100644 --- a/vendor/composer/installed.json +++ b/vendor/composer/installed.json @@ -1291,5 +1291,52 @@ ], "description": "Symfony HttpFoundation Component", "homepage": "https://symfony.com" + }, + { + "name": "vajiral/php-image-compare", + "version": "1.0.1", + "version_normalized": "1.0.1.0", + "source": { + "type": "git", + "url": "https://github.com/vajiralasantha/PHP-Image-Compare.git", + "reference": "9ea466236c91b8bfa169b3459e3089d2f4b31f7e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/vajiralasantha/PHP-Image-Compare/zipball/9ea466236c91b8bfa169b3459e3089d2f4b31f7e", + "reference": "9ea466236c91b8bfa169b3459e3089d2f4b31f7e", + "shasum": "" + }, + "require": { + "ext-gd": "*", + "php": "^5.5.9 || ^7.0" + }, + "time": "2017-05-04T01:48:54+00:00", + "type": "library", + "installation-source": "dist", + "autoload": { + "psr-4": { + "BigV\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Vajira Lasantha", + "email": "vajee555@gmail.com" + } + ], + "description": "A light weight PHP class that can compare two (jpg/png) images to find if they are similar.", + "keywords": [ + "PHP Library", + "image", + "image compare", + "jpg", + "lightweight", + "png" + ] } ]