Skip to content

Commit

Permalink
First attempt at getting PDF thumb creation working again.
Browse files Browse the repository at this point in the history
  • Loading branch information
cheesegrits committed Sep 3, 2018
1 parent 0a8731b commit 0307227
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
11 changes: 10 additions & 1 deletion libraries/fabrik/fabrik/Helpers/Image.php
Expand Up @@ -138,18 +138,21 @@ protected static function testGD()
*/
protected static function testImagemagick()
{
$im = array();

if (function_exists('NewMagickWand'))
{
$im['IM'] = 'Magick wand';
}
else
{
/*
$status = '';
$output = array();
@exec('convert -version', $output, $status);
$im = array();
if (!$status && class_exists('Imagick'))
if ($status && class_exists('Imagick'))
{
if (preg_match("/imagemagick[ \t]+([0-9\.]+)/i", $output[0], $matches))
{
Expand All @@ -158,6 +161,12 @@ protected static function testImagemagick()
}
unset($output, $status);
*/

if (class_exists('Imagick'))
{
$im['IM'] = 'Imagick';
}
}

return $im;
Expand Down
8 changes: 8 additions & 0 deletions libraries/fabrik/fabrik/Helpers/Image/Image.php
Expand Up @@ -74,7 +74,15 @@ public function getImgType($filename)
return 'png';
break;
default:
$pathInfo = pathInfo($filename);

if (ArrayHelper::getValue($pathInfo, 'extension', '') === 'pdf')
{
return 'pdf';
};

return false;
break;
}
}

Expand Down
75 changes: 75 additions & 0 deletions libraries/fabrik/fabrik/Helpers/Image/Imageim.php
Expand Up @@ -79,6 +79,7 @@ public function resize($maxWidth, $maxHeight, $origFile, $destFile, $quality = 1
// OK, it's a PDF, so first we need to add the page number we want to the source filename
$pdfFile = $fromFile . '[0]';

/*
if (is_callable('exec'))
{
$destFile = str_replace('.pdf', '.png', $destFile); // Output File
Expand All @@ -87,6 +88,7 @@ public function resize($maxWidth, $maxHeight, $origFile, $destFile, $quality = 1
}
else
{
*/
// Now just load it, set format, resize, save and garbage collect.
// Hopefully IM will call the right delegate (ghostscript) to load the PDF.
$im = new \Imagick($pdfFile);
Expand All @@ -95,7 +97,9 @@ public function resize($maxWidth, $maxHeight, $origFile, $destFile, $quality = 1
$im->writeImage($destFile);
// as destroy() is deprecated
$im->clear();
/*
}
*/
}
else
{
Expand Down Expand Up @@ -131,4 +135,75 @@ public function resize($maxWidth, $maxHeight, $origFile, $destFile, $quality = 1
MagickWriteImage($resource, $destFile);
}
}

/*
* Check for EXIF orientation data, and rotate image accordingly
*
* @param string path to image file
*/
public function rotateImageFromExif($src, $dest)
{
if (function_exists('exif_read_data'))
{
$exif = exif_read_data($src);
if ($exif && isset($exif['Orientation']))
{
$orientation = $exif['Orientation'];
if ($orientation != 1)
{
$deg = 0;
switch ($orientation)
{
case 3:
$deg = 180;
break;
case 6:
$deg = 270;
break;
case 8:
$deg = 90;
break;
}
if ($deg)
{
self::rotate($src, $dest, $deg);
}
}
}
}
}

/**
* Rotate an image
*
* @param string $source filepath
* @param string $dest output path, if empty defaults to source
* @param int $degrees number of degrees to rotate
*
* @return array (image object, rotated images width, rotated images height)
*/
public function rotate($source, $dest = '', $degrees = 0)
{
if (empty($dest))
{
$dest = $source;
}

$source = $this->imageCreateFrom($source);
$app = JFactory::getApplication();

// Rotates the image
$rotate = imagerotate($source, $degrees, 0);

if ($rotate === false)
{
$app->enqueueMessage('Image rotation failed', 'notice');
}

$this->imageToFile($dest, $rotate);
list($width, $height) = getimagesize($dest);

return array($rotate, $width, $height);
}

}

0 comments on commit 0307227

Please sign in to comment.