Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add file transformers to the file loader #57

Merged
merged 4 commits into from
Mar 19, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions Imagine/Data/Loader/ExtendedFileSystemLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Liip\ImagineBundle\Imagine\Data\Loader;

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

use Imagine\Image\ImagineInterface;

class ExtendedFileSystemLoader extends FileSystemLoader
{
/**
*
* @var array
*/
private $transformers;

/**
* Constructs
*
* @param ImagineInterface $imagine
* @param array $formats
* @param string $rootPath
* @param array $transformers
*/
public function __construct(ImagineInterface $imagine, $formats, $rootPath, array $transformers)
{
parent::__construct($imagine, $formats, $rootPath);
$this->transformers = $transformers;
}

/**
* Apply transformers to the file
*
* @param $absolutePath
* @return array
*/
protected function getFileInfo($absolutePath)
{
if (!empty($this->transformers)) {
foreach ($this->transformers as $transformer) {
$absolutePath = $transformer->applyTransform($absolutePath);
}
}
return pathinfo($absolutePath);
}
}
2 changes: 1 addition & 1 deletion Imagine/Data/Loader/FileSystemLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,4 @@ public function find($path)

return $this->imagine->open($absolutePath);
}
}
}
26 changes: 26 additions & 0 deletions Imagine/Data/Transformer/PdfTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Liip\ImagineBundle\Imagine\Data\Transformer;

class PdfTransformer
{
public function applyTransform($absolutePath)
{
$info = pathinfo($absolutePath);
if (isset($info['extension']) && strpos(strtolower($info['extension']), 'pdf') !== false) {
//Check if Imagick extension is loaded
if (!extension_loaded('Imagick'))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please do not omit curly brackets

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

opps .. i also noticed another thing. i would prefer it if we would inject the Imagick instance .. i don't have experience with its API .. but i assume we could also call setFilename() instead of passing the absolute path to the constructor? could you do a PR to change this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm actually it seems Imagine itself is using the same approach .. the Imagick OO API also seems a bit strange:
https://github.com/avalanche123/Imagine/blob/master/lib/Imagine/Imagick/Imagine.php

@avalanche123 do you have a tip for us here what would make the most sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. It would be better. I'm checkin the API and PR the changes!

throw new \RuntimeException ("PHP Imagick extension is not loaded but required by the PdfTransformer");

//If it doesn't exists extract the first page of the PDF
if (!file_exists("$absolutePath.png")) {
$img = new \Imagick($absolutePath.'[0]');
$img->setImageFormat('png');
$img->writeImages($absolutePath.'.png', true);
}
//finally update $absolutePath
$absolutePath .= '.png';
}
return $absolutePath;
}
}
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,39 @@ liip_imagine:
For an example of a data loader implementation, refer to
`Liip\ImagineBundle\Imagine\Data\Loader\FileSystemLoader`.

## Extending the image loader with data transformers

You can extend a custom data loader to support virtually any file type using transformers.
A data tranformer is intended to transform a file before actually rendering it. You
can refer to `Liip\ImagineBundle\Imagine\Data\Loader\ExtendedFileSystemLoader` and
to `Liip\ImagineBundle\Imagine\Data\Transformer\PdfTransformer` as an example.

ExtendedFileSystemLoader extends FileSystemLoader and takes, as argument, an array of transformers.
In the example, when a file with the pdf extension is passed to the data loader,
PdfTransformer uses php imagick extension to extract the first page of the document
and returns it to the data loader as a png image.

To tell the bundle about the transformers, you have to register them as services
with the new loader:

```yml
services:
acme_custom_transformer:
class: Acme\ImagineBundle\Imagine\Data\Transformer\MyCustomTransformer
custom_loader:
class: Acme\ImagineBundle\Imagine\Data\Loader\MyCustomDataLoader
tags:
- { name: liip_imagine.data.loader, loader: custom_data_loader }
arguments:
- '@liip_imagine'
- %liip_imagine.formats%
- %liip_imagine.data_root%
- [ '@acme_custom_transformer' ]
```

Now you can use your custom data loader, with its transformers, setting it
as in the previous section.

## Custom cache resolver

The ImagineBundle allows you to add your custom cache resolver classes. The only
Expand Down
2 changes: 1 addition & 1 deletion Resources/config/imagine.xml
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,4 @@
</service>

</services>
</container>
</container>