Skip to content

Commit

Permalink
feature 2548 multisize - added a page to build missing derivatives
Browse files Browse the repository at this point in the history
 - browser driven, chained ws calls to retrieve urls, visual feedback of progress through slideshow

git-svn-id: http://piwigo.org/svn/trunk@12865 68402e56-0260-453c-a942-63ccdbb3a9ee
  • Loading branch information
rvelices committed Jan 8, 2012
1 parent 028729f commit 225b45f
Show file tree
Hide file tree
Showing 11 changed files with 446 additions and 54 deletions.
30 changes: 30 additions & 0 deletions admin/derivatives_build.php
@@ -0,0 +1,30 @@
<?php
// +-----------------------------------------------------------------------+
// | Piwigo - a PHP based photo gallery |
// +-----------------------------------------------------------------------+
// | Copyright(C) 2008-2011 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. |
// +-----------------------------------------------------------------------+

defined('PHPWG_ROOT_PATH') or trigger_error('Hacking attempt!', E_USER_ERROR);

$template->assign('derivatives', array_keys(ImageStdParams::get_defined_type_map()));

$template->set_filename('derivatives', 'derivatives_build.tpl');
$template->assign_var_from_handle('ADMIN_CONTENT', 'derivatives');
?>
3 changes: 0 additions & 3 deletions admin/include/functions.php
Expand Up @@ -200,11 +200,8 @@ function delete_element_files($ids)
$files = array();
$files[] = get_element_path($row);


if (!empty($row['representative_ext']))
{
$pi = pathinfo($row['path']);
$file_wo_ext = get_filename_wo_extension($pi['basename']);
$files[] = original_to_representative( $files[0], $row['representative_ext']);
}

Expand Down
1 change: 1 addition & 0 deletions admin/include/image.class.php
Expand Up @@ -704,6 +704,7 @@ function compose($overlay, $x, $y, $opacity)
// Place the source image in the destination image
imagecopy($cut, $ioverlay, 0, 0, 0, 0, $ow, $oh);
imagecopymerge($this->image, $cut, $x, $y, 0, 0, $ow, $oh, $opacity);
imagedestroy($cut);
return true;
}

Expand Down
1 change: 1 addition & 0 deletions admin/themes/default/template/derivatives.tpl
Expand Up @@ -22,6 +22,7 @@
</style>
{/literal}{/html_head}

<p><a href="admin.php?page=derivatives_build">Build missing derivatives</a></p>
<form method="post" id="derviativesForm">
<fieldset>
<legend>{'Watermark'|@translate}</legend>
Expand Down
152 changes: 152 additions & 0 deletions admin/themes/default/template/derivatives_build.tpl
@@ -0,0 +1,152 @@
{html_head}{literal}
<style type="text/css">
TABLE {
font-size: larger;
}
</style>
{/literal}{/html_head}

<p>
<select id="types" name="types[]" multiple="multiple">
{foreach from=$derivatives item=type}
<option value="{$type}" selected="selected">{$type|@translate}</option>
{/foreach}
</select>
<input id="startLink" value="{'Start'|@translate}" onclick="start()" type="button">
<input id="pauseLink" value="{'Pause'|@translate}" onclick="pause()" type="button" disabled="disbled">
<input id="stopLink" value="{'Stop'|@translate}" onclick="stop()" type="button" disabled="disbled">
</p>
<hr/>
<p>
<table>
<tr>
<td>Errors</td>
<td id="errors">0</td>
</tr>
<tr>
<td>Loaded</td>
<td id="loaded">0</td>
</tr>
<tr>
<td>Remaining</td>
<td id="remaining">0</td>
</tr>
</table>
<div id="feedbackWrap" style="height:320px; min-height:320px;">
<img id="feedbackImg">
</div>
</p>

<div id="errorList">
</div>

{combine_script id='iloader' load='footer' path='themes/default/js/image.loader.js'}

{footer_script require='jquery.effects.slide'}{literal}

var loader = new ImageLoader( {onChanged: loaderChanged} )
, pending_next_page = null
, last_image_show_time = 0
, allDoneDfd, urlDfd;

function start() {
allDoneDfd = jQuery.Deferred();
urlDfd = jQuery.Deferred();
allDoneDfd.always( function() {
jQuery("#startLink").attr('disabled', false).css("opacity", 1);
jQuery("#pauseLink,#stopLink").attr('disabled', true).css("opacity", 0.5);
} );

urlDfd.always( function() {
if (loader.remaining()==0)
allDoneDfd.resolve();
} );

jQuery("#startLink").attr('disabled', true).css("opacity", 0.5);
jQuery("#pauseLink,#stopLink").attr('disabled', false).css("opacity", 1);

loader.pause(false);
updateStats();
getUrls();
}

function pause() {
loader.pause( !loader.pause() );
}

function stop() {
loader.clear();
urlDfd.resolve();
}

function getUrls(page_token) {
data = {max_urls: 500, types: []};
jQuery.each(jQuery("#types").serializeArray(), function(i, t) {
data.types.push( t.value );
} );

if (page_token)
data['prev_page'] = page_token;
jQuery.post( '{/literal}{$ROOT_URL}{literal}ws.php?format=json&method=pwg.getMissingDerivatives',
data, wsData, "json").fail( wsError );
}

function wsData(data) {
if (!data.stat || data.stat != "ok") {
wsError();
return;
}
loader.add( data.result.urls );
if (data.result.next_page) {
if (loader.pause() || loader.remaining() > 100) {
pending_next_page = data.result.next_page;
}
else {
getUrls(data.result.next_page);
}
}
}

function wsError() {
urlDfd.reject();
}

function updateStats() {
jQuery("#loaded").text( loader.loaded );
jQuery("#errors").text( loader.errors );
jQuery("#remaining").text( loader.remaining() );
}

function loaderChanged(type, img) {
updateStats();
if (img) {
if (type==="load") {
var now = jQuery.now();
if (now - last_image_show_time > 3000) {
last_image_show_time = now;
var h=img.height, url=img.src;
jQuery("#feedbackWrap").hide("slide", {direction:'down'}, function() {
last_image_show_time = jQuery.now();
if (h > 300 )
jQuery("#feedbackImg").attr("height", 300);
else
jQuery("#feedbackImg").removeAttr("height");
jQuery("#feedbackImg").attr("src", url);
jQuery("#feedbackWrap").show("slide", {direction:'up'} );
} );
}
}
else {
jQuery("#errorList").prepend( '<a href="'+img.src+'">'+img.src+'</a>' + "<br>");
}
}
if (pending_next_page && 100 > loader.remaining() ) {
getUrls(pending_next_page);
pending_next_page = null;
}
else if (loader.remaining() == 0 && (urlDfd.isResolved() || urlDfd.isRejected())) {
allDoneDfd.resolve();
}
}
{/literal}{/footer_script}
47 changes: 32 additions & 15 deletions i.php
Expand Up @@ -108,6 +108,10 @@ function parse_request()
else
{
$req = $_SERVER["QUERY_STRING"];
if ($pos=strpos($req, '&'))
{
$req = substr($req, 0, $pos);
}
/*foreach (array_keys($_GET) as $keynum => $key)
{
$req = $key;
Expand Down Expand Up @@ -167,12 +171,20 @@ function parse_request()
{
try
{
$page['derivative_params'] = DerivativeParams::from_url_tokens($deriv);
$params = $page['derivative_params'] = DerivativeParams::from_url_tokens($deriv);
}
catch (Exception $e)
{
ierror($e->getMessage(), 400);
}
if ($params->sizing->ideal_size[0] < 20 or $params->sizing->ideal_size[1] < 20)
{
ierror('Invalid size', 400);
}
if ($params->sizing->max_crop < 0 or $params->sizing->max_crop > 1)
{
ierror('Invalid crop', 400);
}
}

if ($req[0]!='g' && $req[0]!='u')
Expand Down Expand Up @@ -224,14 +236,6 @@ function send_derivative($expires)
//var_export($page);

$params = $page['derivative_params'];
if ($params->sizing->ideal_size[0] < 20 or $params->sizing->ideal_size[1] < 20)
{
ierror('Invalid size', 400);
}
if ($params->sizing->max_crop < 0 or $params->sizing->max_crop > 1)
{
ierror('Invalid crop', 400);
}

$src_mtime = @filemtime($page['src_path']);
if ($src_mtime === false)
Expand All @@ -250,7 +254,12 @@ function send_derivative($expires)

$expires=false;
$now = time();
if ( $now > (max($src_mtime, $params->last_mod_time) + 24*3600) )
if ( isset($_GET['b']) )
{
$expires = $now + 100;
header("Cache-control: no-store, max-age=100");
}
elseif ( $now > (max($src_mtime, $params->last_mod_time) + 24*3600) )
{// somehow arbitrary - if derivative params or src didn't change for the last 24 hours, we send an expire header for several days
$expires = $now + 10*24*3600;
}
Expand All @@ -267,6 +276,10 @@ function send_derivative($expires)
send_derivative($expires);
}

if (!mkgetdir(dirname($page['derivative_path'])))
{
ierror("dir create error", 500);
}

include_once(PHPWG_ROOT_PATH . 'admin/include/image.class.php');

Expand All @@ -275,11 +288,6 @@ function send_derivative($expires)

$image = new pwg_image($page['src_path']);

if (!mkgetdir(dirname($page['derivative_path'])))
{
ierror("dir create error", 500);
}

$changes = 0;

// todo rotate
Expand Down Expand Up @@ -325,6 +333,15 @@ function send_derivative($expires)
if ($wm->xrepeat)
{
// todo
$pad = $wm_size[0] + max(30, round($wm_size[0]/4));
for($i=-$wm->xrepeat; $i<=$wm->xrepeat; $i++)
{
if (!$i) continue;
$x2 = $x + $i * $pad;
if ($x2>=0 && $x2+$wm_size[0]<$d_size[0])
if (!$image->compose($wm_image, $x2, $y, $wm->opacity))
break;
}
}
}
$wm_image->destroy();
Expand Down
5 changes: 5 additions & 0 deletions include/derivative.inc.php
Expand Up @@ -219,6 +219,11 @@ private static function build($src, &$params, &$rel_path, &$rel_url, &$flags = n
}
}

function get_path()
{
return PHPWG_ROOT_PATH.$this->rel_path;
}

function get_url()
{
return get_root_url().$this->rel_url;
Expand Down

0 comments on commit 225b45f

Please sign in to comment.