feature:2274

pwg.images.resize method is able to create or regenerate image from image path. 

git-svn-id: http://piwigo.org/svn/trunk@10563 68402e56-0260-453c-a942-63ccdbb3a9ee
This commit is contained in:
patdenice 2011-04-22 11:34:57 +00:00
parent 03c9195f7c
commit 10ff3f2f06
3 changed files with 76 additions and 32 deletions

View file

@ -556,6 +556,7 @@ function pwg_image_resize_gd($source_filepath, $destination_filepath, $max_width
return false; return false;
} }
$starttime = get_moment();
$gd_info = gd_info(); $gd_info = gd_info();
// extension of the picture filename // extension of the picture filename
@ -645,11 +646,21 @@ function pwg_image_resize_gd($source_filepath, $destination_filepath, $max_width
imagedestroy($destination_image); imagedestroy($destination_image);
// everything should be OK if we are here! // everything should be OK if we are here!
return true; return array(
'source' => $source_filepath,
'destination' => $destination_filepath,
'width' => $resize_dimensions['width'],
'height' => $resize_dimensions['height'],
'size' => floor(filesize($destination_filepath) / 1024).' KB',
'time' => number_format((get_moment() - $starttime) * 1000, 2, '.', ' ').' ms',
'library' => 'GD',
);
} }
function pwg_image_resize_im($source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata=false, $crop=false, $follow_orientation=true) function pwg_image_resize_im($source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata=false, $crop=false, $follow_orientation=true)
{ {
$starttime = get_moment();
// extension of the picture filename // extension of the picture filename
$extension = strtolower(get_extension($source_filepath)); $extension = strtolower(get_extension($source_filepath));
if (!in_array($extension, array('jpg', 'jpeg', 'png'))) if (!in_array($extension, array('jpg', 'jpeg', 'png')))
@ -705,7 +716,15 @@ function pwg_image_resize_im($source_filepath, $destination_filepath, $max_width
$image->destroy(); $image->destroy();
// everything should be OK if we are here! // everything should be OK if we are here!
return true; return array(
'source' => $source_filepath,
'destination' => $destination_filepath,
'width' => $resize_dimensions['width'],
'height' => $resize_dimensions['height'],
'size' => floor(filesize($destination_filepath) / 1024).' KB',
'time' => number_format((get_moment() - $starttime) * 1000, 2, '.', ' ').' ms',
'library' => 'ImageMagick',
);
} }
function get_rotation_angle($source_filepath) function get_rotation_angle($source_filepath)

View file

@ -2673,6 +2673,11 @@ function ws_images_resize($params, &$service)
return new PwgError(403, 'Unknown type (only "thumbnail" or "websize" are accepted'); return new PwgError(403, 'Unknown type (only "thumbnail" or "websize" are accepted');
} }
if (empty($params['image_id']) and empty($params['image_path']))
{
return new PwgError(403, "image_id or image_path is missing");
}
$resize_params = array('maxwidth', 'maxheight', 'quality', 'crop', 'follow_orientation'); $resize_params = array('maxwidth', 'maxheight', 'quality', 'crop', 'follow_orientation');
$type = $params['type'] == 'thumbnail' ? 'thumb' : 'websize'; $type = $params['type'] == 'thumbnail' ? 'thumb' : 'websize';
foreach ($resize_params as $param) foreach ($resize_params as $param)
@ -2681,32 +2686,50 @@ function ws_images_resize($params, &$service)
$params[$param] = $conf['upload_form_'.$type.'_'.$param]; $params[$param] = $conf['upload_form_'.$type.'_'.$param];
} }
$query=' include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php');
SELECT id, path, tn_ext, has_high
FROM '.IMAGES_TABLE.'
WHERE id = '.(int)$params['image_id'].'
;';
$image = pwg_db_fetch_assoc(pwg_query($query));
if ($image == null)
{
return new PwgError(403, "image_id not found");
}
include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php'); include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
if (!is_valid_image_extension(get_extension($image['path']))) if (!empty($params['image_id']))
{
$query='
SELECT id, path, tn_ext, has_high
FROM '.IMAGES_TABLE.'
WHERE id = '.(int)$params['image_id'].'
;';
$image = pwg_db_fetch_assoc(pwg_query($query));
if ($image == null)
{
return new PwgError(403, "image_id not found");
}
$image_path = $image['path'];
$thumb_path = get_thumbnail_path($image);
$hd_path = get_high_path($image);
}
else
{
$image_path = $params['image_path'];
$thumb_path = file_path_for_type($image_path, 'thumb');
$hd_path = file_path_for_type($image_path, 'high');
}
if (!is_valid_image_extension(get_extension($image_path)))
{ {
return new PwgError(403, "image can't be resized"); return new PwgError(403, "image can't be resized");
} }
if ($params['type'] == 'thumbnail' and !empty($image['tn_ext'])) $result = false;
if ($params['type'] == 'thumbnail' and file_exists($image_path))
{ {
trigger_event( prepare_directory(dirname($thumb_path));
$result = trigger_event(
'upload_thumbnail_resize', 'upload_thumbnail_resize',
false, false,
$image['path'], $image_path,
get_thumbnail_path($image), $thumb_path,
$params['maxwidth'], $params['maxwidth'],
$params['maxheight'], $params['maxheight'],
$params['quality'], $params['quality'],
@ -2714,28 +2737,28 @@ WHERE id = '.(int)$params['image_id'].'
get_boolean($params['crop']), get_boolean($params['crop']),
get_boolean($params['follow_orientation']) get_boolean($params['follow_orientation'])
); );
return true;
} }
elseif (!empty($image['has_high'])) elseif (file_exists($hd_path))
{ {
trigger_event( $result = trigger_event(
'upload_image_resize', 'upload_image_resize',
false, false,
file_path_for_type($image['path'], 'high'), $hd_path,
$image['path'], $image_path,
$params['maxwidth'], $params['maxwidth'],
$params['maxheight'], $params['maxheight'],
$params['quality'], $params['quality'],
false false
); );
$conf['use_exif'] = false; if (!empty($image['has_high']))
$conf['use_iptc'] = false; {
update_metadata(array($image['id'] => $image['path'])); $conf['use_exif'] = false;
$conf['use_iptc'] = false;
return true; update_metadata(array($image['id'] => $image['path']));
}
} }
return false; return $result;
} }
function ws_extensions_update($params, &$service) function ws_extensions_update($params, &$service)

6
ws.php
View file

@ -408,7 +408,8 @@ function ws_addDefaultMethods( $arr )
'pwg.images.resize', 'pwg.images.resize',
'ws_images_resize', 'ws_images_resize',
array( array(
'image_id' => array(), 'image_id' => array('default' => null),
'image_path' => array('default' => null),
'type' => array('default' => 'thumbnail'), 'type' => array('default' => 'thumbnail'),
'maxwidth' => array('default' => null), 'maxwidth' => array('default' => null),
'maxheight' => array('default' => null), 'maxheight' => array('default' => null),
@ -416,7 +417,8 @@ function ws_addDefaultMethods( $arr )
'follow_orientation' => array('default' => null), 'follow_orientation' => array('default' => null),
'quality' => array('default' => null), 'quality' => array('default' => null),
), ),
'Regenerate thumbnails or websize photo with given arguments. 'Create/Regenerate thumbnails or websize photo with given arguments.
<br>One of arguments "image_id" or "image_path" must be passed filled.
<br>Argument "type" can be "thumbnail" or "websize". Default is "thumbnail". <br>Argument "type" can be "thumbnail" or "websize". Default is "thumbnail".
<br>If maxwidth, maxheight, crop, follow_orientation or quality are missing, default parameters of upload will be used.' <br>If maxwidth, maxheight, crop, follow_orientation or quality are missing, default parameters of upload will be used.'
); );