Ignore:
Timestamp:
Dec 21, 2010, 10:55:04 PM (13 years ago)
Author:
plg
Message:

feature 2076 added: automatically use ImageMagick (imagick) instead of GD if
Imagick extension is available. ImageMagick does not strip EXIF/IPTC metadata
from "web size" version of the photo.

We force remove EXIF/IPTC from the thumbnails to save space and bandwith.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/admin/include/functions_upload.inc.php

    r7868 r8219  
    1919
    2020// add default event handler for image and thumbnail resize
    21 add_event_handler('upload_image_resize', 'pwg_image_resize', EVENT_HANDLER_PRIORITY_NEUTRAL, 6);
    22 add_event_handler('upload_thumbnail_resize', 'pwg_image_resize', EVENT_HANDLER_PRIORITY_NEUTRAL, 6);
     21add_event_handler('upload_image_resize', 'pwg_image_resize', EVENT_HANDLER_PRIORITY_NEUTRAL, 7);
     22add_event_handler('upload_thumbnail_resize', 'pwg_image_resize', EVENT_HANDLER_PRIORITY_NEUTRAL, 7);
    2323
    2424function add_uploaded_file($source_filepath, $original_filename=null, $categories=null, $level=null)
     
    7575    $high_infos = pwg_image_infos($high_path);
    7676   
    77     trigger_event('upload_image_resize',
     77    trigger_event(
     78      'upload_image_resize',
    7879      false,
    7980      $high_path,
     
    8182      $conf['upload_form_websize_maxwidth'],
    8283      $conf['upload_form_websize_maxheight'],
    83       $conf['upload_form_websize_quality']
     84      $conf['upload_form_websize_quality'],
     85      false
    8486      );
    8587  }
     
    9193  prepare_directory($thumb_dir);
    9294 
    93   trigger_event('upload_thumbnail_resize',
     95  trigger_event(
     96    'upload_thumbnail_resize',
    9497    false,
    9598    $file_path,
     
    97100    $conf['upload_form_thumb_maxwidth'],
    98101    $conf['upload_form_thumb_maxheight'],
    99     $conf['upload_form_thumb_quality']
     102    $conf['upload_form_thumb_quality'],
     103    true
    100104    );
    101105 
     
    239243}
    240244
    241 function pwg_image_resize($result, $source_filepath, $destination_filepath, $max_width, $max_height, $quality)
     245function pwg_image_resize($result, $source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata=false)
    242246{
    243247  if ($result !== false)
     
    247251  }
    248252
     253  if (extension_loaded('imagick'))
     254  {
     255    return pwg_image_resize_im($source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata);
     256  }
     257  else
     258  {
     259    return pwg_image_resize_gd($source_filepath, $destination_filepath, $max_width, $max_height, $quality);
     260  }
     261}
     262
     263function pwg_image_resize_gd($source_filepath, $destination_filepath, $max_width, $max_height, $quality)
     264{
    249265  if (!function_exists('gd_info'))
    250266  {
     
    272288  if (function_exists('imagerotate'))
    273289  {
    274     $exif = exif_read_data($source_filepath);
    275     if (isset($exif['Orientation']) and preg_match('/^\s*(\d)/', $exif['Orientation'], $matches))
    276     {
    277       $orientation = $matches[1];
    278       if (in_array($orientation, array(3, 4)))
    279       {
    280         $rotation = 180;
    281       }
    282       elseif (in_array($orientation, array(5, 6)))
    283       {
    284         $rotation = 270;
    285       }
    286       elseif (in_array($orientation, array(7, 8)))
    287       {
    288         $rotation = 90;
    289       }
    290     }
     290    $rotation = get_rotation_angle($source_filepath);
    291291  }
    292292 
     
    342342  // everything should be OK if we are here!
    343343  return true;
     344}
     345
     346function pwg_image_resize_im($source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata=false)
     347{
     348  // extension of the picture filename
     349  $extension = strtolower(get_extension($source_filepath));
     350  if (!in_array($extension, array('jpg', 'jpeg', 'png')))
     351  {
     352    die('[Imagick] unsupported file extension');
     353  }
     354
     355  $image = new Imagick($source_filepath);
     356
     357  $rotation = null;
     358  if (function_exists('imagerotate'))
     359  {
     360    $rotation = get_rotation_angle($source_filepath);
     361  }
     362 
     363  // width/height
     364  $source_width  = $image->getImageWidth();
     365  $source_height = $image->getImageHeight();
     366 
     367  $resize_dimensions = get_resize_dimensions($source_width, $source_height, $max_width, $max_height, $rotation);
     368
     369  // testing on height is useless in theory: if width is unchanged, there
     370  // should be no resize, because width/height ratio is not modified.
     371  if ($resize_dimensions['width'] == $source_width and $resize_dimensions['height'] == $source_height)
     372  {
     373    // the image doesn't need any resize! We just copy it to the destination
     374    copy($source_filepath, $destination_filepath);
     375    return true;
     376  }
     377
     378  $image->setImageCompressionQuality($quality);
     379  $image->setInterlaceScheme(Imagick::INTERLACE_LINE);
     380 
     381  if ($strip_metadata)
     382  {
     383    // we save a few kilobytes. For example a thumbnail with metadata
     384    // weights 25KB, without metadata 7KB.
     385    $image->stripImage();
     386  }
     387 
     388  $image->resizeImage($resize_dimensions['width'], $resize_dimensions['height'], Imagick::FILTER_LANCZOS, 0.9);
     389
     390  if (isset($rotation))
     391  {
     392    $image->rotateImage(new ImagickPixel(), -$rotation);
     393    $image->setImageOrientation(Imagick::ORIENTATION_TOPLEFT);
     394  }
     395
     396  $image->writeImage($destination_filepath);
     397  $image->destroy();
     398
     399  // everything should be OK if we are here!
     400  return true;
     401}
     402
     403function get_rotation_angle($source_filepath)
     404{
     405  $rotation = null;
     406 
     407  $exif = exif_read_data($source_filepath);
     408 
     409  if (isset($exif['Orientation']) and preg_match('/^\s*(\d)/', $exif['Orientation'], $matches))
     410  {
     411    $orientation = $matches[1];
     412    if (in_array($orientation, array(3, 4)))
     413    {
     414      $rotation = 180;
     415    }
     416    elseif (in_array($orientation, array(5, 6)))
     417    {
     418      $rotation = 270;
     419    }
     420    elseif (in_array($orientation, array(7, 8)))
     421    {
     422      $rotation = 90;
     423    }
     424  }
     425
     426  return $rotation;
    344427}
    345428
Note: See TracChangeset for help on using the changeset viewer.