Hi there!
Is there maybe a kirby-way for generating a preview image of a pdf file? I think its much easier for my client if they don’t have to generate a jpeg or png of the title page by theirselves.
Greetings!
Markus
Hi there!
Is there maybe a kirby-way for generating a preview image of a pdf file? I think its much easier for my client if they don’t have to generate a jpeg or png of the title page by theirselves.
Greetings!
Markus
Have a look at this thread at Stackoverflow. In Kirby, you are able to use PHP (and include libraries) as you like, e. g. as a plugin.
Yeah, I know that. But i thought, there might be a way of doing this with the toolkits thumb class. But as far as I read the code, it just can handle “real” images…
Ah, okay. Still, the solution posted at Stackoverflow doesn’t seem to be to complex (at least if your hosting meets the requirements).
Based on your suggestion I’ve made a quick and dirty plugin that works for me, using the /thumbs
directory for “caching”:
<?php
function preview($file, $size)
{
$key = 'preview_'. md5($file->root());
$thumb = kirby()->roots()->thumbs() .DS. $key;
if (f::exists($thumb))
{
if ( (f::modified($thumb) > $file->modified())) {
return kirby()->urls()->thumbs() .'/'. $key;
}
f::remove($thumb);
}
$image = new Imagick($file->root().'[0]'); // first page only
$image->flattenImages();
$image->setImageFormat('jpg');
$image->setImageCompression(Imagick::COMPRESSION_JPEG);
$image->setImageCompressionQuality(80);
$image->thumbnailImage($size, 0);
$image->writeImage($thumb);
return kirby()->urls()->thumbs() .'/'. $key;
};
Regards
Heiko
I was turning Heiko’s plugin into a file method, so once imagemagick had made the jpg, we could carry on chaining and perhaps use Kirby’s native thumb() or html() image wrapper.
e.g. echo '$file->preview()->html
However it still appears to be returning a string of the file path, and not returning a new file object. If anyone’s still listening, what am I missing? Is new Media() or Assets() not really a new File() ?
` file::$methods['preview'] = function($file, $size='800', $page='0') {
$key = 'preview_'. md5($file->root());
$thumb = kirby()->roots()->thumbs() .DS. $key;
if (f::exists($thumb))
{
if ( (f::modified($thumb) > $file->modified())) {
return kirby()->urls()->thumbs() .'/'. $key;
}
f::remove($thumb);
}
$image = new Imagick($file->root().'['.$page.']');
$image->setImageBackgroundColor('white');
$image->setImageAlphaChannel(imagick::ALPHACHANNEL_REMOVE);
$image->mergeImageLayers(imagick::LAYERMETHOD_FLATTEN);
$image->setImageFormat('jpg');
$image->setImageCompression(Imagick::COMPRESSION_JPEG);
$image->setImageCompressionQuality(80);
$image->thumbnailImage($size, 0);
$image->writeImage($thumb);
return new Media(kirby()->urls()->thumbs() .'/'. $key);
};
`
Hi there,
Is this plugin still working for Kirby 2.5 ?
I can’t get it work. How to trigger pdf conversion ?
Thanks
Hi everyone.
Following this thread and other similars I managed to create a hook for the panel to create a jpeg of the first page of a pdf using Imagick.
It works on localhost but it doesn’t on the server (I’m using Dreamhost).
I get an error that the file can’t be read. At the beggining I thought that Imagemagick was not installed on my server but I tried the same code with a png to jpeg and worked fine.
So the problem is with pdf. After googling I saw that this is related with Ghostscript. But according to Dreamhost forum Imagemagick and GS are installed by default on the server. So I don‘t if a problem with the Imagemagick included on Kirby 3 or anything else. I completly lost. Any ideas?
Thanks in advance.
Here is my code
'hooks' => [
'file.create:after' => function ($file) {
try {
if ($file->extension() == 'pdf' ) {
$output_format = "jpeg";
$preview_page = "1";
$resolution = "72";
$filePath = $file->root();
$output_file = $file->page()->root()."/".$file->name().".jpeg";
$img_data = new Imagick();
$img_data->setResolution( $resolution, $resolution );
$img_data->readImage( $filePath.'[0]');
$img_data->setImageFormat( $output_format );
$img_data->writeImage($output_file);
}
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
]
If you have access to Imagemagick’s policy.xml
, check if converting PDF files is prohibited.
Check if you find an entry like this:
<policy domain="coder" rights="none" pattern="PDF" />
IMO, it has nothing to do with Kirby. You could also try to use the convert
command on the command line to see if that works or not.
If you don’t have access to the policy.xml
file and if converting from PDF to JPG doesn’t work on the command line either, contact your provider.
I tried with convert via ssh and it didn’t work for pdf, so I assume that converting pdf is prohibited and I don’t have access to the policy.xml. I already contacted my provider. I’m wating for an answer from them, thanks!