Skip to content

Commit

Permalink
Updates PdfTransformer so that imagick is injected
Browse files Browse the repository at this point in the history
With this update Imagick is injected in the transformer via
Symfony's Service Container.
The check, relative to imagick extension being loaded, is now up
to the user: the service container will complain about Imagick
not found before injecting it in the transformer.
  • Loading branch information
lucasaba committed Mar 19, 2012
1 parent 8dd784e commit af67f06
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
25 changes: 16 additions & 9 deletions Imagine/Data/Transformer/PdfTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,28 @@

class PdfTransformer
{
/**
*
* @var \Imagick
*/
private $imagick;

public function __construct(\Imagick $imagick)
{
$this->imagick = $imagick;
}

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'))
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 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);
$this->imagick->readImage($absolutePath.'[0]');
$this->imagick->setImageFormat('png');
$this->imagick->writeImage("$absolutePath.png");
$this->imagick->clear();
}
//finally update $absolutePath
$absolutePath .= '.png';
}
return $absolutePath;
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,16 +406,20 @@ 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.
PdfTransformer uses a php imagick object (injected via the service container)
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:
imagick_object:
class: Imagick
acme_custom_transformer:
class: Acme\ImagineBundle\Imagine\Data\Transformer\MyCustomTransformer
arguments:
- '@imagick_object'
custom_loader:
class: Acme\ImagineBundle\Imagine\Data\Loader\MyCustomDataLoader
tags:
Expand Down

2 comments on commit af67f06

@Tremblay91
Copy link

Choose a reason for hiding this comment

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

Hello,
I tried to use your code to convert a pdf, when I joined my page, it returns me out the first page, but in pdf format and not in png. I can not find where does the error.
Can you help me?

Here are my different files:

services.yml :

imagick_object:
    class: Imagick

liip_pdf_transformer:
    class: Liip\ImagineBundle\Imagine\Data\Transformer\PdfTransformer
    arguments:
        - '@imagick_object'

pdf_loader:
    class: Liip\ImagineBundle\Imagine\Data\Loader\ExtendedFileSystemLoader
    tags:
        - { name: liip_imagine.data.loader, loader: pdf_data_loader }
    arguments:
        - '@liip_imagine'
        - %liip_imagine.formats%
        - %liip_imagine.data_root%
        - ['@liip_pdf_transformer']

config.yml :

liip_imagine:
driver: imagick
filter_sets:
my_thumb:
quality: 75
filters:
thumbnail: { size: [120, 90], mode: inset }
pdf_thumb:
data_loader: pdf_data_loader

@Tremblay91
Copy link

Choose a reason for hiding this comment

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

Hello,
I tried to use your code to convert a pdf, when I joined my page, it returns me out the first page, but in pdf format and not in png. I can not find where does the error.
Can you help me?

Here are my different files:

services.yml :

imagick_object:
    class: Imagick

liip_pdf_transformer:
    class: Liip\ImagineBundle\Imagine\Data\Transformer\PdfTransformer
    arguments:
        - '@imagick_object'

pdf_loader:
    class: Liip\ImagineBundle\Imagine\Data\Loader\ExtendedFileSystemLoader
    tags:
        - { name: liip_imagine.data.loader, loader: pdf_data_loader }
    arguments:
        - '@liip_imagine'
        - %liip_imagine.formats%
        - %liip_imagine.data_root%
        - ['@liip_pdf_transformer']

config.yml :

liip_imagine:
driver: imagick
filter_sets:
my_thumb:
quality: 75
filters:
thumbnail: { size: [120, 90], mode: inset }
pdf_thumb:
data_loader: pdf_data_loader

Please sign in to comment.