Orientation

See also Huge pictures.

This function automatically rotates a picture according to its EXIF orientation tag, while storing the original as a "deleted" hidden file. It uses the "mogrify" utility that is installed on many servers.

function uMiniAutoOrient($fpath,$mpath,$W,$H,$T,$idx) {
  global $Now;

  $exif = exif_read_data($fpath);
  if ($exif && $exif['Orientation'] != 1) {
    @copy($fpath, "$fpath,$Now");
    exec("mogrify -auto-orient -quality 90 \"$fpath\"");
    clearstatcache();
    list($W, $H) = @getimagesize($fpath);
  }

  return MiniCreate($fpath,$mpath,$W,$H,$T,$idx);
}
$Mini['CreateFunction'] = 'uMiniAutoOrient';

See also Cookbook:RotatePicture for an easy way to manually rotate pictures (if they are not caught by the function here).

Sometimes photos from digital cameras show fine on the desktop, but appear rotated left, right or upside down when posted on a website. This is caused by the way a camera saves the picture, see:

  https://www.impulseadventure.com/photo/exif-orientation.html

Almost all browsers on all operating systems ignore the EXIF orientation tag and display the actual pixels -- only Firefox can be instructed to rotate the picture according to the tag (but it does not by default, unless a single image is loaded, not a web page), and iOS Safari always rotates it.

OTOH almost all desktop photo viewers and native mobile gallery apps always rotate the image when they display it, and people often don't suspect that the image file is encoded sideways or upside down.

This function is not compatible with Huge pictures, but could be adapted to do both the resizing and rotation at once.

Fix orientation and scale down large pictures

This is the adaptation for a picture that will be pre-processed with ImageMagick if it is either in the wrong orientation, or if it has one dimension larger than 1200 pixels:

function uMiniAutoOrientResize($fpath,$mpath,$W,$H,$T,$idx) {
  global $Now;
  $maxsize = 1200;

  $exif = exif_read_data($fpath);
  if (($exif && $exif['Orientation'] != 1) || $W>$maxsize || $H>$maxsize) {
    @copy($fpath, "$fpath,$Now");
    exec("mogrify -auto-orient -resize '{$maxsize}x{$maxsize}>' -quality 80 \"$fpath\"");
    clearstatcache();
    list($W, $H) = @getimagesize($fpath);
  }

  return MiniCreate($fpath,$mpath,$W,$H,$T,$idx);
}
$Mini['CreateFunction'] = 'uMiniAutoOrientResize';