-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
250 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
vendor/ | ||
tests/fixtures/to/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
language: php | ||
|
||
php: | ||
- 7.1 | ||
- 7.2 | ||
- 7.3 | ||
|
||
before_script: | ||
- composer install | ||
|
||
script: | ||
- mkdir -p build/logs | ||
- ./vendor/bin/phpunit --coverage-clover build/logs/clover.xml | ||
|
||
after_success: | ||
- travis_retry php vendor/bin/php-coveralls -v |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "jens/php-image-converter", | ||
"description": "PHP class to convert image formats", | ||
"keywords": ["php", "image", "conversion"], | ||
"type": "library", | ||
"homepage": "https://github.com/jenstornell/php-image-converter", | ||
"license": "MIT", | ||
"authors": [{ | ||
"name": "Jens Törnell", | ||
"homepage": "http://www.jenst.se/" | ||
}], | ||
"require": { | ||
"php": "^7.1", | ||
"ext-gd": "*", | ||
"ext-exif": "*" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Jens\\ImageConverter\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Jens\\ImageConverter\\Tests\\": "test/" | ||
} | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^7.0", | ||
"php-coveralls/php-coveralls": "^2.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
if (file_exists(__DIR__ . '/vendor/autoload.php')) { | ||
require_once __DIR__ . '/vendor/autoload.php'; | ||
} else { | ||
require_once __DIR__ . '/src/ImageConverter.php'; | ||
} | ||
|
||
use Jens\ImageConverter\ImageConverter; | ||
|
||
$from = __DIR__ . '/tests/fixtures/from/dices.png'; | ||
$to = __DIR__ . '/tests/fixtures/to/dices.webp'; | ||
|
||
$converter = new ImageConverter(); | ||
|
||
echo $converter->convert($from, $to, 5); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit bootstrap="vendor/autoload.php" colors="true" backupGlobals="false" | ||
backupStaticAttributes="false"> | ||
<testsuites> | ||
<testsuite name="Tests"> | ||
<directory suffix="Test.php">tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist processUncoveredFilesFromWhitelist="false"> | ||
<directory suffix=".php">src</directory> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
|
||
namespace Jens\ImageConverter; | ||
|
||
class ImageConverter | ||
{ | ||
/** @var array */ | ||
private $imageFormat = [ | ||
'gif', | ||
'jpeg', | ||
'jpg', | ||
'png', | ||
'webp', | ||
]; | ||
|
||
/** @var array */ | ||
private $constImageFormat = [ | ||
IMAGETYPE_GIF => 'gif', | ||
IMAGETYPE_JPEG => 'jpeg', | ||
IMAGETYPE_PNG => 'png', | ||
IMAGETYPE_WEBP => 'webp', | ||
]; | ||
|
||
/** | ||
* Do image conversion work | ||
* | ||
* @param string $from | ||
* @param string $to | ||
* | ||
* @return resource | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function convert($from, $to, $quality = -1) | ||
{ | ||
$image = $this->loadImage($from); | ||
if (!$image) { | ||
throw new \InvalidArgumentException(sprintf('Cannot load image from %s', $from)); | ||
} | ||
|
||
return $this->saveImage($to, $image, $quality); | ||
} | ||
|
||
private function loadImage($from) | ||
{ | ||
$extension = $this->getRealExtension($from); | ||
|
||
if (!array_key_exists($extension, $this->constImageFormat)) { | ||
throw new \InvalidArgumentException(sprintf('The %s extension is unsupported', $extension)); | ||
} | ||
|
||
$method = 'imagecreatefrom' . $this->constImageFormat[$extension]; | ||
|
||
return $method($from); | ||
} | ||
|
||
private function saveImage($to, $image, $quality) | ||
{ | ||
$extension = $this->getExtension($to); | ||
|
||
if ($extension === 'jpg') { | ||
$extension = 'jpeg'; | ||
} | ||
|
||
if (!in_array($extension, $this->imageFormat)) { | ||
throw new \InvalidArgumentException(sprintf('The %s extension is unsupported', $extension)); | ||
} | ||
if (!file_exists(dirname($to))) { | ||
$this->makeDirectory($to); | ||
} | ||
|
||
$method = 'image' . $extension; | ||
|
||
return $method($image, $to, $quality); | ||
} | ||
|
||
/** | ||
* Given specific $path to detect current image extension | ||
*/ | ||
private function getRealExtension($path) | ||
{ | ||
$extension = exif_imagetype($path); | ||
|
||
if (!array_key_exists($extension, $this->constImageFormat)) { | ||
throw new \InvalidArgumentException(sprintf('Cannot detect %s extension', $path)); | ||
} | ||
|
||
return $extension; | ||
} | ||
|
||
/** | ||
* Get image extension from specific $path | ||
* | ||
* @param string $path | ||
* | ||
* @return string | ||
*/ | ||
private function getExtension($path) | ||
{ | ||
$pathInfo = pathinfo($path); | ||
|
||
if (!array_key_exists('extension', $pathInfo)) { | ||
throw new \InvalidArgumentException(sprintf('Cannot find extension from %s', $path)); | ||
} | ||
|
||
return $pathInfo['extension']; | ||
} | ||
|
||
/** | ||
* Try creating the directory | ||
* | ||
* @return bool | ||
* @throws \InvalidArgumentException | ||
*/ | ||
private function makeDirectory($to) | ||
{ | ||
$result = @mkdir(dirname($to), 0755); | ||
|
||
if (!$result) { | ||
throw new \InvalidArgumentException(\sprintf('Cannot create %s directory', $to)); | ||
} | ||
|
||
return $result; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Jens\ImageConverter\Tests; | ||
|
||
use Jens\ImageConverter\ImageConverter; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ImageConverterTest extends TestCase | ||
{ | ||
public function pngToAvailablImagesProvider() | ||
{ | ||
return [ | ||
[__DIR__ . '/fixtures/to/dices.gif'], | ||
[__DIR__ . '/fixtures/to/dices.jpg'], | ||
]; | ||
} | ||
|
||
public function jpgToAvailablImagesProvider() | ||
{ | ||
return [ | ||
[__DIR__ . '/fixtures/to/dices.gif'], | ||
[__DIR__ . '/fixtures/to/dices.png'], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider pngToAvailablImagesProvider | ||
*/ | ||
public function testPngToAvailablImages($to) | ||
{ | ||
$from = __DIR__ . '/fixtures/from/dices.png'; | ||
$converter = new ImageConverter(); | ||
|
||
$this->assertTrue($converter->convert($from, $to, 5)); | ||
} | ||
|
||
/** | ||
* @dataProvider jpgToAvailablImagesProvider | ||
*/ | ||
public function testJpgToAvailablImages($to) | ||
{ | ||
$from = __DIR__ . '/fixtures/from/road.jpg'; | ||
$converter = new ImageConverter(); | ||
|
||
$this->assertTrue($converter->convert($from, $to, 5)); | ||
} | ||
} |
File renamed without changes
File renamed without changes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.