Main Content

Adding a PHP build option, rotating an image based on camera data, and a new look at thumbnails in PHP

Archive - Originally posted on "The Horse's Mouth" - 2015-02-22 06:38:00 - Graham Ellis

A real pleasure to do a bit of image manipulation in PHP, and to install an extra module onto our PHP web server. Not common tasks, but ones we have to do occasionally.

Summaries ...

Installing the exif module onto my existing PHP. Note that each build updates config.nice so that adding an option is simply a question of running that script with the extra option selected, then doing a make:

  cd php-5.{version}
  ./config.nice --enable-exif
  make


The (as the administrator) do the install. As this was a live (running) server, but one on which a few seconds of downtime in the middle of the night wouldn't be an issue, I simply stopped httpd, installed the new PHP, and restarted. And, yes, I tested it too ;-)

  su -
  cd /home/graham/packages/php-5.{version}
  /etc/init.d/apachectl stop
  make install
  /etc/init.d/apachectl start
  exit


Purpose of the upgrade - to allow my PHP scripts to examine image file headers to see if they needed rotation. I've posted an updated thumbnail generator that does this - the main code is just a few lines

  $w_required = 300;
  $h_required = $w_required * 3 / 4;
  $image = imagecreatefromjpeg($_GET[img]);
  $image = correctImageOrientation($_GET[img],$image);
  $res = correctImageSize($image,$w_required,$h_required);
  header("content-type: image/jpeg");
  imagejpeg ($res);


with all the work being done in the correctImageSize and correctImageOrientation functions (both of which I have included in the source and both of which are commented to help guide the newcomer (we're a training company, after all!)

A separate PHP script has been used to produce my thumbnail library - simply looping through all the images in a folder - the PHP code in there is just:

  $img_table = "";
  $dh = opendir(".");
  while (false !== ($entry = readdir($dh))) {
    if (! preg_match('/(jpg|JPG)$/',$entry)) continue;
    $img_table .= "<a href=$entry target=fullsize><img src=thumbnew.php?img=$entry border=0 /></a>  ";
  }


Complete source [here]; try it out on the rail partnership web site [here].