Skip to content

Commit

Permalink
Merge pull request #86 from pataar/upgrade_intervention
Browse files Browse the repository at this point in the history
Upgrade minimum PHP version and dependencies
  • Loading branch information
jenssegers authored Jun 17, 2024
2 parents a23404b + c492dbd commit 643d8d6
Show file tree
Hide file tree
Showing 17 changed files with 111 additions and 147 deletions.
28 changes: 14 additions & 14 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: ["7.1", "7.2", "7.3", "7.4", "8.0"]
extensions: ["gd", "imagick"]
name: PHP ${{ matrix.php-versions }} - ${{ matrix.extensions }}
php-version: ["8.1", "8.2", "8.3"]
extension: ["gd, :imagick", "imagick, :gd"] # the ':" prefix is used to unload the other extension
name: PHP ${{ matrix.php-version }} - ${{ matrix.extension }}
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
extensions: ${{ matrix.extensions }}
php-version: ${{ matrix.php-version }}
extensions: ${{ matrix.extension }}
coverage: xdebug

- name: Check environment
Expand All @@ -27,15 +27,15 @@ jobs:
composer --version
- name: Get composer cache directory
id: composercache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Cache dependencies
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: ${{ steps.composercache.outputs.dir }}
key: ${{ matrix.os }}-composer-${{ matrix.php-versions }}-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ matrix.os }}-composer-${{ matrix.php-versions }}-
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ matrix.os }}-composer-${{ matrix.php-version }}-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ matrix.os }}-composer-${{ matrix.php-version }}-

- name: Install dependencies
run: composer install --prefer-dist
Expand All @@ -49,8 +49,8 @@ jobs:
- name: Upload coverage to Codecov
env:
OS: ${{ matrix.os }}
PHP: ${{ matrix.php-versions }}
uses: codecov/codecov-action@v1
PHP: ${{ matrix.php-version }}
uses: codecov/codecov-action@v3
with:
file: build/logs/clover.xml
env_vars: OS,PHP
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor
composer.lock
.phpunit.cache
.phpunit.result.cache
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ This code was inspired/based on:
Requirements
------------

- PHP 7.1 or higher
- PHP 8.1 or higher
- The [gd](http://php.net/manual/en/book.image.php) or [imagick](http://php.net/manual/en/book.imagick.php) extension
- Optionally, install the [GMP](http://php.net/manual/en/book.gmp.php) extension for faster fingerprint comparisons

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
}
],
"require": {
"php": "^7.1|^8.0",
"intervention/image": "^2.4"
"php": "^8.1",
"intervention/image": "^3.3"
},
"require-dev": {
"phpunit/phpunit": "^7|^8|^9",
"phpunit/phpunit": "^10",
"php-coveralls/php-coveralls": "^2.0"
},
"suggest": {
Expand Down
8 changes: 5 additions & 3 deletions docker/gd.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
ARG PHP_VERSION=7.2
ARG COMPOSER_VERSION=1.8
ARG PHP_VERSION=8.1
ARG COMPOSER_VERSION=2

FROM composer:${COMPOSER_VERSION}
FROM composer:${COMPOSER_VERSION} as composer
FROM php:${PHP_VERSION}-cli

COPY --from=composer /usr/bin/composer /usr/bin/composer

RUN apt-get update && \
apt-get install libpng-dev libjpeg-dev --no-install-recommends -qy && \
rm -rf /var/lib/apt/lists/* && \
Expand Down
8 changes: 5 additions & 3 deletions docker/gmp.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
ARG PHP_VERSION=7.2
ARG COMPOSER_VERSION=1.8
ARG PHP_VERSION=8.1
ARG COMPOSER_VERSION=2

FROM composer:${COMPOSER_VERSION}
FROM composer:${COMPOSER_VERSION} as composer
FROM php:${PHP_VERSION}-cli

COPY --from=composer /usr/bin/composer /usr/bin/composer

RUN apt-get update && \
apt-get install libgmp-dev libpng-dev libjpeg-dev --no-install-recommends -qy && \
rm -rf /var/lib/apt/lists/* && \
Expand Down
11 changes: 7 additions & 4 deletions docker/imagick.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
ARG PHP_VERSION=7.2
ARG COMPOSER_VERSION=1.8
ARG PHP_VERSION=8.1
ARG COMPOSER_VERSION=2

FROM composer:${COMPOSER_VERSION}
FROM composer:${COMPOSER_VERSION} as composer
FROM php:${PHP_VERSION}-cli

COPY --from=composer /usr/bin/composer /usr/bin/composer

RUN apt-get update && \
apt-get install libmagickwand-dev --no-install-recommends -qy && \
rm -rf /var/lib/apt/lists/* && \
ln -s /usr/include/x86_64-linux-gnu/gmp.h /usr/include/gmp.h && \
pecl install imagick && docker-php-ext-enable imagick
pecl install imagick && \
docker-php-ext-enable imagick
30 changes: 11 additions & 19 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="pHash">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist addUncoveredFilesFromWhitelist="false">
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="tests/bootstrap.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<testsuites>
<testsuite name="pHash">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">src/</directory>
</include>
</source>
</phpunit>
6 changes: 3 additions & 3 deletions src/Hash.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ class Hash implements JsonSerializable
*
* @var string
*/
protected $binaryValue;
protected string $binaryValue;

/**
* Hash will be split in several integers if longer than PHP_INT_SIZE
*
* @var int[]|null
*/
protected $integers = null;
protected ?array $integers = null;

/**
* @param string $binaryValue
Expand All @@ -35,7 +35,7 @@ private function __construct(string $binaryValue)
*
* @return self
*/
public static function fromBits($bits): self
public static function fromBits(array|string $bits): self
{
if (\is_array($bits)) {
$bits = implode('', $bits);
Expand Down
26 changes: 11 additions & 15 deletions src/ImageHash.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
<?php namespace Jenssegers\ImageHash;

use Intervention\Image\Drivers\Imagick\Driver as ImagickDriver;
use Intervention\Image\Drivers\Gd\Driver as GdDriver;
use Intervention\Image\Image;
use Intervention\Image\ImageManager;
use Jenssegers\ImageHash\Implementations\DifferenceHash;
use RuntimeException;

class ImageHash
{
/**
* @var Implementation
*/
protected $implementation;
protected Implementation $implementation;

/**
* @var Image
*/
private $driver;
private ImageManager $driver;

public function __construct(
Implementation $implementation = null,
Expand All @@ -30,9 +26,9 @@ public function __construct(
* @param mixed $image
* @return Hash
*/
public function hash($image): Hash
public function hash(mixed $image): Hash
{
$image = $this->driver->make($image);
$image = $this->driver->read($image);

return $this->implementation->hash($image);
}
Expand All @@ -43,7 +39,7 @@ public function hash($image): Hash
* @param mixed $resource2
* @return int
*/
public function compare($resource1, $resource2): int
public function compare(mixed $resource1, mixed $resource2): int
{
$hash1 = $this->hash($resource1);
$hash2 = $this->hash($resource2);
Expand All @@ -58,7 +54,7 @@ public function distance(Hash $hash1, Hash $hash2): int

protected function createResource(string $data): Image
{
return $this->driver->make($data);
return $this->driver->read($data);
}

protected function defaultImplementation(): Implementation
Expand All @@ -69,11 +65,11 @@ protected function defaultImplementation(): Implementation
protected function defaultDriver(): ImageManager
{
if (extension_loaded('imagick')) {
return new ImageManager(['driver' => 'imagick']);
return new ImageManager(new ImagickDriver());
}

if (extension_loaded('gd')) {
return new ImageManager(['driver' => 'gd']);
return new ImageManager(new GdDriver());
}

throw new RuntimeException('Please install GD or ImageMagick');
Expand Down
7 changes: 2 additions & 5 deletions src/Implementations/AverageHash.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@

class AverageHash implements Implementation
{
/**
* @var int
*/
protected $size;
protected int $size;

public function __construct(int $size = 8)
{
Expand All @@ -25,7 +22,7 @@ public function hash(Image $image): Hash
$pixels = [];
for ($y = 0; $y < $this->size; $y++) {
for ($x = 0; $x < $this->size; $x++) {
$rgb = $resized->pickColor($x, $y);
$rgb = $resized->pickColor($x, $y)->toArray();
$pixels[] = (int) floor(($rgb[0] * 0.299) + ($rgb[1] * 0.587) + ($rgb[2] * 0.114));
}
}
Expand Down
26 changes: 10 additions & 16 deletions src/Implementations/BlockHash.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,9 @@ class BlockHash implements Implementation
*/
const QUICK = 'quick';

/**
* @var string
*/
protected $mode;
protected string $mode;

/**
* @var int
*/
protected $size;
protected int $size;

public function __construct(int $size = 16, $mode = self::PRECISE)
{
Expand All @@ -54,8 +48,8 @@ public function hash(Image $image): Hash

private function even(Image $image): Hash
{
$width = $image->getWidth();
$height = $image->getHeight();
$width = $image->width();
$height = $image->height();
$blocksizeX = (int) floor($width / $this->size);
$blocksizeY = (int) floor($height / $this->size);

Expand All @@ -69,7 +63,7 @@ private function even(Image $image): Hash
for ($ix = 0; $ix < $blocksizeX; $ix++) {
$cx = $x * $blocksizeX + $ix;
$cy = $y * $blocksizeY + $iy;
$rgb = $image->pickColor($cx, $cy);
$rgb = $image->pickColor($cx, $cy)->toArray();
$value += $rgb[0] + $rgb[1] + $rgb[2];
}
}
Expand All @@ -83,8 +77,8 @@ private function even(Image $image): Hash

private function uneven(Image $image): Hash
{
$imageWidth = $image->getWidth();
$imageHeight = $image->getHeight();
$imageWidth = $image->width();
$imageHeight = $image->height();
$evenX = $imageWidth % $this->size === 0;
$evenY = $imageHeight % $this->size === 0;
$blockWidth = $imageWidth / $this->size;
Expand All @@ -103,7 +97,7 @@ private function uneven(Image $image): Hash
$weightTop = 1;
$weightBottom = 0;
} else {
$yMod = ($y + 1) % $blockHeight;
$yMod = fmod($y + 1, $blockHeight);
$yFrac = $yMod - (int) floor($yMod);
$yInt = $yMod - $yFrac;

Expand All @@ -120,15 +114,15 @@ private function uneven(Image $image): Hash
}

for ($x = 0; $x < $imageWidth; $x++) {
$rgb = $image->pickColor($x, $y);
$rgb = $image->pickColor($x, $y)->toArray();
$value = $rgb[0] + $rgb[1] + $rgb[2];

if ($evenX) {
$blockLeft = $blockRight = (int) floor($x / $blockWidth);
$weightLeft = 1;
$weightRight = 0;
} else {
$xMod = ($x + 1) % $blockWidth;
$xMod = fmod($x + 1, $blockWidth);
$xFrac = $xMod - (int) floor($xMod);
$xInt = $xMod - $xFrac;

Expand Down
9 changes: 3 additions & 6 deletions src/Implementations/DifferenceHash.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@

class DifferenceHash implements Implementation
{
/**
* @var int
*/
protected $size;
protected int $size;

public function __construct(int $size = 8)
{
Expand All @@ -28,12 +25,12 @@ public function hash(Image $image): Hash
$bits = [];
for ($y = 0; $y < $height; $y++) {
// Get the pixel value for the leftmost pixel.
$rgb = $resized->pickColor(0, $y);
$rgb = $resized->pickColor(0, $y)->toArray();
$left = (int) floor(($rgb[0] * 0.299) + ($rgb[1] * 0.587) + ($rgb[2] * 0.114));

for ($x = 1; $x < $width; $x++) {
// Get the pixel value for each pixel starting from position 1.
$rgb = $resized->pickColor($x, $y);
$rgb = $resized->pickColor($x, $y)->toArray();
$right = (int) floor(($rgb[0] * 0.299) + ($rgb[1] * 0.587) + ($rgb[2] * 0.114));

// Each hash bit is set based on whether the left pixel is brighter than the right pixel.
Expand Down
Loading

0 comments on commit 643d8d6

Please sign in to comment.