Skip to content

Commit

Permalink
Scrutinizer Auto-Fixes (#1533)
Browse files Browse the repository at this point in the history
This commit consists of patches automatically generated for this project on https://scrutinizer-ci.com
  • Loading branch information
scrutinizer-auto-fixer authored and fisharebest committed Dec 10, 2017
1 parent 2cb301c commit e10c316
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 17 deletions.
75 changes: 59 additions & 16 deletions app/Http/Controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace Fisharebest\Webtrees\Http\Controllers;

use BigV\ImageCompare;
use DirectoryIterator;
use Fisharebest\Algorithm\MyersDiff;
use Fisharebest\Webtrees\Auth;
Expand All @@ -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
Expand Down Expand Up @@ -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;
});
}
Expand Down Expand Up @@ -1146,10 +1149,10 @@ public function webtrees1ThumbnailsData(Request $request): JsonResponse {


return [
'<img src="' . e($thumbnail_url) . '"><br>' . str_replace('/', '/&zwnj;', e($thumbnail_path)),
'<img src="' . e($original_url) . '"><br>' . str_replace('/', '/&zwnj;', e($original_path)),
'<img src="' . e($thumbnail_url) . '" title="' . e($thumbnail_path) . '">',
'<img src="' . e($original_url) . '" title="' . e($original_path) . '">',
$media,
$status . ' ' . $import . ' ' . $delete,
$status . ' ' . $import . ' ' . $delete . ' ' . @$this->imageDiff($original, $thumbnail),
];
}, $thumbnails);

Expand Down Expand Up @@ -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
*/
Expand All @@ -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])) {
Expand All @@ -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
Expand All @@ -1874,23 +1919,21 @@ 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];
$thumbnail_width = $thumbnail_attributes[0];
$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;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion resources/views/admin/webtrees1-thumbnails.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
<?= I18N::translate('In webtrees version 1, you could add custom thumbnails to media objects by creating files in the "thumbs" folders.') ?>
<br>
<?= I18N::translate('In webtrees version 2, custom thumbnails are stored as a second media file in the same media object.') ?>
<br>
<?= 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.') ?>
</p>

<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">
<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">
<caption class="sr-only">
<?= I18N::translate('Media objects') ?>
</caption>
Expand Down
1 change: 1 addition & 0 deletions vendor/composer/autoload_psr4.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
);
8 changes: 8 additions & 0 deletions vendor/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ class ComposerStaticInit6d3b4e102d83b25f563035091d204a43
'Fisharebest\\ExtCalendar\\' => 24,
'Fisharebest\\Algorithm\\' => 22,
),
'B' =>
array (
'BigV\\' => 5,
),
);

public static $prefixDirsPsr4 = array (
Expand Down Expand Up @@ -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 (
Expand Down
47 changes: 47 additions & 0 deletions vendor/composer/installed.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "[email protected]"
}
],
"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"
]
}
]

0 comments on commit e10c316

Please sign in to comment.