Skip to content

Commit 225b45f

Browse files
author
rvelices
committed
feature 2548 multisize - added a page to build missing derivatives
- 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
1 parent 028729f commit 225b45f

11 files changed

Lines changed: 446 additions & 54 deletions

File tree

admin/derivatives_build.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
// +-----------------------------------------------------------------------+
3+
// | Piwigo - a PHP based photo gallery |
4+
// +-----------------------------------------------------------------------+
5+
// | Copyright(C) 2008-2011 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+
defined('PHPWG_ROOT_PATH') or trigger_error('Hacking attempt!', E_USER_ERROR);
25+
26+
$template->assign('derivatives', array_keys(ImageStdParams::get_defined_type_map()));
27+
28+
$template->set_filename('derivatives', 'derivatives_build.tpl');
29+
$template->assign_var_from_handle('ADMIN_CONTENT', 'derivatives');
30+
?>

admin/include/functions.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,8 @@ function delete_element_files($ids)
200200
$files = array();
201201
$files[] = get_element_path($row);
202202

203-
204203
if (!empty($row['representative_ext']))
205204
{
206-
$pi = pathinfo($row['path']);
207-
$file_wo_ext = get_filename_wo_extension($pi['basename']);
208205
$files[] = original_to_representative( $files[0], $row['representative_ext']);
209206
}
210207

admin/include/image.class.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,7 @@ function compose($overlay, $x, $y, $opacity)
704704
// Place the source image in the destination image
705705
imagecopy($cut, $ioverlay, 0, 0, 0, 0, $ow, $oh);
706706
imagecopymerge($this->image, $cut, $x, $y, 0, 0, $ow, $oh, $opacity);
707+
imagedestroy($cut);
707708
return true;
708709
}
709710

admin/themes/default/template/derivatives.tpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
</style>
2323
{/literal}{/html_head}
2424

25+
<p><a href="admin.php?page=derivatives_build">Build missing derivatives</a></p>
2526
<form method="post" id="derviativesForm">
2627
<fieldset>
2728
<legend>{'Watermark'|@translate}</legend>
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
{html_head}{literal}
2+
<style type="text/css">
3+
TABLE {
4+
font-size: larger;
5+
}
6+
</style>
7+
{/literal}{/html_head}
8+
9+
<p>
10+
<select id="types" name="types[]" multiple="multiple">
11+
{foreach from=$derivatives item=type}
12+
<option value="{$type}" selected="selected">{$type|@translate}</option>
13+
{/foreach}
14+
</select>
15+
<input id="startLink" value="{'Start'|@translate}" onclick="start()" type="button">
16+
<input id="pauseLink" value="{'Pause'|@translate}" onclick="pause()" type="button" disabled="disbled">
17+
<input id="stopLink" value="{'Stop'|@translate}" onclick="stop()" type="button" disabled="disbled">
18+
</p>
19+
<hr/>
20+
<p>
21+
<table>
22+
<tr>
23+
<td>Errors</td>
24+
<td id="errors">0</td>
25+
</tr>
26+
<tr>
27+
<td>Loaded</td>
28+
<td id="loaded">0</td>
29+
</tr>
30+
<tr>
31+
<td>Remaining</td>
32+
<td id="remaining">0</td>
33+
</tr>
34+
</table>
35+
<div id="feedbackWrap" style="height:320px; min-height:320px;">
36+
<img id="feedbackImg">
37+
</div>
38+
</p>
39+
40+
<div id="errorList">
41+
</div>
42+
43+
{combine_script id='iloader' load='footer' path='themes/default/js/image.loader.js'}
44+
45+
{footer_script require='jquery.effects.slide'}{literal}
46+
47+
var loader = new ImageLoader( {onChanged: loaderChanged} )
48+
, pending_next_page = null
49+
, last_image_show_time = 0
50+
, allDoneDfd, urlDfd;
51+
52+
function start() {
53+
allDoneDfd = jQuery.Deferred();
54+
urlDfd = jQuery.Deferred();
55+
56+
allDoneDfd.always( function() {
57+
jQuery("#startLink").attr('disabled', false).css("opacity", 1);
58+
jQuery("#pauseLink,#stopLink").attr('disabled', true).css("opacity", 0.5);
59+
} );
60+
61+
urlDfd.always( function() {
62+
if (loader.remaining()==0)
63+
allDoneDfd.resolve();
64+
} );
65+
66+
jQuery("#startLink").attr('disabled', true).css("opacity", 0.5);
67+
jQuery("#pauseLink,#stopLink").attr('disabled', false).css("opacity", 1);
68+
69+
loader.pause(false);
70+
updateStats();
71+
getUrls();
72+
}
73+
74+
function pause() {
75+
loader.pause( !loader.pause() );
76+
}
77+
78+
function stop() {
79+
loader.clear();
80+
urlDfd.resolve();
81+
}
82+
83+
function getUrls(page_token) {
84+
data = {max_urls: 500, types: []};
85+
jQuery.each(jQuery("#types").serializeArray(), function(i, t) {
86+
data.types.push( t.value );
87+
} );
88+
89+
if (page_token)
90+
data['prev_page'] = page_token;
91+
jQuery.post( '{/literal}{$ROOT_URL}{literal}ws.php?format=json&method=pwg.getMissingDerivatives',
92+
data, wsData, "json").fail( wsError );
93+
}
94+
95+
function wsData(data) {
96+
if (!data.stat || data.stat != "ok") {
97+
wsError();
98+
return;
99+
}
100+
loader.add( data.result.urls );
101+
if (data.result.next_page) {
102+
if (loader.pause() || loader.remaining() > 100) {
103+
pending_next_page = data.result.next_page;
104+
}
105+
else {
106+
getUrls(data.result.next_page);
107+
}
108+
}
109+
}
110+
111+
function wsError() {
112+
urlDfd.reject();
113+
}
114+
115+
function updateStats() {
116+
jQuery("#loaded").text( loader.loaded );
117+
jQuery("#errors").text( loader.errors );
118+
jQuery("#remaining").text( loader.remaining() );
119+
}
120+
121+
function loaderChanged(type, img) {
122+
updateStats();
123+
if (img) {
124+
if (type==="load") {
125+
var now = jQuery.now();
126+
if (now - last_image_show_time > 3000) {
127+
last_image_show_time = now;
128+
var h=img.height, url=img.src;
129+
jQuery("#feedbackWrap").hide("slide", {direction:'down'}, function() {
130+
last_image_show_time = jQuery.now();
131+
if (h > 300 )
132+
jQuery("#feedbackImg").attr("height", 300);
133+
else
134+
jQuery("#feedbackImg").removeAttr("height");
135+
jQuery("#feedbackImg").attr("src", url);
136+
jQuery("#feedbackWrap").show("slide", {direction:'up'} );
137+
} );
138+
}
139+
}
140+
else {
141+
jQuery("#errorList").prepend( '<a href="'+img.src+'">'+img.src+'</a>' + "<br>");
142+
}
143+
}
144+
if (pending_next_page && 100 > loader.remaining() ) {
145+
getUrls(pending_next_page);
146+
pending_next_page = null;
147+
}
148+
else if (loader.remaining() == 0 && (urlDfd.isResolved() || urlDfd.isRejected())) {
149+
allDoneDfd.resolve();
150+
}
151+
}
152+
{/literal}{/footer_script}

i.php

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ function parse_request()
108108
else
109109
{
110110
$req = $_SERVER["QUERY_STRING"];
111+
if ($pos=strpos($req, '&'))
112+
{
113+
$req = substr($req, 0, $pos);
114+
}
111115
/*foreach (array_keys($_GET) as $keynum => $key)
112116
{
113117
$req = $key;
@@ -167,12 +171,20 @@ function parse_request()
167171
{
168172
try
169173
{
170-
$page['derivative_params'] = DerivativeParams::from_url_tokens($deriv);
174+
$params = $page['derivative_params'] = DerivativeParams::from_url_tokens($deriv);
171175
}
172176
catch (Exception $e)
173177
{
174178
ierror($e->getMessage(), 400);
175179
}
180+
if ($params->sizing->ideal_size[0] < 20 or $params->sizing->ideal_size[1] < 20)
181+
{
182+
ierror('Invalid size', 400);
183+
}
184+
if ($params->sizing->max_crop < 0 or $params->sizing->max_crop > 1)
185+
{
186+
ierror('Invalid crop', 400);
187+
}
176188
}
177189

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

226238
$params = $page['derivative_params'];
227-
if ($params->sizing->ideal_size[0] < 20 or $params->sizing->ideal_size[1] < 20)
228-
{
229-
ierror('Invalid size', 400);
230-
}
231-
if ($params->sizing->max_crop < 0 or $params->sizing->max_crop > 1)
232-
{
233-
ierror('Invalid crop', 400);
234-
}
235239

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

251255
$expires=false;
252256
$now = time();
253-
if ( $now > (max($src_mtime, $params->last_mod_time) + 24*3600) )
257+
if ( isset($_GET['b']) )
258+
{
259+
$expires = $now + 100;
260+
header("Cache-control: no-store, max-age=100");
261+
}
262+
elseif ( $now > (max($src_mtime, $params->last_mod_time) + 24*3600) )
254263
{// somehow arbitrary - if derivative params or src didn't change for the last 24 hours, we send an expire header for several days
255264
$expires = $now + 10*24*3600;
256265
}
@@ -267,6 +276,10 @@ function send_derivative($expires)
267276
send_derivative($expires);
268277
}
269278

279+
if (!mkgetdir(dirname($page['derivative_path'])))
280+
{
281+
ierror("dir create error", 500);
282+
}
270283

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

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

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

278-
if (!mkgetdir(dirname($page['derivative_path'])))
279-
{
280-
ierror("dir create error", 500);
281-
}
282-
283291
$changes = 0;
284292

285293
// todo rotate
@@ -325,6 +333,15 @@ function send_derivative($expires)
325333
if ($wm->xrepeat)
326334
{
327335
// todo
336+
$pad = $wm_size[0] + max(30, round($wm_size[0]/4));
337+
for($i=-$wm->xrepeat; $i<=$wm->xrepeat; $i++)
338+
{
339+
if (!$i) continue;
340+
$x2 = $x + $i * $pad;
341+
if ($x2>=0 && $x2+$wm_size[0]<$d_size[0])
342+
if (!$image->compose($wm_image, $x2, $y, $wm->opacity))
343+
break;
344+
}
328345
}
329346
}
330347
$wm_image->destroy();

include/derivative.inc.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,11 @@ private static function build($src, &$params, &$rel_path, &$rel_url, &$flags = n
219219
}
220220
}
221221

222+
function get_path()
223+
{
224+
return PHPWG_ROOT_PATH.$this->rel_path;
225+
}
226+
222227
function get_url()
223228
{
224229
return get_root_url().$this->rel_url;

0 commit comments

Comments
 (0)