Skip to content

Instantly share code, notes, and snippets.

@slashfan
Created March 11, 2015 08:40
Show Gist options
  • Save slashfan/b68e58fe889fafeeaa45 to your computer and use it in GitHub Desktop.
Save slashfan/b68e58fe889fafeeaa45 to your computer and use it in GitHub Desktop.
Basic ImageExtractor PHP Class - Extract first page of a PDF file to an image (jpg or png) using Imagick PHP extension
<?php
namespace Acme\Util;
/**
* ImageExtractor
*/
class ImageExtractor
{
const FORMAT_JPG = 'jpg';
const FORMAT_PNG = 'png';
/**
* @param string $source source filepath
* @param string $destination destination filepath
* @param string $format destination format
*
* @return bool
*/
public static function extract($source, $destination, $format = self::FORMAT_JPG)
{
if (!extension_loaded('Imagick')) {
return false;
}
$imagick = new \Imagick($source . '[0]');
$imagick->setFormat($format);
return $imagick->writeImage($destination);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment