Skip to content

Commit f34c2a5

Browse files
committed
Huge changes
1 parent 31a1dfb commit f34c2a5

17 files changed

+250
-53
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor/
2+
tests/fixtures/to/*

.travis.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
language: php
2+
3+
php:
4+
- 7.1
5+
- 7.2
6+
- 7.3
7+
8+
before_script:
9+
- composer install
10+
11+
script:
12+
- mkdir -p build/logs
13+
- ./vendor/bin/phpunit --coverage-clover build/logs/clover.xml
14+
15+
after_success:
16+
- travis_retry php vendor/bin/php-coveralls -v

composer.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "jens/php-image-converter",
3+
"description": "PHP class to convert image formats",
4+
"keywords": ["php", "image", "conversion"],
5+
"type": "library",
6+
"homepage": "https://github.com/jenstornell/php-image-converter",
7+
"license": "MIT",
8+
"authors": [{
9+
"name": "Jens Törnell",
10+
"homepage": "http://www.jenst.se/"
11+
}],
12+
"require": {
13+
"php": "^7.1",
14+
"ext-gd": "*",
15+
"ext-exif": "*"
16+
},
17+
"autoload": {
18+
"psr-4": {
19+
"Jens\\ImageConverter\\": "src/"
20+
}
21+
},
22+
"autoload-dev": {
23+
"psr-4": {
24+
"Jens\\ImageConverter\\Tests\\": "test/"
25+
}
26+
},
27+
"require-dev": {
28+
"phpunit/phpunit": "^7.0",
29+
"php-coveralls/php-coveralls": "^2.1"
30+
}
31+
}

example.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
if (file_exists(__DIR__ . '/vendor/autoload.php')) {
4+
require_once __DIR__ . '/vendor/autoload.php';
5+
} else {
6+
require_once __DIR__ . '/src/ImageConverter.php';
7+
}
8+
9+
use Jens\ImageConverter\ImageConverter;
10+
11+
$from = __DIR__ . '/tests/fixtures/from/dices.png';
12+
$to = __DIR__ . '/tests/fixtures/to/dices.webp';
13+
14+
$converter = new ImageConverter();
15+
16+
echo $converter->convert($from, $to, 5);

index.php

Lines changed: 0 additions & 7 deletions
This file was deleted.

phpunit.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php" colors="true" backupGlobals="false"
3+
backupStaticAttributes="false">
4+
<testsuites>
5+
<testsuite name="Tests">
6+
<directory suffix="Test.php">tests</directory>
7+
</testsuite>
8+
</testsuites>
9+
<filter>
10+
<whitelist processUncoveredFilesFromWhitelist="false">
11+
<directory suffix=".php">src</directory>
12+
</whitelist>
13+
</filter>
14+
</phpunit>

src/ImageConverter.php

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
namespace Jens\ImageConverter;
4+
5+
class ImageConverter
6+
{
7+
/** @var array */
8+
private $imageFormat = [
9+
'gif',
10+
'jpeg',
11+
'jpg',
12+
'png',
13+
'webp',
14+
];
15+
16+
/** @var array */
17+
private $constImageFormat = [
18+
IMAGETYPE_GIF => 'gif',
19+
IMAGETYPE_JPEG => 'jpeg',
20+
IMAGETYPE_PNG => 'png',
21+
IMAGETYPE_WEBP => 'webp',
22+
];
23+
24+
/**
25+
* Do image conversion work
26+
*
27+
* @param string $from
28+
* @param string $to
29+
*
30+
* @return resource
31+
* @throws \InvalidArgumentException
32+
*/
33+
public function convert($from, $to, $quality = -1)
34+
{
35+
$image = $this->loadImage($from);
36+
if (!$image) {
37+
throw new \InvalidArgumentException(sprintf('Cannot load image from %s', $from));
38+
}
39+
40+
return $this->saveImage($to, $image, $quality);
41+
}
42+
43+
private function loadImage($from)
44+
{
45+
$extension = $this->getRealExtension($from);
46+
47+
if (!array_key_exists($extension, $this->constImageFormat)) {
48+
throw new \InvalidArgumentException(sprintf('The %s extension is unsupported', $extension));
49+
}
50+
51+
$method = 'imagecreatefrom' . $this->constImageFormat[$extension];
52+
53+
return $method($from);
54+
}
55+
56+
private function saveImage($to, $image, $quality)
57+
{
58+
$extension = $this->getExtension($to);
59+
60+
if ($extension === 'jpg') {
61+
$extension = 'jpeg';
62+
}
63+
64+
if (!in_array($extension, $this->imageFormat)) {
65+
throw new \InvalidArgumentException(sprintf('The %s extension is unsupported', $extension));
66+
}
67+
if (!file_exists(dirname($to))) {
68+
$this->makeDirectory($to);
69+
}
70+
71+
$method = 'image' . $extension;
72+
73+
return $method($image, $to, $quality);
74+
}
75+
76+
/**
77+
* Given specific $path to detect current image extension
78+
*/
79+
private function getRealExtension($path)
80+
{
81+
$extension = exif_imagetype($path);
82+
83+
if (!array_key_exists($extension, $this->constImageFormat)) {
84+
throw new \InvalidArgumentException(sprintf('Cannot detect %s extension', $path));
85+
}
86+
87+
return $extension;
88+
}
89+
90+
/**
91+
* Get image extension from specific $path
92+
*
93+
* @param string $path
94+
*
95+
* @return string
96+
*/
97+
private function getExtension($path)
98+
{
99+
$pathInfo = pathinfo($path);
100+
101+
if (!array_key_exists('extension', $pathInfo)) {
102+
throw new \InvalidArgumentException(sprintf('Cannot find extension from %s', $path));
103+
}
104+
105+
return $pathInfo['extension'];
106+
}
107+
108+
/**
109+
* Try creating the directory
110+
*
111+
* @return bool
112+
* @throws \InvalidArgumentException
113+
*/
114+
private function makeDirectory($to)
115+
{
116+
$result = @mkdir(dirname($to), 0755);
117+
118+
if (!$result) {
119+
throw new \InvalidArgumentException(\sprintf('Cannot create %s directory', $to));
120+
}
121+
122+
return $result;
123+
}
124+
}

src/php-image-converter.php

Lines changed: 0 additions & 46 deletions
This file was deleted.

tests/ImageConverterTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Jens\ImageConverter\Tests;
4+
5+
use Jens\ImageConverter\ImageConverter;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class ImageConverterTest extends TestCase
9+
{
10+
public function pngToAvailablImagesProvider()
11+
{
12+
return [
13+
[__DIR__ . '/fixtures/to/dices.gif'],
14+
[__DIR__ . '/fixtures/to/dices.jpg'],
15+
];
16+
}
17+
18+
public function jpgToAvailablImagesProvider()
19+
{
20+
return [
21+
[__DIR__ . '/fixtures/to/dices.gif'],
22+
[__DIR__ . '/fixtures/to/dices.png'],
23+
];
24+
}
25+
26+
/**
27+
* @dataProvider pngToAvailablImagesProvider
28+
*/
29+
public function testPngToAvailablImages($to)
30+
{
31+
$from = __DIR__ . '/fixtures/from/dices.png';
32+
$converter = new ImageConverter();
33+
34+
$this->assertTrue($converter->convert($from, $to, 5));
35+
}
36+
37+
/**
38+
* @dataProvider jpgToAvailablImagesProvider
39+
*/
40+
public function testJpgToAvailablImages($to)
41+
{
42+
$from = __DIR__ . '/fixtures/from/road.jpg';
43+
$converter = new ImageConverter();
44+
45+
$this->assertTrue($converter->convert($from, $to, 5));
46+
}
47+
}
File renamed without changes.
File renamed without changes.

tests/to/dices.webp

-10.6 KB
Binary file not shown.

tests/to/road.bmp

-580 KB
Binary file not shown.

tests/to/road.gif

-170 KB
Binary file not shown.

tests/to/road.jpg

-8.04 KB
Binary file not shown.

tests/to/road.png

-490 KB
Binary file not shown.

tests/to/road.webp

-20.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)