Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
patdenice committed Apr 22, 2011
1 parent 03c9195 commit 10ff3f2
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 28 deletions.
23 changes: 21 additions & 2 deletions admin/include/functions_upload.inc.php
Expand Up @@ -556,6 +556,7 @@ function pwg_image_resize_gd($source_filepath, $destination_filepath, $max_width
return false;
}

$starttime = get_moment();
$gd_info = gd_info();

// extension of the picture filename
Expand Down Expand Up @@ -645,11 +646,21 @@ function pwg_image_resize_gd($source_filepath, $destination_filepath, $max_width
imagedestroy($destination_image);

// 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)
{
$starttime = get_moment();

// extension of the picture filename
$extension = strtolower(get_extension($source_filepath));
if (!in_array($extension, array('jpg', 'jpeg', 'png')))
Expand Down Expand Up @@ -705,7 +716,15 @@ function pwg_image_resize_im($source_filepath, $destination_filepath, $max_width
$image->destroy();

// 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)
Expand Down
71 changes: 47 additions & 24 deletions include/ws_functions.inc.php
Expand Up @@ -2673,6 +2673,11 @@ function ws_images_resize($params, &$service)
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');
$type = $params['type'] == 'thumbnail' ? 'thumb' : 'websize';
foreach ($resize_params as $param)
Expand All @@ -2681,61 +2686,79 @@ function ws_images_resize($params, &$service)
$params[$param] = $conf['upload_form_'.$type.'_'.$param];
}

$query='
include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php');
include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');

if (!empty($params['image_id']))
{
$query='
SELECT id, path, tn_ext, has_high
FROM '.IMAGES_TABLE.'
WHERE id = '.(int)$params['image_id'].'
FROM '.IMAGES_TABLE.'
WHERE id = '.(int)$params['image_id'].'
;';
$image = pwg_db_fetch_assoc(pwg_query($query));
$image = pwg_db_fetch_assoc(pwg_query($query));

if ($image == null)
{
return new PwgError(403, "image_id not found");
}

if ($image == null)
$image_path = $image['path'];
$thumb_path = get_thumbnail_path($image);
$hd_path = get_high_path($image);
}
else
{
return new PwgError(403, "image_id not found");
$image_path = $params['image_path'];
$thumb_path = file_path_for_type($image_path, 'thumb');
$hd_path = file_path_for_type($image_path, 'high');
}

include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');

if (!is_valid_image_extension(get_extension($image['path'])))
if (!is_valid_image_extension(get_extension($image_path)))
{
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',
false,
$image['path'],
get_thumbnail_path($image),
$image_path,
$thumb_path,
$params['maxwidth'],
$params['maxheight'],
$params['quality'],
true,
get_boolean($params['crop']),
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',
false,
file_path_for_type($image['path'], 'high'),
$image['path'],
$hd_path,
$image_path,
$params['maxwidth'],
$params['maxheight'],
$params['quality'],
false
);

$conf['use_exif'] = false;
$conf['use_iptc'] = false;
update_metadata(array($image['id'] => $image['path']));

return true;
if (!empty($image['has_high']))
{
$conf['use_exif'] = false;
$conf['use_iptc'] = false;
update_metadata(array($image['id'] => $image['path']));
}
}
return false;
return $result;
}

function ws_extensions_update($params, &$service)
Expand Down
6 changes: 4 additions & 2 deletions ws.php
Expand Up @@ -408,15 +408,17 @@ function ws_addDefaultMethods( $arr )
'pwg.images.resize',
'ws_images_resize',
array(
'image_id' => array(),
'image_id' => array('default' => null),
'image_path' => array('default' => null),
'type' => array('default' => 'thumbnail'),
'maxwidth' => array('default' => null),
'maxheight' => array('default' => null),
'crop' => array('default' => null),
'follow_orientation' => 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>If maxwidth, maxheight, crop, follow_orientation or quality are missing, default parameters of upload will be used.'
);
Expand Down

0 comments on commit 10ff3f2

Please sign in to comment.