Skip to content

Commit

Permalink
Huge changes
Browse files Browse the repository at this point in the history
  • Loading branch information
peter279k committed Apr 28, 2019
1 parent 31a1dfb commit f34c2a5
Show file tree
Hide file tree
Showing 17 changed files with 250 additions and 53 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor/
tests/fixtures/to/*
16 changes: 16 additions & 0 deletions .travis.yml
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
31 changes: 31 additions & 0 deletions composer.json
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"
}
}
16 changes: 16 additions & 0 deletions example.php
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);
7 changes: 0 additions & 7 deletions index.php

This file was deleted.

14 changes: 14 additions & 0 deletions phpunit.xml
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>
124 changes: 124 additions & 0 deletions src/ImageConverter.php
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;
}
}
46 changes: 0 additions & 46 deletions src/php-image-converter.php

This file was deleted.

47 changes: 47 additions & 0 deletions tests/ImageConverterTest.php
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 removed tests/to/dices.webp
Binary file not shown.
Binary file removed tests/to/road.bmp
Binary file not shown.
Binary file removed tests/to/road.gif
Binary file not shown.
Binary file removed tests/to/road.jpg
Binary file not shown.
Binary file removed tests/to/road.png
Binary file not shown.
Binary file removed tests/to/road.webp
Binary file not shown.

0 comments on commit f34c2a5

Please sign in to comment.