Command Line ImageMagick PDF and Animated GIF Tutorial

Command Line ImageMagick PDF and Animated GIF Tutorial

Command Line ImageMagick PDF and Animated GIF Tutorial

The recent Command Line ImageMagick JPEG Image Creation Tutorial addressed …

For a while now, on macOS, we’ve been putting up with a scenario with our beloved macOS Paintbrush desktop application version whereby it will not save images in …

  • JPEG format … any more, so we have been saving in …
  • PNG format

… for some time now. Is this a big deal? Well, that is the question? We got a bit of a surprise in aspects of the solution we came up with.

… as a “single image file” converting concept. But then, while we are there …

  1. there are two “container like” image slide presentation formats we like to use, they being animated GIF and PDF … and it just so happens …
  2. ImageMagick command line (for us, gotten to via PHP exec) happens to be very good at converting sets of JPEG images to animated GIF and PDF “container like” presentation formats

But we don’t want to automate any such task on every scenario where we are creating those original PNG files in our macOS Paintbrush desktop application. So we decided that …

  • if filenames contain _to_pdf or _via_pdf within their PNG filenames we are interested in a set of JPEG images to PDF conversion via ImageMagick command line automated process, here
  • if filenames contain _to_gif or _via_gif within their PNG filenames we are interested in a set of JPEG images to animated GIF via ImageMagick command line conversion automated process, here

And do any of the “automation” scheduling arrangements need to change, here? No, it’s the PHP that changes, to suit …

<?php

// paintbrush_png_jpg.php
// August, 2025
// crontab php command line called way to backup Paintbrush created *.png to reduced size *.jpg
$pdfprefix="";
$pdfcommand="";
$gifcommand="";

$it = new GlobIterator(__DIR__ . "/*.png");
$last2mis = 9020;
foreach ( $it as $file ) {
if ((time() - $file->getMTime()) <= $last2mis) {
if (strpos($file, '_to_pdf') !== false || strpos($file, '_via_pdf') !== false) {
if (strpos($file, '_to_pdf') !== false) {
$pdfprefix=explode('_to_pdf', $file)[0];
if (file_exists($pdfprefix . ".Pdf")) {
unlink($pdfprefix . ".Pdf");
}
$pdfcommand=' ; /usr/local/bin/convert -quality 100 "' . $pdfprefix . '*.{jpeg}" ' . $pdfprefix . '.Pdf';
} else if (strpos($file, '_via_pdf') !== false) {
$pdfprefix=explode('_via_pdf', $file)[0];
if (file_exists($pdfprefix . ".Pdf")) {
unlink($pdfprefix . ".Pdf");
}
$pdfcommand=' ; /usr/local/bin/convert -quality 100 "' . $pdfprefix . '*.{jpeg}" ' . $pdfprefix . '.Pdf';
}
}
if (strpos($file, '_to_gif') !== false || strpos($file, '_via_gif') !== false) {
if (strpos($file, '_to_gif') !== false) {
$gifprefix=explode('_to_gif', $file)[0];
if (file_exists($gifprefix . ".Gif")) {
unlink($gifprefix . ".Gif");
}
$gifcommand=' ; /usr/local/bin/convert -delay 400 "' . $gifprefix . '*.{jpeg}" -loop 0 ' . $gifprefix . '.Gif';
} else if (strpos($file, '_via_gif') !== false) {
$gifprefix=explode('_via_gif', $file)[0];
if (file_exists($gifprefix . ".Gif")) {
unlink($gifprefix . ".Gif");
}
$gifcommand=' ; /usr/local/bin/convert -delay 400 "' . $gifprefix . '*.{jpeg}" -loop 0 ' . $gifprefix . '.Gif';
}
}

if (!file_exists(str_replace('.png','.jpg',$file)) && !file_exists(str_replace('.png','.jpeg',$file))) {
exec("cd " . __DIR__ . '; /usr/local/bin/convert -quality 30 ' . $file . ' ' . str_replace('.png','.jpeg',$file) . $pdfcommand . $gifcommand);
} else if (!file_exists(str_replace('.png','.jpg',$file)) && file_exists(str_replace('.png','.jpeg',$file))) {
exec("cd " . __DIR__ . '; /usr/local/bin/convert -quality 30 ' . $file . ' ' . str_replace('.png','.jpg',$file) . $pdfcommand . $gifcommand);
}
}
}

?>

… to make this happen in the changed paintbrush_png_jpg.php second draft, designed for “inhouse use only” but feel free to download and use, if it interests you.

All this begs the question … so, how will we remember this arcane arrangement?


Previous relevant Command Line ImageMagick JPEG Image Creation Tutorial is shown below.

Command Line ImageMagick JPEG Image Creation Tutorial

Command Line ImageMagick JPEG Image Creation Tutorial

For a while now, on macOS, we’ve been putting up with a scenario with our beloved macOS Paintbrush desktop application version whereby it will not save images in …

  • JPEG format … any more, so we have been saving in …
  • PNG format

… for some time now. Is this a big deal? Well, that is the question? We got a bit of a surprise in aspects of the solution we came up with.

With that, pretty obvious to us, first aim of smaller sized output image media files to start producing, we decided to involve …

  • macOS command line …
  • PHP usage ( ending up with paintbrush_png_jpg.php ) … in a …
  • crontab scheduled …

    * * * * * ksh -c "cd /Applications/MAMP/htdocs; /Applications/MAMP/bin/php/php7.4.33/bin/php /Applications/MAMP/htdocs/paintbrush_png_jpg.php"

… procedure, running each minute here on this MacBook Air using a command line command exemplified for a PNG called my_image.png …


convert -quality 30 my_image.png my_image.jpeg

… via this PHP ( where we thank this useful link for great help ) …


<?php
// paintbrush_png_jpg.php
// August, 2025
// crontab php command line called way to backup Paintbrush created *.png to reduced size *.jpg
$it = new GlobIterator(__DIR__ . "/*.png");
$last2mis = 9020;
foreach ( $it as $file ) {
if ((time() - $file->getMTime()) <= $last2mis) {
if (!file_exists(str_replace('.png','.jpg',$file)) && !file_exists(str_replace('.png','.jpeg',$file))) {
exec("cd " . __DIR__ . '; /usr/local/bin/convert -quality 30 ' . $file . ' ' . str_replace('.png','.jpeg',$file));
}
}
}
?>

… which achieves it’s objective, for the most part, because we are always saving these PNG files to MAMP‘s Document Root ( ie. /Applications/MAMP/htdocs for us ).

All good, smaller image JPEG files do result, but the twist for us came when we assembled our first JPEG vs PNG sets …


$ ls -l mamp_p*.*g*
-rw-r--r-- 1 user admin 459400 3 Aug 15:15 mamp_paintbrush_job-15.jpeg
-rw-r--r--@ 1 user admin 1857056 3 Aug 14:39 mamp_paintbrush_job-15.png
-rw-r--r-- 1 user admin 413477 3 Aug 15:14 mamp_paintbrush_job-17.jpeg
-rw-r--r--@ 1 user admin 1816410 3 Aug 14:47 mamp_paintbrush_job-17.png
-rw-r--r--@ 1 user admin 246569 3 Aug 15:08 mamp_paintbrush_job-21.jpeg
-rw-r--r--@ 1 user admin 1959028 3 Aug 15:08 mamp_paintbrush_job-21.png
-rw-r--r-- 1 user admin 264874 3 Aug 15:10 mamp_paintbrush_job-23.jpeg
-rw-r--r--@ 1 user admin 2555260 3 Aug 15:09 mamp_paintbrush_job-23.png
-rw-r--r-- 1 user admin 249117 3 Aug 15:11 mamp_paintbrush_job-25.jpeg
-rw-r--r--@ 1 user admin 2704233 3 Aug 15:11 mamp_paintbrush_job-25.png
-rw-r--r-- 1 user admin 230524 3 Aug 15:16 mamp_paintbrush_job-27.jpeg
-rw-r--r--@ 1 user admin 1962296 3 Aug 15:15 mamp_paintbrush_job-27.png
-rw-r--r-- 1 user admin 280417 3 Aug 15:17 mamp_paintbrush_job-29.jpeg
-rw-r--r--@ 1 user admin 2677561 3 Aug 15:16 mamp_paintbrush_job-29.png
$

… into two animated GIF images ( with PNG based one called paintbrush_crontab_png_to_jpeg.gif ) …


$ ls -l paintbrush_*.gif
-rw-r--r--@ 1 user staff 5100166 3 Aug 19:11 paintbrush_crontab_png_to_jpeg.gif
-rw-r--r--@ 1 user staff 6485830 3 Aug 19:14 paintbrush_png_to_jpeg_crontab.gif
$

… via our inhouse animated GIF creator helper web application.

Huh?! Read a bit more about it here!

If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.

This entry was posted in Animation, eLearning, Operating System, Photography, Tutorials and tagged , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *