Skip to content

Commit

Permalink
add Mesavolt\Service\ImagingService
Browse files Browse the repository at this point in the history
  • Loading branch information
Mickael GOETZ committed Aug 1, 2018
0 parents commit 51d44e9
Show file tree
Hide file tree
Showing 20 changed files with 533 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .coveralls.yml
@@ -0,0 +1,6 @@
# thanks to
# https://www.tomasvotruba.cz/blog/2017/06/12/how-to-require-minimal-code-coverage-for-github-pull-requests-with-coveralls/

service_name: travis-ci
coverage_clover: coverage.xml # file generated by phpunit
json_path: coverage.json # file generated by php-coveralls
8 changes: 8 additions & 0 deletions .gitignore
@@ -0,0 +1,8 @@
vendor/
composer.lock
.DS_Store
.idea
/tests/cache-dir

/coverage.xml
/coverage
10 changes: 10 additions & 0 deletions .travis.yml
@@ -0,0 +1,10 @@
language: php
php:
- '7.1'

install: composer install
script: vendor/bin/phpunit --coverage-clover coverage.xml

after_script:
# upload coverage.xml file to Coveralls to analyze it
- travis_retry vendor/bin/php-coveralls --verbose
21 changes: 21 additions & 0 deletions LICENSE.md
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 MesaVolt, SARL

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 the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
86 changes: 86 additions & 0 deletions README.md
@@ -0,0 +1,86 @@
# Mesavolt/ImagingBundle


## Installation

### Applications that use Symfony Flex

Open a command console, enter your project directory and execute:

```console
composer require mesavolt/imaging-bundle
```

That's it. Flex automagically enables the bundle for you. Go to the **Configuration**
section of this README to see how you can customize the bundle's behavior.

### Applications that don't use Symfony Flex

#### Step 1: Download the Bundle

Open a command console, enter your project directory and execute the
following command to download the latest stable version of this bundle:

```console
composer require mesavolt/imaging-bundle
```

This command requires you to have Composer installed globally, as explained
in the [installation chapter](https://getcomposer.org/doc/00-intro.md)
of the Composer documentation.

#### Step 2: Enable the Bundle

Then, enable the bundle by adding it to the list of registered bundles
in the `app/AppKernel.php` file of your project:

```php
<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
new Mesavolt\ImagingBundle\ImagingBundle(),
);

// ...
}

// ...
}
```

## Usage

Inject the `Mesavolt\SimpleCache` service into your services and controllers
(or get the `mesavolt.simple_cache` service from the container) :

```php
<?php

namespace App;


use Mesavolt\ImagingBundle\Service\ImagingService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class HomeController extends AbstractController
{
public function index(ImagingService $imagingService)
{
$relative = '/public/thumbnails/thumbnail.jpeg';
$path = $this->getParameter('kernel.project_dir').$relative;
$imagingService->shrink('/tmp/image.jpg', $path);

return $this->render('home/index.html.twig', [
'shrunk' => $relative
]);
}
}

```
Empty file added Tests/.gitkeep
Empty file.
Binary file added Tests/Resources/images/gif.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tests/Resources/images/jpg.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tests/Resources/images/png.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tests/Resources/images/tall.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tests/Resources/images/tiff.tiff
Binary file not shown.
Binary file added Tests/Resources/images/wide.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
134 changes: 134 additions & 0 deletions Tests/Service/ImageServiceTest.php
@@ -0,0 +1,134 @@
<?php

namespace Mesavolt\Tests\Service;


use Mesavolt\ImagingBundle\Service\ImagingService;
use PHPUnit\Framework\TestCase;

class ImageServiceTest extends TestCase
{
/** @var ImagingService */
protected $service;
/** @var array */
protected $tempFiles;
protected $tall;
protected $wide;


protected function setUp()/* The :void return type declaration that should be here would cause a BC issue */
{
$this->service = new ImagingService();
$this->tempFiles = [];

$this->tall = __DIR__.'/../Resources/images/tall.png';
$this->wide = __DIR__.'/../Resources/images/wide.png';
}

protected function tearDown()/* The :void return type declaration that should be here would cause a BC issue */
{
$this->service = null;

foreach($this->tempFiles as $file) {
@unlink($file);
}
}

protected function createTempFile(): string
{
$file = tempnam(sys_get_temp_dir(), null);
$this->tempFiles[] = $file;

return $file;
}

public function testShrink()
{
// handles JPG, PNG and GIF
foreach(['jpg', 'gif', 'png'] as $ext) {
$this->assertTrue($this->service->shrink(__DIR__."/../Resources/images/$ext.$ext", $this->createTempFile(), 1));
}

// doesn't handle tiff
$this->expectException(\InvalidArgumentException::class);
$this->assertTrue($this->service->shrink(__DIR__."/../Resources/images/tiff.tiff", $this->createTempFile(), 1));

// shrinks horizontal image to match width
$tallShrunkW = $this->createTempFile();
$this->service->shrink($this->tall, $tallShrunkW, 100, null);
[$w, $h] = getimagesize($tallShrunkW);
$this->assertEquals(100, $w, 'Width should be 100px');
$this->assertEquals(200, $h, 'Height should be 200px');

// shrinks horizontal image to match height
$tallShrunkH = $this->createTempFile();
$this->service->shrink($this->tall, $tallShrunkH, null, 1000);
[$w, $h] = getimagesize($tallShrunkH);
$this->assertEquals(500, $w, 'Width should be 500px');
$this->assertEquals(1000, $h, 'Height should be 1000px');

// shrinks vertical image to match width
$wideShrunkW = $this->createTempFile();
$this->service->shrink($this->wide, $wideShrunkW, 1000, null);
[$w, $h] = getimagesize($wideShrunkW);
$this->assertEquals(1000, $w, 'Width should be 1000px');
$this->assertEquals(500, $h, 'Height should be 500px');

// shrinks vertical image to match height
$wideShrunkH = $this->createTempFile();
$this->service->shrink($this->wide, $wideShrunkH, null, 100);
[$w, $h] = getimagesize($wideShrunkH);
$this->assertEquals(200, $w, 'Width should be 200px');
$this->assertEquals(100, $h, 'Height should be 100px');

// asking for 3000*3000 won't resize the image because it's already smaller
$wideUntouched = $this->createTempFile();
$this->service->shrink($this->wide, $wideUntouched, 3000, 3000);
[$w, $h] = getimagesize($wideUntouched);
$this->assertEquals(2000, $w, 'Width should be 2000px');
$this->assertEquals(1000, $h, 'Height should be 1000px');

// asking for 300*100 will result in 200*100 to keep tall proportions
$tallShrunk300x100 = $this->createTempFile();
$this->service->shrink($this->tall, $tallShrunk300x100, 300, 100);
[$w, $h] = getimagesize($tallShrunk300x100);
$this->assertEquals(50, $w, 'Width should be 50px');
$this->assertEquals(100, $h, 'Height should be 100px');

// asking for 100*300 will result in 100*50 to keep tall proportions
$tallShrunk100x300 = $this->createTempFile();
$this->service->shrink($this->tall, $tallShrunk100x300, 100, 300);
[$w, $h] = getimagesize($tallShrunk100x300);
$this->assertEquals(100, $w, 'Width should be 100px');
$this->assertEquals(200, $h, 'Height should be 200px');

// asking for 300*100 will result in 200*100 to keep wide proportions
$wideShrunk300x100 = $this->createTempFile();
$this->service->shrink($this->wide, $wideShrunk300x100, 300, 100);
[$w, $h] = getimagesize($wideShrunk300x100);
$this->assertEquals(200, $w, 'Width should be 200px');
$this->assertEquals(100, $h, 'Height should be 100px');

// asking for 100*300 will result in 100*50 to keep wide proportions
$wideShrunk100x300 = $this->createTempFile();
$this->service->shrink($this->wide, $wideShrunk100x300, 100, 300);
[$w, $h] = getimagesize($wideShrunk100x300);
$this->assertEquals(100, $w, 'Width should be 100px');
$this->assertEquals(50, $h, 'Height should be 50px');

// __FILE__ is not an image, shrinking fails and returns false
$this->assertFalse($this->service->shrink(__FILE__, '/dev/null'));
}

public function testGenerateWebp()
{
$webp = $this->createTempFile();
$this->service->generateWebp($this->wide, $webp);
[$w, $h] = getimagesize($webp);
// dimensions shouldn't have changed
$this->assertEquals(2000, $w, 'Width should be 2000px');
$this->assertEquals(1000, $h, 'Height should be 1000px');
// generated image must be webp
$this->assertEquals(IMAGETYPE_WEBP, exif_imagetype($webp));
}
}
39 changes: 39 additions & 0 deletions composer.json
@@ -0,0 +1,39 @@
{
"name": "mesavolt/imaging-bundle",
"description": "Symfony bundle to facilitate image manipulation in PHP",
"keywords": ["image", "gd", "webp", "bundle"],
"license": "MIT",
"authors": [
{
"name": "MesaVolt",
"email": "contact@mesavolt.com"
}
],
"type": "symfony-bundle",
"config": {
"sort-packages": true
},
"require": {
"php": ">=7.1",
"ext-gd": "*",
"ext-exif": "*",
"rosell-dk/webp-convert": "^1.1",
"symfony/config": "^3.3||^4.1",
"symfony/dependency-injection": "^3.3||^4.1",
"symfony/http-kernel": "^3.3||^4.1"
},
"require-dev": {
"php-coveralls/php-coveralls": "^2.1",
"phpunit/phpunit": "^7.2"
},
"autoload": {
"psr-4": {
"Mesavolt\\ImagingBundle\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Mesavolt\\Tests\\": "Tests/"
}
}
}
28 changes: 28 additions & 0 deletions phpunit.xml
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/7.2/phpunit.xsd"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
stopOnRisky="false"
verbose="true">
<testsuites>
<testsuite name="Project Test Suite">
<directory phpVersion="7.1" phpVersionOperator=">=">Tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./src/</directory>
</whitelist>
</filter>
</phpunit>
32 changes: 32 additions & 0 deletions src/DependencyInjection/Configuration.php
@@ -0,0 +1,32 @@
<?php

namespace Mesavolt\ImagingBundle\DependencyInjection;


use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
* This is the class that validates and merges configuration from your app/config files
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
*/
class Configuration implements ConfigurationInterface
{
/**
* {@inheritDoc}
* @throws \InvalidArgumentException
*/
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('mesavolt_imaging');

// Here you should define the parameters that are allowed to
// configure your bundle. See the documentation linked above for
// more information on that topic.

return $treeBuilder;

}
}

0 comments on commit 51d44e9

Please sign in to comment.