Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jbowman99's avatar

convert PDF into thumbnail

I need to convert an uploaded pdf's first page into a thumbnail. installed Imagick, and GhostScript and using intervention/image to try to accomplish this, and am not getting any results.

the upload and storage of the file is working fine

$flyer = new Flyer();
        $flyer->title = $request->title;
        $flyer->type = $request->type;
        if ($request->file('file')->isValid())
        {
            $flyerpath = $request->file->store('flyers');
            $flyer->file = $flyerpath;
            $flyer->thumbnail = '';
            $flyer->save();
        }

currently just making the thumbnail an empty string. tried something like this to no avail.

$im = new imagick('file.pdf[0]');
$im->setImageFormat('jpg');
header('Content-Type: image/jpeg');

0 likes
2 replies
gabriel007's avatar

May not be the most "robust/elegant" way, but as a pointer, I am using shell_exec to accomplish this (with Imagick installed).

...
shell_exec("convert '$flyer->file'[0] -colorspace RGB -geometry 200 -flatten '$flyer->thumbnail_path'");
$flyer->save();

Just make sure that the paths for both $flyer->file and $flyer->thumbnail_path are defined beforehand.

The back-ticks around $flyer->file and $flyer->thumbnail_path, are for filenames with spaces (learned the hard way).

While it works, I personally am not fully happy with this, as it is not fast enough, IMHO. If looping through several PDFs, it may take a few good seconds.

The way I would like this handled is in background, but I need the thumbnails on the next view after "Save" - so this has be handled at this point.

If anyone has a better solution, please comment.

Antho686's avatar

Since you are converting multiple files maybe you could set-up the conversions in parallel using threads ?

Please or to participate in this conversation.