Skip to content

Commit

Permalink
Have I been dreaming again?
Browse files Browse the repository at this point in the history
  • Loading branch information
beryllium committed Mar 5, 2015
1 parent 9ee5caf commit 0524b58
Show file tree
Hide file tree
Showing 14 changed files with 475 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
vendor
composer.lock
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: php

php:
- 5.4
- 5.5
- hhvm

install:
- composer --prefer-source install

script: phpunit --coverage-text --verbose
30 changes: 30 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Beryllium\Icelus\DependencyInjection;

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

/**
* Configuration.
*/
class Configuration implements ConfigurationInterface
{
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder;

$rootNode = $treeBuilder->root('icelus');

$rootNode
->children()
->scalarNode('prefix')->defaultValue('/_thumbs')->end()
->scalarNode('output_dir')->defaultNull()->end()
->end();

return $treeBuilder;
}
}
23 changes: 23 additions & 0 deletions DependencyInjection/IcelusExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Beryllium\Icelus\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\Config\FileLocator;

/**
* Icelus Extension.
*/
class IcelusExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');
}
}
12 changes: 12 additions & 0 deletions IcelusBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Beryllium\Icelus;

use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
* Icelus Bundle.
*/
class IcelusBundle extends Bundle
{
}
85 changes: 85 additions & 0 deletions ImageService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Beryllium\Icelus;

use Imanee\Imanee;

/**
* Service for Image Manipulation.
*/
class ImageService
{
public $env;
public $imanee;
public $source_dir;
public $output_dir;
public $prefix = '/_thumbs';
public $completed = array();

/**
* Constructor
*
* @param Imanee $imanee Performs the required image manipulations
* @param string $source_dir Where to find the images
* @param string $output_dir Where to save the images
* @param string $env Current environment type
*/
public function __construct(Imanee $imanee, $source_dir, $output_dir, $env)
{
$this->imanee = $imanee;
$this->source_dir = rtrim($source_dir, '/');
$this->output_dir = rtrim($output_dir, '/');
$this->env = $env;
}

/**
* Prepare the output directory.
*
* This makes sure we have somewhere to put the thumbnails once we've generated them.
*/
protected function prepOutputDir()
{
if (!is_dir($this->output_dir . $this->prefix)) {
mkdir($this->output_dir . $this->prefix);
}
}

/**
* Generate a thumbnail
*
* @param string $image Path to image file (relative to source_dir)
* @param int $width Width, in pixels (default: 150)
* @param int $height Height, in pixels (default: 150)
* @param bool $crop When set to true, the thumbnail will be cropped
* from the center to match the given size
*
* @return string Location of the thumbnail, for use in <img> tags
*/
public function thumbnail($image, $width = 150, $height = 150, $crop = false)
{
// no sense duplicating work - only process image if thumbnail doesn't already exist
if (!isset($this->completed[$image][$width][$height][$crop]['filename'])) {
$this->prepOutputDir();
$this->imanee->load($this->source_dir . '/' . $image)->thumbnail($width, $height, $crop);
$thumb_name = vsprintf(
'%s-%sx%s%s.%s',
array(
md5($image),
$width,
$height,
($crop ? '-cropped' : ''),
strtolower($this->imanee->getFormat())
)
);

// write the thumbnail to disk
file_put_contents(
$this->output_dir . $this->prefix . '/' . $thumb_name,
$this->imanee->output()
);
$this->completed[$image][$width][$height][$crop]['filename'] = $thumb_name;
}

return $this->prefix . '/' . $this->completed[$image][$width][$height][$crop]['filename'];
}
}
93 changes: 93 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
Icelus
======

> _Icelus, otherwise known as "Scaled Sculpin", are a small fish native to the North Pacific._
Icelus is a quick and easy thumbnail generator for your Sculpin-based websites and blogs.

Requirements
------------

Icelus requires:

* PHP 5.4+
* Imagick extension (installable via apt-get, pecl, or yum)
* Imanee library ([imanee.io](http://imanee.io) - fetched automatically by Composer)

Installation
------------

If you are using the Phar-based Sculpin utility, you can create or modify a sculpin.json file in your project root and add `"beryllium/icelus"` to the `"requires"` block. Then, run `sculpin install` or `sculpin update` to fetch the required dependencies.

{
"requires": {
"beryllium/icelus": "*"
}
}

Alternatively, if you are using a Composer-based sculpin installation, you should simply be able to run `composer require beryllium/icelus` to get things rolling.

Once the library is installed, you have to tell Sculpin how to load it. You can do this by creating or modifying a `app/SculpinKernel.php` file to resemble the following:

<?php

class SculpinKernel extends \Sculpin\Bundle\SculpinBundle\HttpKernel\AbstractKernel
{
protected function getAdditionalSculpinBundles()
{
return array(
'Beryllium/Icelus/IcelusBundle',
);
}
}

__Note:__ The class name should be a string, not an object instantiation. (This differs from the way Symfony 2 configures bundles.)

Usage
-----

Icelus exposes a `thumbnail` function in Twig, which you can use either on its own or by creating Twig macros to customize the output.

___thumbnail(image, width, height, crop)___

* __image__ (string): The relative path to the image in the `source/` folder.
* __width__ (int): Maximum width, in pixels
* __height__ (int): Maximum height, in pixels
* __crop__ (bool): False will fit the whole image inside the provided dimensions. True will crop the image from the center. Default: __FALSE__

Inline Example:

<a href="image.jpg"><img src="{% thumbnail('image.jpg', 100, 100) %}"></a>

Macro Example:

index.html:

{% import '_macros.html.twig' as m %}

<h1>Gone Fishin'!</h1>
{{ m.small_thumbnail('image.jpg', 'A picture from my fishing trip') }}


_macros.html.twig:

{% macro small_thumbnail(image, caption) %}
<a href="{{ image }}">
<img src="{% thumbnail(image, 100, 100) %}">
<br>
<em>{{ caption }}</em>
</a>
{% endmacro %}

A service called `icelus.service` is also added to the Sculpin dependency injection container, which you can use in your own Sculpin extensions.

For raw access to the underlying Imanee library, the service is named `icelus.imanee`. If you need to go deeper, you can then retrieve an Imagick instance using `$imanee->getIMResource()`.

Technically speaking, this extension could also be used as a Symfony 2 bundle. This has not been tested, but experimentation is welcome.

Future Plans
------------

I would like for Icelus to expose more features of the underlying Imanee library, particularly with regard to watermarks and drawing text onto images. Imanee's support for animated gifs could possibly also be advantageous in some way.

I would also like for Icelus to be compatible with a wide variety of PHP frameworks and workflows. I've concentrated on having it as a Twig extension, but it could also work with other template systems and even Markdown-style parsers.
30 changes: 30 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="icelus.imanee.class">Imanee\Imanee</parameter>
<parameter key="icelus.image_service.class">Beryllium\Icelus\ImageService</parameter>
<parameter key="icelus.twig_extension.class">Beryllium\Icelus\TwigImageExtension</parameter>
</parameters>

<services>

<service id="icelus.imanee" class="%icelus.imanee.class%" />

<service id="icelus.service" class="%icelus.image_service.class%">
<argument type="service" id="icelus.imanee" />
<argument>%sculpin.source_dir%</argument>
<argument>%sculpin.output_dir%</argument>
<argument>%kernel.environment%</argument>
</service>

<service id="icelus.extension.image" class="%icelus.twig_extension.class%">
<argument type="service" id="icelus.service" />
<tag name="twig.extension" />
</service>

</services>

</container>
30 changes: 30 additions & 0 deletions TwigImageExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Beryllium\Icelus;

use Twig_Extension;

/**
* Exposes a "thumbnail" function to Twig templates
*/
class TwigImageExtension extends Twig_Extension
{
public $service = null;

public function __construct($service)
{
$this->service = $service;
}

public function getName()
{
return 'image_extension';
}

public function getFunctions()
{
return array(
new \Twig_SimpleFunction('thumbnail', array($this->service, 'thumbnail'))
);
}
}
28 changes: 28 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "beryllium/icelus",
"description": "Thumbnail generator for Sculpin-based websites",
"homepage": "http://whateverthing.com",
"keywords": ["images", "thumbnails", "sculpin", "sculpin-plugin"],
"license": "MIT",
"authors": [
{
"name": "Kevin Boyd",
"email": "[email protected]",
"homepage": "http://whateverthing.com"
}
],
"require": {
"imanee/imanee": "*"
},
"require-dev": {
"phpunit/phpunit": "~4.5",
"mikey179/vfsStream": "~1.4.0",
"symfony/config": "~2.1",
"symfony/dependency-injection": "~2.1",
"symfony/http-kernel": "~2.6",
"twig/twig": "~1.18"
},
"autoload": {
"psr-4": { "Beryllium\\Icelus\\": "" }
}
}
17 changes: 17 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false">
<testsuites>
<testsuite name="Beryllium/Icelus">
<directory>./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
Loading

0 comments on commit 0524b58

Please sign in to comment.