Skip to content

Commit c0bdb8a

Browse files
author
patdenice
committed
Multisize
Load thumbnails with ajax. git-svn-id: http://piwigo.org/svn/trunk@13444 68402e56-0260-453c-a942-63ccdbb3a9ee
1 parent ce06fe8 commit c0bdb8a

File tree

4 files changed

+60
-4
lines changed

4 files changed

+60
-4
lines changed

Diff for: i.php

+16
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,17 @@ function try_switch_source(DerivativeParams $params, $original_mtime)
319319
function send_derivative($expires)
320320
{
321321
global $page;
322+
323+
if (isset($_GET['ajaxload']) and $_GET['ajaxload'] == 'true')
324+
{
325+
include_once(PHPWG_ROOT_PATH.'include/functions_cookie.inc.php');
326+
include_once(PHPWG_ROOT_PATH.'include/functions_url.inc.php');
327+
328+
$response = new json_response();
329+
$response->url = embellish_url(get_absolute_root_url().$page['derivative_path']);
330+
echo json_encode($response);
331+
return;
332+
}
322333
$fp = fopen($page['derivative_path'], 'rb');
323334

324335
$fstat = fstat($fp);
@@ -343,6 +354,10 @@ function send_derivative($expires)
343354
fclose($fp);
344355
}
345356

357+
class json_response
358+
{
359+
var $url;
360+
}
346361

347362
$page=array();
348363
$begin = $step = microtime(true);
@@ -400,6 +415,7 @@ function send_derivative($expires)
400415
exit;
401416
}
402417
send_derivative($expires);
418+
exit;
403419
}
404420

405421
$page['coi'] = null;

Diff for: include/derivative.inc.php

+11-3
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ final class DerivativeImage
9999
public $src_image;
100100

101101
private $params;
102-
private $rel_path, $rel_url;
102+
private $rel_path, $rel_url, $is_cached;
103103

104104
function __construct($type, $src_image)
105105
{
@@ -113,7 +113,7 @@ function __construct($type, $src_image)
113113
$this->params = $type;
114114
}
115115

116-
self::build($src_image, $this->params, $this->rel_path, $this->rel_url);
116+
self::build($src_image, $this->params, $this->rel_path, $this->rel_url, $this->is_cached);
117117
}
118118

119119
static function thumb_url($infos)
@@ -153,13 +153,14 @@ static function get_all($src_image)
153153
return $ret;
154154
}
155155

156-
private static function build($src, &$params, &$rel_path, &$rel_url)
156+
private static function build($src, &$params, &$rel_path, &$rel_url, &$is_cached=null)
157157
{
158158
if ( $src->has_size() && $params->is_identity( $src->get_size() ) )
159159
{
160160
// todo - what if we have a watermark maybe return a smaller size?
161161
$params = null;
162162
$rel_path = $rel_url = $src->rel_path;
163+
$is_cached = true;
163164
return;
164165
}
165166

@@ -191,10 +192,12 @@ private static function build($src, &$params, &$rel_path, &$rel_url)
191192
$mtime = @filemtime(PHPWG_ROOT_PATH.$rel_path);
192193
if ($mtime===false or $mtime < $params->last_mod_time)
193194
{
195+
$is_cached = false;
194196
$url_style = 2;
195197
}
196198
else
197199
{
200+
$is_cached = true;
198201
$url_style = 1;
199202
}
200203
}
@@ -303,6 +306,11 @@ function get_scaled_size_htm($maxw=9999, $maxh=9999)
303306
return 'width="'.$size[0].'" height="'.$size[1].'"';
304307
}
305308
}
309+
310+
function is_cached()
311+
{
312+
return $this->is_cached;
313+
}
306314
}
307315

308316
?>

Diff for: themes/default/js/thumbnails.loader.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var thumbnails_queue = jQuery.manageAjax.create('queued', {
2+
queue: true,
3+
cacheResponse: false,
4+
maxRequests: 3,
5+
preventDoubleRequests: false
6+
});
7+
8+
function add_thumbnail_to_queue(img, loop) {
9+
thumbnails_queue.add({
10+
type: 'GET',
11+
url: img.data('src'),
12+
data: { ajaxload: 'true' },
13+
dataType: 'json',
14+
success: function(result) {
15+
img.attr('src', result.url);
16+
},
17+
error: function() {
18+
if (loop < 3)
19+
add_thumbnail_to_queue(img, ++loop); // Retry 3 times
20+
}
21+
});
22+
}
23+
24+
jQuery('img').each(function() {
25+
var img = jQuery(this);
26+
if (typeof img.data('src') != 'undefined') {
27+
add_thumbnail_to_queue(img, 0);
28+
}
29+
});

Diff for: themes/default/template/thumbnails.tpl

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{if !empty($thumbnails)}{strip}
2+
{combine_script id='jquery.ajaxmanager' path='themes/default/js/plugins/jquery.ajaxmanager.js' load='async'}
3+
{combine_script id='thumbnails.loader' path='themes/default/js/thumbnails.loader.js' require='jquery.ajaxmanager' load='async'}
24
{*define_derivative name='derivative_params' width=160 height=90 crop=true*}
35
{html_style}
46
{*Set some sizes according to maximum thumbnail width and height*}
@@ -22,11 +24,12 @@
2224
{/if}
2325
{/html_style}
2426
{foreach from=$thumbnails item=thumbnail}
27+
{assign var=derivative value=$pwg->derivative($derivative_params, $thumbnail.src_image)}
2528
<li>
2629
<span class="wrap1">
2730
<span class="wrap2">
2831
<a href="{$thumbnail.URL}">
29-
<img class="thumbnail" src="{$pwg->derivative_url($derivative_params, $thumbnail.src_image)}" alt="{$thumbnail.TN_ALT}" title="{$thumbnail.TN_TITLE}">
32+
<img class="thumbnail" {if !$derivative->is_cached()}data-{/if}src="{$derivative->get_url()}" alt="{$thumbnail.TN_ALT}" title="{$thumbnail.TN_TITLE}">
3033
</a>
3134
</span>
3235
{if $SHOW_THUMBNAIL_CAPTION }

0 commit comments

Comments
 (0)