feature 2604: support rotation on derivatives

git-svn-id: http://piwigo.org/svn/trunk@13843 68402e56-0260-453c-a942-63ccdbb3a9ee
This commit is contained in:
plegall 2012-04-01 00:02:36 +00:00
parent 3a76852f0c
commit 2ec7183adb
6 changed files with 123 additions and 9 deletions

View file

@ -269,6 +269,11 @@ SELECT
} }
} }
// we need to save the rotation angle in the database to compute
// width/height of "multisizes"
$rotation_angle = pwg_image::get_rotation_angle($file_path);
$rotation = pwg_image::get_rotation_code_from_angle($rotation_angle);
$file_infos = pwg_image_infos($file_path); $file_infos = pwg_image_infos($file_path);
if (isset($image_id)) if (isset($image_id))
@ -280,6 +285,7 @@ SELECT
'height' => $file_infos['height'], 'height' => $file_infos['height'],
'md5sum' => $md5sum, 'md5sum' => $md5sum,
'added_by' => $user['id'], 'added_by' => $user['id'],
'rotation' => $rotation,
); );
if (isset($level)) if (isset($level))
@ -307,6 +313,7 @@ SELECT
'height' => $file_infos['height'], 'height' => $file_infos['height'],
'md5sum' => $md5sum, 'md5sum' => $md5sum,
'added_by' => $user['id'], 'added_by' => $user['id'],
'rotation' => $rotation,
); );
if (isset($level)) if (isset($level))

View file

@ -238,7 +238,7 @@ class pwg_image
return null; return null;
} }
$rotation = null; $rotation = 0;
$exif = exif_read_data($source_filepath); $exif = exif_read_data($source_filepath);
@ -262,6 +262,28 @@ class pwg_image
return $rotation; return $rotation;
} }
static function get_rotation_code_from_angle($rotation_angle)
{
switch($rotation_angle)
{
case 0: return 0;
case 90: return 1;
case 180: return 2;
case 270: return 3;
}
}
static function get_rotation_angle_from_code($rotation_code)
{
switch($rotation_code)
{
case 0: return 0;
case 1: return 90;
case 2: return 180;
case 3: return 270;
}
}
/** Returns a normalized convolution kernel for sharpening*/ /** Returns a normalized convolution kernel for sharpening*/
static function get_sharpen_matrix($amount) static function get_sharpen_matrix($amount)
{ {
@ -423,11 +445,15 @@ class image_imagick implements imageInterface
function resize($width, $height) function resize($width, $height)
{ {
$this->image->setInterlaceScheme(Imagick::INTERLACE_LINE); $this->image->setInterlaceScheme(Imagick::INTERLACE_LINE);
if ($this->get_width()%2 == 0 && $this->get_height()%2 == 0
&& $this->get_width() > 3*$width) // TODO need to explain this condition
if ($this->get_width()%2 == 0
&& $this->get_height()%2 == 0
&& $this->get_width() > 3*$width)
{ {
$this->image->scaleImage($this->get_width()/2, $this->get_height()/2); $this->image->scaleImage($this->get_width()/2, $this->get_height()/2);
} }
return $this->image->resizeImage($width, $height, Imagick::FILTER_LANCZOS, 0.9); return $this->image->resizeImage($width, $height, Imagick::FILTER_LANCZOS, 0.9);
} }

39
i.php
View file

@ -444,7 +444,17 @@ if (strpos($page['src_location'], '/pwg_representative/')===false
{ {
try try
{ {
$query = 'SELECT coi, width, height FROM '.$prefixeTable.'images WHERE path=\''.$page['src_location'].'\''; $query = '
SELECT
id,
coi,
width,
height,
rotation
FROM '.$prefixeTable.'images
WHERE path=\''.$page['src_location'].'\'
;';
if ( ($row=pwg_db_fetch_assoc(pwg_query($query))) ) if ( ($row=pwg_db_fetch_assoc(pwg_query($query))) )
{ {
if (isset($row['width'])) if (isset($row['width']))
@ -452,6 +462,24 @@ if (strpos($page['src_location'], '/pwg_representative/')===false
$page['original_size'] = array($row['width'],$row['height']); $page['original_size'] = array($row['width'],$row['height']);
} }
$page['coi'] = $row['coi']; $page['coi'] = $row['coi'];
include_once(PHPWG_ROOT_PATH . 'admin/include/image.class.php');
if (empty($row['rotation']))
{
$page['rotation_angle'] = pwg_image::get_rotation_angle($page['src_path']);
single_update(
$prefixeTable.'images',
array('rotation' => pwg_image::get_rotation_code_from_angle($page['rotation_angle'])),
array('id' => $row['id'])
);
}
else
{
$page['rotation_angle'] = pwg_image::get_rotation_angle_from_code($row['rotation']);
}
} }
if (!$row) if (!$row)
{ {
@ -472,8 +500,6 @@ if (!mkgetdir(dirname($page['derivative_path'])))
ierror("dir create error", 500); ierror("dir create error", 500);
} }
include_once(PHPWG_ROOT_PATH . 'admin/include/image.class.php');
ignore_user_abort(true); ignore_user_abort(true);
set_time_limit(0); set_time_limit(0);
@ -482,7 +508,11 @@ $timing['load'] = time_step($step);
$changes = 0; $changes = 0;
// todo rotate // rotate
if (0 != $page['rotation_angle'])
{
$image->rotate($page['rotation_angle']);
}
// Crop & scale // Crop & scale
$o_size = $d_size = array($image->get_width(),$image->get_height()); $o_size = $d_size = array($image->get_width(),$image->get_height());
@ -554,6 +584,7 @@ if ($d_size[0]*$d_size[1] < 256000)
{// strip metadata for small images {// strip metadata for small images
$image->strip(); $image->strip();
} }
$image->set_compression_quality( $params->quality ); $image->set_compression_quality( $params->quality );
$image->write( $page['derivative_path'] ); $image->write( $page['derivative_path'] );
$image->destroy(); $image->destroy();

View file

@ -370,7 +370,7 @@ UPDATE '.$tablename.'
{ {
$separator = $is_first ? '' : ",\n "; $separator = $is_first ? '' : ",\n ";
if (isset($value) and $value != '') if (isset($value) and $value !== '')
{ {
$query.= $separator.$key.' = \''.$value.'\''; $query.= $separator.$key.' = \''.$value.'\'';
} }

View file

@ -55,7 +55,18 @@ final class SrcImage
if (!$this->size && isset($infos['width']) && isset($infos['height'])) if (!$this->size && isset($infos['width']) && isset($infos['height']))
{ {
$this->size = array($infos['width'], $infos['height']); $width = $infos['width'];
$height = $infos['height'];
// 1 or 5 => 90 clockwise
// 3 or 7 => 270 clockwise
if ($infos['rotation'] % 2 != 0)
{
$width = $infos['height'];
$height = $infos['width'];
}
$this->size = array($width, $height);
} }
} }

View file

@ -0,0 +1,39 @@
<?php
// +-----------------------------------------------------------------------+
// | Piwigo - a PHP based photo gallery |
// +-----------------------------------------------------------------------+
// | Copyright(C) 2008-2012 Piwigo Team http://piwigo.org |
// | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net |
// | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick |
// +-----------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or modify |
// | it under the terms of the GNU General Public License as published by |
// | the Free Software Foundation |
// | |
// | This program is distributed in the hope that it will be useful, but |
// | WITHOUT ANY WARRANTY; without even the implied warranty of |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
// | General Public License for more details. |
// | |
// | You should have received a copy of the GNU General Public License |
// | along with this program; if not, write to the Free Software |
// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
// | USA. |
// +-----------------------------------------------------------------------+
if (!defined('PHPWG_ROOT_PATH'))
{
die('Hacking attempt!');
}
$upgrade_description = 'rotation mode (code, not angle) is stored in the database';
$query = 'ALTER TABLE '.IMAGES_TABLE.' ADD COLUMN rotation tinyint DEFAULT NULL';
pwg_query($query);
echo
"\n"
. $upgrade_description
."\n"
;
?>