diff --git a/.docker/php71/Dockerfile b/.docker/php71/Dockerfile deleted file mode 100644 index a112d7e..0000000 --- a/.docker/php71/Dockerfile +++ /dev/null @@ -1,34 +0,0 @@ -FROM php:7.1-cli - -ENV DEBIAN_FRONTEND noninteractive -ENV COMPOSER_ALLOW_SUPERUSER 1 - -RUN apt-get update && \ - apt-get install -y --no-install-recommends --allow-unauthenticated --no-upgrade \ - git && \ - apt-get clean && \ - rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/ - -RUN apt-get update && apt-get install -y \ - libwebp-dev \ - libjpeg62-turbo-dev \ - libpng-dev \ - libxpm-dev \ - libfreetype6-dev - -RUN docker-php-ext-configure gd --with-gd --with-webp-dir --with-jpeg-dir \ - --with-png-dir --with-zlib-dir --with-xpm-dir --with-freetype-dir \ - --enable-gd-native-ttf - -RUN docker-php-ext-install gd -RUN docker-php-ext-install exif - -RUN pecl install xdebug-2.8.1 \ - && docker-php-ext-enable xdebug - -RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" && \ - php composer-setup.php && \ - php -r "unlink('composer-setup.php');" && \ - mv composer.phar /usr/local/bin/ && \ - ln -s /usr/local/bin/composer.phar /usr/local/bin/composer && \ - composer global require hirak/prestissimo diff --git a/.docker/php74/Dockerfile b/.docker/php74/Dockerfile new file mode 100644 index 0000000..4ca493f --- /dev/null +++ b/.docker/php74/Dockerfile @@ -0,0 +1,15 @@ +FROM php:7.4-cli + +ENV DEBIAN_FRONTEND noninteractive + +RUN apt-get update && \ + apt-get install -y --no-install-recommends --allow-unauthenticated --no-upgrade \ + git && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/ + +COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/bin/ + +RUN install-php-extensions gd exif xdebug + +COPY --from=composer /usr/bin/composer /usr/bin/composer diff --git a/.travis.yml b/.travis.yml index 8cce2b8..a79a2d9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,9 +2,6 @@ language: php sudo: false php: - - 7.1 - - 7.2 - - 7.3 - 7.4 cache: diff --git a/CHANGELOG.md b/CHANGELOG.md index 0633d0b..b43723b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +## v3.0.0 +⚠ BREAKING CHANGES +- Refactoring library to minor php version `7.4` + ## v2.0.0 ⚠ BREAKING CHANGES - Refactoring library to minor php version `7.1` diff --git a/LICENSE b/LICENSE index 927ab98..3189bfa 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2014 Andrea Giannantonio +Copyright (c) 2014-2020 Andrea Giannantonio Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/README.md b/README.md index d4e38e2..b128796 100644 --- a/README.md +++ b/README.md @@ -12,9 +12,7 @@ PHP library to fix image orientation by exif data with thanks to method [exif_re ## Image Example -| Input | Output | -|--- |--- | -| ![after](images/input_landscape_3.jpg) | ![output](images/output_landscape_3.jpg) | +![after](images/after_and_before.png) ## How to install @@ -46,20 +44,20 @@ Dependencies are managed through composer: ``` $ docker-compose up --build -d -$ docker-compose run php71 composer install +$ docker-compose run php74 composer install ``` ### Run phpunit: ``` -$ docker-compose run php71 composer test +$ docker-compose run php74 composer test ``` ### Run php-cs-fixer ``` bash -docker-compose run php71 composer cs-fixer +docker-compose run php74 composer cs-fixer ``` diff --git a/composer.json b/composer.json index 2f42bf1..cacf135 100644 --- a/composer.json +++ b/composer.json @@ -31,12 +31,12 @@ } }, "require": { - "php": "^7.1", + "php": "^7.4", "ext-gd": "*", "ext-exif": "*" }, "require-dev": { - "phpunit/phpunit": "^7.5", + "phpunit/phpunit": "^9.4", "friendsofphp/php-cs-fixer": "^2.16" }, "scripts": { diff --git a/docker-compose.yml b/docker-compose.yml index 602427a..befdf1b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,8 +1,8 @@ version: '3.1' services: - php71: - build: .docker/php71 + php74: + build: .docker/php74 working_dir: /application volumes: - .:/application:cached diff --git a/images/after_and_before.png b/images/after_and_before.png new file mode 100644 index 0000000..dbb06a8 Binary files /dev/null and b/images/after_and_before.png differ diff --git a/images/input_landscape_3.jpg b/images/input_landscape_3.jpg deleted file mode 100755 index 74505cf..0000000 Binary files a/images/input_landscape_3.jpg and /dev/null differ diff --git a/images/output_landscape_3.jpg b/images/output_landscape_3.jpg deleted file mode 100644 index a8d10ce..0000000 Binary files a/images/output_landscape_3.jpg and /dev/null differ diff --git a/src/Image.php b/src/Image.php index fe4328e..a21fd90 100644 --- a/src/Image.php +++ b/src/Image.php @@ -8,7 +8,7 @@ namespace ImageOrientationFix; -use Exception; +use RuntimeException; /** * Class Image @@ -16,13 +16,12 @@ */ class Image { - /** @var string */ - private $filePathInput = null; - private $mimeType = null; - private $exifData = null; - private $orientation = null; - private $extension = null; - private static $mimeTypesValid = [ + private ?string $filePathInput = null; + private ?string $mimeType = null; + private $exifData = null; + private ?int $orientation = null; + private ?string $extension = null; + private static array $mimeTypesValid = [ 'jpe' => 'image/jpe', 'jpeg' => 'image/jpeg', 'jpg' => 'image/jpg', @@ -41,12 +40,12 @@ public function __construct($filePathInput) /** * @param $filePathInput - * @throws Exception + * @throws RuntimeException */ private function setFilePathInput($filePathInput): void { if (!file_exists($filePathInput)) { - throw new Exception('FilePathInput not exists'); + throw new RuntimeException('FilePathInput not exists'); } $this->filePathInput = $filePathInput; } @@ -65,8 +64,8 @@ public function getFilePathInput(): string private function setMimeType(): void { $mimeType = MimeType::get($this->getFilePathInput()); - if (empty($mimeType) || !in_array($mimeType, self::$mimeTypesValid)) { - throw new Exception("$mimeType: mimeType not valid"); + if (empty($mimeType) || !in_array($mimeType, self::$mimeTypesValid, true)) { + throw new RuntimeException("$mimeType: mimeType not valid"); } $this->mimeType = $mimeType; } @@ -104,17 +103,15 @@ public function getExifData() private function setOrientation(): void { $exifData = $this->getExifData(); - if ($exifData && is_array($exifData) && array_key_exists('Orientation', $exifData)) { - if (1 <= $exifData['Orientation'] && $exifData['Orientation'] <= 8) { - $this->orientation = ($exifData && isset($exifData['Orientation'])) ? $exifData['Orientation'] : false; - } + if ($exifData && is_array($exifData) && array_key_exists('Orientation', $exifData) && 1 <= $exifData['Orientation'] && $exifData['Orientation'] <= 8) { + $this->orientation = $exifData['Orientation']; } } /** - * @return int + * @return int|null */ - public function getOrientation(): int + public function getOrientation(): ?int { return $this->orientation; } @@ -131,9 +128,9 @@ private function setExtension(): void } /** - * @return string + * @return string|null */ - public function getExtension(): string + public function getExtension(): ?string { return $this->extension; } diff --git a/src/ImageOrientationFixer.php b/src/ImageOrientationFixer.php index 936c145..1d1290f 100755 --- a/src/ImageOrientationFixer.php +++ b/src/ImageOrientationFixer.php @@ -8,7 +8,7 @@ namespace ImageOrientationFix; -use Exception; +use RuntimeException; /** * Class ImageOrientationFixer @@ -16,17 +16,17 @@ */ class ImageOrientationFixer { - private $image; - private $filePathOutput; - private $resourceImage; - private $resourceImageFixed; + private Image $image; + private ?string $filePathOutput = null; + private $resourceImage = null; + private $resourceImageFixed = null; - public function __construct($filePathInput, $filePathOutput = false) + public function __construct($filePathInput, $filePathOutput = null) { try { $this->image = new Image($filePathInput); $this->setFilePathOutput($filePathOutput); - } catch (Exception $e) { + } catch (RuntimeException $e) { throw $e; } } @@ -34,7 +34,7 @@ public function __construct($filePathInput, $filePathOutput = false) /** * Function manager to fix orientation image * @return bool - * @throws Exception + * @throws RuntimeException */ public function fix(): bool { @@ -59,18 +59,18 @@ public function fix(): bool $this->setResourceImage(); // If it failed to load a resource, give up if (null === $this->getResourceImage()) { - throw new Exception('Unable load resource image'); + throw new RuntimeException('Unable load resource image'); } // Set the GD image resource fixed $this->setResourceImageFixed(); if (null === $this->getResourceImageFixed()) { - throw new Exception('Unable fix image'); + throw new RuntimeException('Unable fix image'); } // Save the image fixed return $this->saveFix(); - } catch (Exception $e) { + } catch (RuntimeException $e) { throw $e; } } @@ -78,9 +78,8 @@ public function fix(): bool /** * Set the GD image resource for loaded image */ - private function setResourceImage() + private function setResourceImage(): void { - $this->resourceImage = null; switch ($this->image->getExtension()) { case 'png': $this->resourceImage = \imagecreatefrompng($this->image->getFilePathInput()); @@ -96,7 +95,7 @@ private function setResourceImage() } /** - * @return mixed + * @return resource|false|null an image resource identifier on success, false on errors or null if extension id not recognized */ public function getResourceImage() { @@ -106,9 +105,8 @@ public function getResourceImage() /** * Set the resource image fixed */ - private function setResourceImageFixed() + private function setResourceImageFixed(): void { - $this->resourceImageFixed = null; switch ($this->image->getOrientation()) { // horizontal flip case 2: @@ -150,7 +148,7 @@ private function setResourceImageFixed() } /** - * @return mixed + * @return resource|false|null an image resource identifier on success, false on errors or null if extension id not recognized */ public function getResourceImageFixed() { @@ -158,21 +156,21 @@ public function getResourceImageFixed() } /** - * @param $resourceImage + * @param resource|false|null $resourceImage * @param int $mode - possible parameters: IMG_FLIP_HORIZONTAL || IMG_FLIP_VERTICAL || IMG_FLIP_BOTH * @return resource - * @throws Exception + * @throws RuntimeException */ - private function executeImageFlip($resourceImage, $mode) + private function executeImageFlip($resourceImage, int $mode) { if (function_exists('imageflip')) { //only php >= 5.5 imageflip($resourceImage, $mode); } else { - if ($mode == IMG_FLIP_VERTICAL || $mode == IMG_FLIP_BOTH) { + if ($mode === IMG_FLIP_VERTICAL || $mode === IMG_FLIP_BOTH) { $resourceImage = $this->flipVertical($resourceImage); } - if ($mode == IMG_FLIP_HORIZONTAL || $mode == IMG_FLIP_BOTH) { + if ($mode === IMG_FLIP_HORIZONTAL || $mode === IMG_FLIP_BOTH) { $resourceImage = $this->flipHorizontal($resourceImage); } } @@ -182,9 +180,9 @@ private function executeImageFlip($resourceImage, $mode) /** * Flip vertical - * @param $resourceImage + * @param resource|false|null $resourceImage * @return resource - * @throws Exception + * @throws RuntimeException */ private function flipVertical($resourceImage) { @@ -193,7 +191,7 @@ private function flipVertical($resourceImage) $temp = imagecreatetruecolor($size_x, $size_y); $x = imagecopyresampled($temp, $resourceImage, 0, 0, 0, ($size_y - 1), $size_x, $size_y, $size_x, 0 - $size_y); if (!$x) { - throw new Exception('Unable to flip vertical image'); + throw new RuntimeException('Unable to flip vertical image'); } return $temp; @@ -201,9 +199,9 @@ private function flipVertical($resourceImage) /** * Flip horizontal - * @param $resourceImage + * @param resource|false|null $resourceImage * @return resource - * @throws Exception + * @throws RuntimeException */ private function flipHorizontal($resourceImage) { @@ -212,7 +210,7 @@ private function flipHorizontal($resourceImage) $temp = imagecreatetruecolor($size_x, $size_y); $x = imagecopyresampled($temp, $resourceImage, 0, 0, ($size_x - 1), 0, $size_x, $size_y, 0 - $size_x, $size_y); if (!$x) { - throw new Exception('Unable to flip horizontal image'); + throw new RuntimeException('Unable to flip horizontal image'); } return $temp; @@ -245,18 +243,17 @@ private function saveFix(): bool } /** - * @param $filePathOutput - * @throws Exception + * @param string|null $filePathOutput */ - public function setFilePathOutput($filePathOutput = false): void + public function setFilePathOutput(?string $filePathOutput): void { $this->filePathOutput = $filePathOutput; } /** - * @return mixed + * @return string|null */ - public function getFilePathOutput() + public function getFilePathOutput(): ?string { return $this->filePathOutput; } diff --git a/src/MimeType.php b/src/MimeType.php index 4bf7a96..170a833 100644 --- a/src/MimeType.php +++ b/src/MimeType.php @@ -17,7 +17,7 @@ class MimeType /** * @var array */ - public static $mimeTypes = [ + public static array $mimeTypes = [ 'txt' => 'text/plain', 'htm' => 'text/html', 'html' => 'text/html', @@ -77,14 +77,16 @@ public static function get($filename): ?string if (array_key_exists($extension, self::$mimeTypes)) { return self::$mimeTypes[$extension]; - } elseif (function_exists('finfo_open') && is_file($filename)) { + } + + if (function_exists('finfo_open') && is_file($filename)) { $finfo = finfo_open(FILEINFO_MIME); $mimetype = finfo_file($finfo, $filename); finfo_close($finfo); return $mimetype; - } else { - return 'application/octet-stream'; } + + return 'application/octet-stream'; } } diff --git a/tests/ImageOrientationFixerTest.php b/tests/ImageOrientationFixerTest.php index 38449da..90b5aea 100755 --- a/tests/ImageOrientationFixerTest.php +++ b/tests/ImageOrientationFixerTest.php @@ -12,14 +12,6 @@ */ class ImageOrientationFixerTest extends TestCase { - public function setUp() - { - } - - public function tearDown() - { - } - public function dataProviderImages(): array { return [ @@ -61,13 +53,13 @@ public function testFix(string $filename, int $orientation): void } catch (Exception $e) { } - $this->assertFileExists($inputFullFilePathTmp); + self::assertFileExists($inputFullFilePathTmp); $exifData = exif_read_data($inputFullFilePathTmp, 'IFD0', 0); if (1 === $orientation) { - $this->assertEquals($orientation, $exifData['Orientation']); + self::assertEquals($orientation, $exifData['Orientation']); } else { - $this->assertFalse($exifData); + self::assertFalse($exifData); } @unlink($inputFullFilePathTmp); @@ -75,8 +67,10 @@ public function testFix(string $filename, int $orientation): void /** * @dataProvider dataProviderImages + * @param string $filename + * @param int $orientation */ - public function testFixWithOutput($filename, $orientation): void + public function testFixWithOutput(string $filename, int $orientation): void { $inputFullFilePath = $this->getInputImagesPath() . $filename; $outputFullFilePath = $this->getOutputImagesPath() . $filename; @@ -88,11 +82,11 @@ public function testFixWithOutput($filename, $orientation): void } if (1 === $orientation) { - $this->markTestSkipped('no action'); + self::markTestSkipped('no action'); } else { - $this->assertFileExists($outputFullFilePath); + self::assertFileExists($outputFullFilePath); $exifData = exif_read_data($outputFullFilePath, 'IFD0', 0); - $this->assertFalse($exifData); + self::assertFalse($exifData); } @unlink($outputFullFilePath); diff --git a/tests/ImageTest.php b/tests/ImageTest.php index cef4ecc..ac3f7df 100644 --- a/tests/ImageTest.php +++ b/tests/ImageTest.php @@ -5,6 +5,7 @@ use Exception; use ImageOrientationFix\Image; use PHPUnit\Framework\TestCase; +use RuntimeException; /** * Class ImageTest @@ -12,39 +13,35 @@ */ class ImageTest extends TestCase { - public $fileNameImageLandscape = 'Landscape_1.jpg'; + public string $fileNameImageLandscape = 'Landscape_1.jpg'; public function testConstructImageClass(): void { $image = new Image($this->getInputImagesPath() . $this->fileNameImageLandscape); - $this->assertInstanceOf(Image::class, $image); + self::assertInstanceOf(Image::class, $image); } public function testGetFilePathInput(): void { $image = new Image($this->getInputImagesPath() . $this->fileNameImageLandscape); - $this->assertTrue(file_exists($image->getFilePathInput())); + self::assertFileExists($image->getFilePathInput()); } - /** - * @expectedException Exception - */ public function testFileExistException(): void { + $this->expectException(RuntimeException::class); $image = new Image($this->getInputImagesPath() . 'ThisFileNotExist.jpg'); } public function testSetMimeType(): void { $image = new Image($this->getInputImagesPath() . $this->fileNameImageLandscape); - $this->assertEquals('image/jpeg', $image->getMimeType()); + self::assertEquals('image/jpeg', $image->getMimeType()); } - /** - * @expectedException Exception - */ public function testMimeTypeNotValidException(): void { + $this->expectException(RuntimeException::class); $image = new Image($this->getInputImagesPath() . 'mimeTypeNotValid.gif'); } @@ -52,39 +49,39 @@ public function testSetExifData(): void { $image = new Image($this->getInputImagesPath() . $this->fileNameImageLandscape); $exifData = exif_read_data($this->getInputImagesPath() . $this->fileNameImageLandscape, 'IFD0', 0); - $this->assertEquals($exifData, $image->getExifData()); + self::assertEquals($exifData, $image->getExifData()); } public function testGetExifData(): void { $image = new Image($this->getInputImagesPath() . $this->fileNameImageLandscape); $exifData = $image->getExifData(); - $this->assertTrue(is_array($exifData)); - $this->assertNotEmpty($exifData); + self::assertIsArray($exifData); + self::assertNotEmpty($exifData); } public function testGetOrientation(): void { $image = new Image($this->getInputImagesPath() . $this->fileNameImageLandscape); $orientation = $image->getOrientation(); - $this->assertNotEmpty($orientation); - $this->assertEquals(1, $orientation); + self::assertNotEmpty($orientation); + self::assertEquals(1, $orientation); } public function testGetOrientation3(): void { $image = new Image($this->getInputImagesPath() . 'Landscape_3.jpg'); $orientation = $image->getOrientation(); - $this->assertNotEmpty($orientation); - $this->assertEquals(3, $orientation); + self::assertNotEmpty($orientation); + self::assertEquals(3, $orientation); } public function testGetExtension(): void { $image = new Image($this->getInputImagesPath() . $this->fileNameImageLandscape); $extension = $image->getExtension(); - $this->assertNotEmpty($extension); - $this->assertEquals('jpeg', $extension); + self::assertNotEmpty($extension); + self::assertEquals('jpeg', $extension); } /**