Skip to content

Commit e10c316

Browse files
scrutinizer-auto-fixerfisharebest
authored andcommitted
Scrutinizer Auto-Fixes (#1533)
This commit consists of patches automatically generated for this project on https://scrutinizer-ci.com
1 parent 2cb301c commit e10c316

File tree

5 files changed

+118
-17
lines changed

5 files changed

+118
-17
lines changed

app/Http/Controllers/AdminController.php

+59-16
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
namespace Fisharebest\Webtrees\Http\Controllers;
1919

20+
use BigV\ImageCompare;
2021
use DirectoryIterator;
2122
use Fisharebest\Algorithm\MyersDiff;
2223
use Fisharebest\Webtrees\Auth;
@@ -40,14 +41,16 @@
4041
use Fisharebest\Webtrees\Tree;
4142
use Fisharebest\Webtrees\User;
4243
use Fisharebest\Webtrees\View;
44+
use Intervention\Image\Image;
45+
use Intervention\Image\ImageManager;
4346
use RecursiveDirectoryIterator;
4447
use RecursiveIteratorIterator;
45-
use stdClass;
4648
use Symfony\Component\HttpFoundation\JsonResponse;
4749
use Symfony\Component\HttpFoundation\RedirectResponse;
4850
use Symfony\Component\HttpFoundation\Request;
4951
use Symfony\Component\HttpFoundation\Response;
5052
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
53+
use stdClass;
5154

5255
/**
5356
* Controller for the administration pages
@@ -1095,7 +1098,7 @@ public function webtrees1ThumbnailsData(Request $request): JsonResponse {
10951098
$recordsTotal = count($thumbnails);
10961099

10971100
if ($search !== '') {
1098-
$thumbnails = array_filter($thumbnails, function(string $thumbnail) use ($search) {
1101+
$thumbnails = array_filter($thumbnails, function (string $thumbnail) use ($search) {
10991102
return stripos($thumbnail, $search) !== false;
11001103
});
11011104
}
@@ -1146,10 +1149,10 @@ public function webtrees1ThumbnailsData(Request $request): JsonResponse {
11461149

11471150

11481151
return [
1149-
'<img src="' . e($thumbnail_url) . '"><br>' . str_replace('/', '/&zwnj;', e($thumbnail_path)),
1150-
'<img src="' . e($original_url) . '"><br>' . str_replace('/', '/&zwnj;', e($original_path)),
1152+
'<img src="' . e($thumbnail_url) . '" title="' . e($thumbnail_path) . '">',
1153+
'<img src="' . e($original_url) . '" title="' . e($original_path) . '">',
11511154
$media,
1152-
$status . ' ' . $import . ' ' . $delete,
1155+
$status . ' ' . $import . ' ' . $delete . ' ' . @$this->imageDiff($original, $thumbnail),
11531156
];
11541157
}, $thumbnails);
11551158

@@ -1824,7 +1827,7 @@ private function findMediaObjectsForMediaFile(string $file): array {
18241827
/**
18251828
* Find the original image that corresponds to a (webtrees 1.x) thumbnail file.
18261829
*
1827-
* @param string $thumbanil
1830+
* @param string $thumbnail
18281831
*
18291832
* @return string
18301833
*/
@@ -1833,7 +1836,7 @@ private function findOriginalFileFromThumbnail(string $thumbnail): string {
18331836
$original = dirname(dirname($thumbnail)) . '/' . basename($thumbnail);
18341837

18351838
// Second option - a .PNG thumbnail for some other image type
1836-
if (substr_compare($original,'.png', -4, 4) === 0) {
1839+
if (substr_compare($original, '.png', -4, 4) === 0) {
18371840
$pattern = substr($original, 0, -3) . '*';
18381841
$matches = glob($pattern);
18391842
if (!empty($matches) && is_file($matches[0])) {
@@ -1844,11 +1847,53 @@ private function findOriginalFileFromThumbnail(string $thumbnail): string {
18441847
return $original;
18451848
}
18461849

1850+
/**
1851+
* Compare two images, and return a quantified difference.
1852+
*
1853+
* 0 (different) ... 100 (same)
1854+
*
1855+
* @param $image1
1856+
* @param $image2
1857+
*
1858+
* @return int
1859+
*/
1860+
private function imageDiff($image1, $image2): int {
1861+
$size = 10;
1862+
1863+
// Convert images to 10x10
1864+
try {
1865+
$manager = new ImageManager;
1866+
$image1 = $manager->make($image1)->resize($size, $size);
1867+
$image2 = $manager->make($image2)->resize($size, $size);
1868+
} catch (\Throwable $ex) {
1869+
//var_dump($image1, $image2);
1870+
//throw $ex;
1871+
return -1;
1872+
}
1873+
1874+
$max_difference = 0;
1875+
// Compare each pixel
1876+
for ($x = 0; $x < $size; ++$x) {
1877+
for ($y = 0; $y < $size; ++$y) {
1878+
// Sum the RGB channels to convert to grayscale.
1879+
$pixel1 = $image1->pickColor($x, $y);
1880+
$pixel2 = $image2->pickColor($x, $y);
1881+
$value1 = $pixel1[0] + $pixel1[1] + $pixel1[2];
1882+
$value2 = $pixel2[0] + $pixel2[1] + $pixel2[2];
1883+
1884+
$max_difference = max($max_difference, abs($value1 - $value2));
1885+
}
1886+
}
1887+
1888+
// The maximum difference is 3 x 255 = 765 (black versus white).
1889+
1890+
return 100 - (int) ($max_difference * 100 / 765);
1891+
}
18471892

18481893
/**
18491894
* Does the thumbnail file appear to be custom generated.
1850-
* If yes, we should probably import it.
1851-
* If no, we should probably delete it.
1895+
* If yes (true), we should probably import it.
1896+
* If no (false), we should probably delete it.
18521897
*
18531898
* @param string $original
18541899
* @param string $thumbnail
@@ -1874,23 +1919,21 @@ private function isCustomWebtrees1Thumbnail(string $original, string $thumbnail)
18741919
return true;
18751920
}
18761921

1877-
// The same size image?
1878-
if ($original_attributes[3] === $thumbnail_attributes[3]) {
1879-
return false;
1880-
}
1881-
18821922
// Different aspect ratio? Use exact same algorithm as webtrees 1.x
18831923
$original_width = $original_attributes[0];
18841924
$original_height = $original_attributes[1];
18851925
$thumbnail_width = $thumbnail_attributes[0];
18861926
$thumbnail_height = $thumbnail_attributes[1];
18871927
$calculated_height = round($original_height * ($thumbnail_width / $original_width));
18881928

1889-
if ($calculated_height !== $thumbnail_height) {
1929+
if (abs($calculated_height - $thumbnail_height) > 1) {
18901930
return true;
18911931
}
18921932

1893-
return true;
1933+
// Do a pixel-by-pixel comparison
1934+
$image_compare = new ImageCompare;
1935+
1936+
return $image_compare->compare($original, $thumbnail) >= 10;
18941937
}
18951938

18961939
/**

resources/views/admin/webtrees1-thumbnails.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
<?= I18N::translate('In webtrees version 1, you could add custom thumbnails to media objects by creating files in the "thumbs" folders.') ?>
1010
<br>
1111
<?= I18N::translate('In webtrees version 2, custom thumbnails are stored as a second media file in the same media object.') ?>
12+
<br>
13+
<?= I18N::translate('If the thumbnail image is the same as the original, you should delete it. If it is a custom image, you should import it.') ?>
1214
</p>
1315

14-
<table class="table table-bordered table-sm table-hover table-responsive datatables wt-fix-table" data-ajax="<?= e(json_encode(['url' => route('admin-webtrees1-thumbs-data')])) ?>" data-server-side="true" data-state-save="true" data-sort="false">
16+
<table class="table table-bordered table-sm table-hover table-responsive datatables wt-fix-table" data-ajax="<?= e(json_encode(['url' => route('admin-webtrees1-thumbs-data')])) ?>" data-server-side="true" data-state-save="true" data-sort="false" data-auto-width="false">
1517
<caption class="sr-only">
1618
<?= I18N::translate('Media objects') ?>
1719
</caption>

vendor/composer/autoload_psr4.php

+1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@
2525
'Fisharebest\\Localization\\' => array($vendorDir . '/fisharebest/localization/src'),
2626
'Fisharebest\\ExtCalendar\\' => array($vendorDir . '/fisharebest/ext-calendar/src'),
2727
'Fisharebest\\Algorithm\\' => array($vendorDir . '/fisharebest/algorithm/src'),
28+
'BigV\\' => array($vendorDir . '/vajiral/php-image-compare/src'),
2829
);

vendor/composer/autoload_static.php

+8
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ class ComposerStaticInit6d3b4e102d83b25f563035091d204a43
6363
'Fisharebest\\ExtCalendar\\' => 24,
6464
'Fisharebest\\Algorithm\\' => 22,
6565
),
66+
'B' =>
67+
array (
68+
'BigV\\' => 5,
69+
),
6670
);
6771

6872
public static $prefixDirsPsr4 = array (
@@ -142,6 +146,10 @@ class ComposerStaticInit6d3b4e102d83b25f563035091d204a43
142146
array (
143147
0 => __DIR__ . '/..' . '/fisharebest/algorithm/src',
144148
),
149+
'BigV\\' =>
150+
array (
151+
0 => __DIR__ . '/..' . '/vajiral/php-image-compare/src',
152+
),
145153
);
146154

147155
public static $classMap = array (

vendor/composer/installed.json

+47
Original file line numberDiff line numberDiff line change
@@ -1291,5 +1291,52 @@
12911291
],
12921292
"description": "Symfony HttpFoundation Component",
12931293
"homepage": "https://symfony.com"
1294+
},
1295+
{
1296+
"name": "vajiral/php-image-compare",
1297+
"version": "1.0.1",
1298+
"version_normalized": "1.0.1.0",
1299+
"source": {
1300+
"type": "git",
1301+
"url": "https://github.com/vajiralasantha/PHP-Image-Compare.git",
1302+
"reference": "9ea466236c91b8bfa169b3459e3089d2f4b31f7e"
1303+
},
1304+
"dist": {
1305+
"type": "zip",
1306+
"url": "https://api.github.com/repos/vajiralasantha/PHP-Image-Compare/zipball/9ea466236c91b8bfa169b3459e3089d2f4b31f7e",
1307+
"reference": "9ea466236c91b8bfa169b3459e3089d2f4b31f7e",
1308+
"shasum": ""
1309+
},
1310+
"require": {
1311+
"ext-gd": "*",
1312+
"php": "^5.5.9 || ^7.0"
1313+
},
1314+
"time": "2017-05-04T01:48:54+00:00",
1315+
"type": "library",
1316+
"installation-source": "dist",
1317+
"autoload": {
1318+
"psr-4": {
1319+
"BigV\\": "src/"
1320+
}
1321+
},
1322+
"notification-url": "https://packagist.org/downloads/",
1323+
"license": [
1324+
"MIT"
1325+
],
1326+
"authors": [
1327+
{
1328+
"name": "Vajira Lasantha",
1329+
"email": "vajee555@gmail.com"
1330+
}
1331+
],
1332+
"description": "A light weight PHP class that can compare two (jpg/png) images to find if they are similar.",
1333+
"keywords": [
1334+
"PHP Library",
1335+
"image",
1336+
"image compare",
1337+
"jpg",
1338+
"lightweight",
1339+
"png"
1340+
]
12941341
}
12951342
]

0 commit comments

Comments
 (0)