Skip to content

Commit 2ec7183

Browse files
committed
feature 2604: support rotation on derivatives
git-svn-id: http://piwigo.org/svn/trunk@13843 68402e56-0260-453c-a942-63ccdbb3a9ee
1 parent 3a76852 commit 2ec7183

6 files changed

Lines changed: 123 additions & 9 deletions

File tree

admin/include/functions_upload.inc.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,11 @@ function add_uploaded_file($source_filepath, $original_filename=null, $categorie
269269
}
270270
}
271271

272+
// we need to save the rotation angle in the database to compute
273+
// width/height of "multisizes"
274+
$rotation_angle = pwg_image::get_rotation_angle($file_path);
275+
$rotation = pwg_image::get_rotation_code_from_angle($rotation_angle);
276+
272277
$file_infos = pwg_image_infos($file_path);
273278

274279
if (isset($image_id))
@@ -280,6 +285,7 @@ function add_uploaded_file($source_filepath, $original_filename=null, $categorie
280285
'height' => $file_infos['height'],
281286
'md5sum' => $md5sum,
282287
'added_by' => $user['id'],
288+
'rotation' => $rotation,
283289
);
284290

285291
if (isset($level))
@@ -307,6 +313,7 @@ function add_uploaded_file($source_filepath, $original_filename=null, $categorie
307313
'height' => $file_infos['height'],
308314
'md5sum' => $md5sum,
309315
'added_by' => $user['id'],
316+
'rotation' => $rotation,
310317
);
311318

312319
if (isset($level))

admin/include/image.class.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ static function get_rotation_angle($source_filepath)
238238
return null;
239239
}
240240

241-
$rotation = null;
241+
$rotation = 0;
242242

243243
$exif = exif_read_data($source_filepath);
244244

@@ -262,6 +262,28 @@ static function get_rotation_angle($source_filepath)
262262
return $rotation;
263263
}
264264

265+
static function get_rotation_code_from_angle($rotation_angle)
266+
{
267+
switch($rotation_angle)
268+
{
269+
case 0: return 0;
270+
case 90: return 1;
271+
case 180: return 2;
272+
case 270: return 3;
273+
}
274+
}
275+
276+
static function get_rotation_angle_from_code($rotation_code)
277+
{
278+
switch($rotation_code)
279+
{
280+
case 0: return 0;
281+
case 1: return 90;
282+
case 2: return 180;
283+
case 3: return 270;
284+
}
285+
}
286+
265287
/** Returns a normalized convolution kernel for sharpening*/
266288
static function get_sharpen_matrix($amount)
267289
{
@@ -423,11 +445,15 @@ function rotate($rotation)
423445
function resize($width, $height)
424446
{
425447
$this->image->setInterlaceScheme(Imagick::INTERLACE_LINE);
426-
if ($this->get_width()%2 == 0 && $this->get_height()%2 == 0
427-
&& $this->get_width() > 3*$width)
448+
449+
// TODO need to explain this condition
450+
if ($this->get_width()%2 == 0
451+
&& $this->get_height()%2 == 0
452+
&& $this->get_width() > 3*$width)
428453
{
429454
$this->image->scaleImage($this->get_width()/2, $this->get_height()/2);
430455
}
456+
431457
return $this->image->resizeImage($width, $height, Imagick::FILTER_LANCZOS, 0.9);
432458
}
433459

i.php

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -444,14 +444,42 @@ class json_response
444444
{
445445
try
446446
{
447-
$query = 'SELECT coi, width, height FROM '.$prefixeTable.'images WHERE path=\''.$page['src_location'].'\'';
447+
$query = '
448+
SELECT
449+
id,
450+
coi,
451+
width,
452+
height,
453+
rotation
454+
FROM '.$prefixeTable.'images
455+
WHERE path=\''.$page['src_location'].'\'
456+
;';
457+
448458
if ( ($row=pwg_db_fetch_assoc(pwg_query($query))) )
449459
{
450460
if (isset($row['width']))
451461
{
452462
$page['original_size'] = array($row['width'],$row['height']);
453463
}
454464
$page['coi'] = $row['coi'];
465+
466+
include_once(PHPWG_ROOT_PATH . 'admin/include/image.class.php');
467+
468+
if (empty($row['rotation']))
469+
{
470+
$page['rotation_angle'] = pwg_image::get_rotation_angle($page['src_path']);
471+
472+
single_update(
473+
$prefixeTable.'images',
474+
array('rotation' => pwg_image::get_rotation_code_from_angle($page['rotation_angle'])),
475+
array('id' => $row['id'])
476+
);
477+
}
478+
else
479+
{
480+
$page['rotation_angle'] = pwg_image::get_rotation_angle_from_code($row['rotation']);
481+
}
482+
455483
}
456484
if (!$row)
457485
{
@@ -472,8 +500,6 @@ class json_response
472500
ierror("dir create error", 500);
473501
}
474502

475-
include_once(PHPWG_ROOT_PATH . 'admin/include/image.class.php');
476-
477503
ignore_user_abort(true);
478504
set_time_limit(0);
479505

@@ -482,7 +508,11 @@ class json_response
482508

483509
$changes = 0;
484510

485-
// todo rotate
511+
// rotate
512+
if (0 != $page['rotation_angle'])
513+
{
514+
$image->rotate($page['rotation_angle']);
515+
}
486516

487517
// Crop & scale
488518
$o_size = $d_size = array($image->get_width(),$image->get_height());
@@ -554,6 +584,7 @@ class json_response
554584
{// strip metadata for small images
555585
$image->strip();
556586
}
587+
557588
$image->set_compression_quality( $params->quality );
558589
$image->write( $page['derivative_path'] );
559590
$image->destroy();

include/dblayer/functions_mysql.inc.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ function single_update($tablename, $set_fields, $where_fields, $flags=0)
370370
{
371371
$separator = $is_first ? '' : ",\n ";
372372

373-
if (isset($value) and $value != '')
373+
if (isset($value) and $value !== '')
374374
{
375375
$query.= $separator.$key.' = \''.$value.'\'';
376376
}

include/derivative.inc.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,18 @@ function __construct($infos)
5555

5656
if (!$this->size && isset($infos['width']) && isset($infos['height']))
5757
{
58-
$this->size = array($infos['width'], $infos['height']);
58+
$width = $infos['width'];
59+
$height = $infos['height'];
60+
61+
// 1 or 5 => 90 clockwise
62+
// 3 or 7 => 270 clockwise
63+
if ($infos['rotation'] % 2 != 0)
64+
{
65+
$width = $infos['height'];
66+
$height = $infos['width'];
67+
}
68+
69+
$this->size = array($width, $height);
5970
}
6071
}
6172

install/db/120-database.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
// +-----------------------------------------------------------------------+
3+
// | Piwigo - a PHP based photo gallery |
4+
// +-----------------------------------------------------------------------+
5+
// | Copyright(C) 2008-2012 Piwigo Team http://piwigo.org |
6+
// | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net |
7+
// | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick |
8+
// +-----------------------------------------------------------------------+
9+
// | This program is free software; you can redistribute it and/or modify |
10+
// | it under the terms of the GNU General Public License as published by |
11+
// | the Free Software Foundation |
12+
// | |
13+
// | This program is distributed in the hope that it will be useful, but |
14+
// | WITHOUT ANY WARRANTY; without even the implied warranty of |
15+
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16+
// | General Public License for more details. |
17+
// | |
18+
// | You should have received a copy of the GNU General Public License |
19+
// | along with this program; if not, write to the Free Software |
20+
// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21+
// | USA. |
22+
// +-----------------------------------------------------------------------+
23+
24+
if (!defined('PHPWG_ROOT_PATH'))
25+
{
26+
die('Hacking attempt!');
27+
}
28+
29+
$upgrade_description = 'rotation mode (code, not angle) is stored in the database';
30+
31+
$query = 'ALTER TABLE '.IMAGES_TABLE.' ADD COLUMN rotation tinyint DEFAULT NULL';
32+
pwg_query($query);
33+
34+
echo
35+
"\n"
36+
. $upgrade_description
37+
."\n"
38+
;
39+
?>

0 commit comments

Comments
 (0)