Ignore:
Timestamp:
Jul 24, 2012, 3:38:35 PM (12 years ago)
Author:
plg
Message:

compatibility with Piwigo 2.4

ability to use jpegtran with $confrotate_image_jpegtran = true : lossless photo rotation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/rotateImage/ws_functions.inc.php

    r13524 r16973  
    11<?php
    2 
    32if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
    43
     
    3433  }
    3534
    36   include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
     35  include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
    3736  include_once(PHPWG_ROOT_PATH.'admin/include/image.class.php');
     37
    3838  $image_id=(int)$params['image_id'];
    3939 
    4040  $query='
    41 SELECT id, path, tn_ext, has_high
     41SELECT
     42    id,
     43    path,
     44    representative_ext,
     45    rotation
    4246  FROM '.IMAGES_TABLE.'
    4347  WHERE id = '.$image_id.'
    4448;';
    45   $image = pwg_db_fetch_assoc(pwg_query($query));
    46   if ($image == null)
     49  $row = pwg_db_fetch_assoc(pwg_query($query));
     50  if ($row == null)
    4751  {
    4852    return new PwgError(403, "image_id not found");
    4953  }
    5054
    51   // rotation angle
    52   if ('auto' == $params['angle']) {
    53     $angle = $params['angle'];
     55  $base_angle = pwg_image::get_rotation_angle_from_code($row['rotation']);
     56
     57  if (get_boolean($params['rotate_hd'])) {
     58    if ('auto' == $angle) {
     59      $angle = pwg_image::get_rotation_angle($row['path']);
     60      $rotation_code = 0;
     61    }
     62    else {
     63      // the angle is based on what the user sees (the thumbnail) and not on
     64      // the original, which may have a different angle
     65      $angle = ($base_angle + $params['angle']) % 360;
     66
     67      // the derivatives must not be rotated
     68      $rotation_code = 4;
     69    }
     70
     71    if (isset($conf['rotate_image_jpegtran']) and $conf['rotate_image_jpegtran']) {
     72      $angle = ($angle + 180) % 360;
     73      $command = 'jpegtran -copy all -rotate '.$angle.' -outfile '.$row['path'].' '.$row['path'];
     74      exec($command);
     75    }
     76    else {
     77      $image = new pwg_image($row['path']);
     78      $image->set_compression_quality(98);
     79      $image->rotate($angle);
     80      $image->write($row['path']);
     81    }
     82
     83    $conf['use_exif'] = false;
     84    $conf['use_iptc'] = false;
     85    sync_metadata(array($row['id']));
     86
     87    single_update(
     88      IMAGES_TABLE,
     89      array('rotation' => $rotation_code),
     90      array('id' => $row['id'])
     91    );
    5492  }
    55   else {
    56     $angle = (int)$params['angle'];
     93  elseif ('auto' != $params['angle']) {
     94    $new_angle = ($base_angle + $params['angle']) % 360;
     95    $rotation_code = pwg_image::get_rotation_code_from_angle($new_angle);
     96
     97    // to show that it's a manual rotation, we use 4,5,6,7 instead of 0,1,2,3
     98    $rotation_code+= 4;
     99
     100    single_update(
     101      IMAGES_TABLE,
     102      array('rotation' => $rotation_code),
     103      array('id' => $row['id'])
     104    );
    57105  }
    58106
    59   if (get_boolean($params['rotate_hd']) and get_boolean($image['has_high'])) {
    60     $to_rotate_path = file_path_for_type($image['path'], 'high');
    61     $quality = $conf['upload_form_hd_quality'];
    62     $regenerate_websize = true;
    63 
    64     if ('auto' == $angle) {
    65       $angle = pwg_image::get_rotation_angle($to_rotate_path);
    66     }
    67   }
    68   else {
    69     $to_rotate_path = $image['path'];
    70     $quality = $conf['upload_form_websize_quality'];
    71     $regenerate_websize = false;
    72   }
    73 
    74   $rotated = new pwg_image($to_rotate_path);
    75   $rotated->set_compression_quality($quality);
    76   $rotated->rotate($angle);
    77   $rotated->write($to_rotate_path);
    78 
    79   if ($regenerate_websize) {
    80     ws_images_resizewebsize(
    81       array(
    82         'image_id' => $params['image_id'],
    83         'maxwidth' => $conf['upload_form_websize_maxwidth'],
    84         'maxheight' => $conf['upload_form_websize_maxheight'],
    85         'quality' => $conf['upload_form_websize_quality'],
    86         'automatic_rotation' => $conf['upload_form_automatic_rotation'],
    87         'library' => $conf['graphics_library'],
    88         ),
    89       $service
    90       );
    91   }
    92 
    93   ws_images_resizethumbnail(
    94     array(
    95       'image_id' => $params['image_id'],
    96       'maxwidth' => $conf['upload_form_thumb_maxwidth'],
    97       'maxheight' => $conf['upload_form_thumb_maxheight'],
    98       'quality' => $conf['upload_form_thumb_quality'],
    99       'crop' => $conf['upload_form_thumb_crop'],
    100       'follow_orientation' => $conf['upload_form_thumb_follow_orientation'],
    101       'library' => $conf['graphics_library'],
    102       ),
    103     $service
    104     );
    105 
    106   $conf['use_exif'] = false;
    107   $conf['use_iptc'] = false;
    108   update_metadata(array($image['id'] => $image['path']));
     107  delete_element_derivatives($row);
    109108 
    110109  return true;
Note: See TracChangeset for help on using the changeset viewer.