Skip to content

Commit e64ab97

Browse files
author
rvelices
committed
feature 2548 multisize - sharpen + watermarks (partially implemented / no test with imagick extension)
git-svn-id: http://piwigo.org/svn/trunk@12851 68402e56-0260-453c-a942-63ccdbb3a9ee
1 parent 19b58d4 commit e64ab97

8 files changed

Lines changed: 340 additions & 13 deletions

File tree

admin/derivatives.php

Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
if ( isset($_POST['d']) )
2929
{
3030
$pderivatives = $_POST['d'];
31+
$pwatermark = $_POST['w'];
3132

3233
// step 1 - sanitize HTML input
3334
foreach($pderivatives as $type => &$pderivative)
@@ -86,10 +87,55 @@
8687
$prev_w = intval($pderivative['w']);
8788
$prev_h = intval($pderivative['h']);
8889
}
90+
91+
$v = intval($pderivative['sharpen']);
92+
if ($v<0 || $v>100)
93+
{
94+
$errors[$type]['sharpen'] = '[0..100]';
95+
}
96+
$v = intval($pderivative['quality']);
97+
if ($v<=0 || $v>100)
98+
{
99+
$errors[$type]['quality'] = '(0..100]';
100+
}
101+
}
102+
$v = intval($pwatermark['xpos']);
103+
if ($v<0 || $v>100)
104+
{
105+
$errors['watermark']['xpos'] = '[0..100]';
89106
}
107+
$v = intval($pwatermark['ypos']);
108+
if ($v<0 || $v>100)
109+
{
110+
$errors['watermark']['ypos'] = '[0..100]';
111+
}
112+
$v = intval($pwatermark['opacity']);
113+
if ($v<=0 || $v>100)
114+
{
115+
$errors['watermark']['opacity'] = '(0..100]';
116+
}
117+
118+
90119
// step 3 - save data
91120
if (count($errors)==0)
92121
{
122+
$watermark = new WatermarkParams();
123+
$watermark->file = $pwatermark['file'];
124+
$watermark->xpos = intval($pwatermark['xpos']);
125+
$watermark->ypos = intval($pwatermark['ypos']);
126+
$watermark->xrepeat = intval($pwatermark['xrepeat']);
127+
$watermark->opacity = intval($pwatermark['opacity']);
128+
$watermark->min_size = array(intval($pwatermark['minw']),intval($pwatermark['minh']));
129+
130+
$old_watermark = ImageStdParams::get_watermark();
131+
$watermark_changed =
132+
$watermark->file != $old_watermark->file
133+
|| $watermark->xpos != $old_watermark->xpos
134+
|| $watermark->ypos != $old_watermark->ypos
135+
|| $watermark->xrepeat != $old_watermark->xrepeat
136+
|| $watermark->opacity != $old_watermark->opacity;
137+
ImageStdParams::set_watermark($watermark);
138+
93139
$enabled = ImageStdParams::get_defined_type_map();
94140
$disabled = @unserialize( @$conf['disabled_derivatives'] );
95141
if ($disabled===false)
@@ -106,11 +152,15 @@
106152
{
107153
$new_params = new DerivativeParams(
108154
new SizingParams(
109-
array($pderivative['w'],$pderivative['h']),
155+
array(intval($pderivative['w']), intval($pderivative['h'])),
110156
round($pderivative['crop'] / 100, 2),
111-
array($pderivative['minw'],$pderivative['minh'])
157+
array(intval($pderivative['minw']), intval($pderivative['minh']))
112158
)
113159
);
160+
$new_params->sharpen = intval($pderivative['sharpen']);
161+
$new_params->quality = intval($pderivative['quality']);
162+
ImageStdParams::apply_global($new_params);
163+
114164
if (isset($enabled[$type]))
115165
{
116166
$old_params = $enabled[$type];
@@ -126,6 +176,22 @@
126176
{
127177
$same = false;
128178
}
179+
180+
if ( $same &&
181+
( $new_params->sharpen != $old_params->sharpen
182+
|| $new_params->quality > $old_params->quality)
183+
)
184+
{
185+
$same = false;
186+
}
187+
188+
if ($same &&
189+
( $new_params->use_watermark != $old_params->use_watermark
190+
|| $new_params->use_watermark && $watermark_changed )
191+
)
192+
{
193+
$same = false;
194+
}
129195

130196
if (!$same)
131197
{
@@ -182,6 +248,7 @@
182248
else
183249
{
184250
$template->assign('derivatives', $pderivatives);
251+
$template->assign('watermark', $pwatermark);
185252
$template->assign('ferrors', $errors);
186253
}
187254
}
@@ -224,11 +291,37 @@
224291
{
225292
$tpl_var['minw'] = $tpl_var['minh'] = "";
226293
}
294+
$tpl_var['sharpen'] = $params->sharpen;
295+
$tpl_var['quality'] = $params->quality;
227296
}
228297
$tpl_vars[$type]=$tpl_var;
229298
}
230299
$template->assign('derivatives', $tpl_vars);
300+
301+
$wm = ImageStdParams::get_watermark();
302+
$template->assign('watermark', array(
303+
'file' => $wm->file,
304+
'minw' => $wm->min_size[0],
305+
'minh' => $wm->min_size[1],
306+
'xpos' => $wm->xpos,
307+
'ypos' => $wm->ypos,
308+
'xrepeat' => $wm->xrepeat,
309+
'opacity' => $wm->opacity,
310+
));
311+
}
312+
313+
$watermark_files = array();
314+
foreach (glob(PHPWG_ROOT_PATH.'themes/default/watermarks/*.png') as $file)
315+
{
316+
$watermark_files[] = substr($file, strlen(PHPWG_ROOT_PATH));
317+
}
318+
$watermark_filemap = array( '' => '---' );
319+
foreach( $watermark_files as $file)
320+
{
321+
$display = basename($file);
322+
$watermark_filemap[$file] = $display;
231323
}
324+
$template->assign('watermark_files', $watermark_filemap);
232325

233326
$template->set_filename('derivatives', 'derivatives.tpl');
234327
$template->assign_var_from_handle('ADMIN_CONTENT', 'derivatives');

admin/include/image.class.php

Lines changed: 100 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ function strip();
4141
function rotate($rotation);
4242

4343
function resize($width, $height);
44+
45+
function sharpen($amount);
46+
47+
function compose($overlay, $x, $y, $opacity);
4448

4549
function write($destination_filepath);
4650
}
@@ -258,6 +262,31 @@ static function get_rotation_angle($source_filepath)
258262
return $rotation;
259263
}
260264

265+
/** Returns a normalized convolution kernel for sharpening*/
266+
static function get_sharpen_matrix($amount)
267+
{
268+
// Amount should be in the range of 18-10
269+
$amount = round(abs(-18 + ($amount * 0.08)), 2);
270+
271+
$matrix = array
272+
(
273+
array(-1, -1, -1),
274+
array(-1, $amount, -1),
275+
array(-1, -1, -1),
276+
);
277+
278+
$norm = array_sum(array_map('array_sum', $matrix));
279+
280+
for ($i=0; $i<3; $i++)
281+
{
282+
$line = & $matrix[$i];
283+
for ($j=0; $j<3; $j++)
284+
$line[$j] /= $norm;
285+
}
286+
287+
return $matrix;
288+
}
289+
261290
private function get_resize_result($destination_filepath, $width, $height, $time=null)
262291
{
263292
return array(
@@ -397,6 +426,18 @@ function resize($width, $height)
397426
return $this->image->resizeImage($width, $height, Imagick::FILTER_LANCZOS, 0.9);
398427
}
399428

429+
function sharpen($amount)
430+
{
431+
$m = pwg_image::get_sharpen_matrix($amount);
432+
return $this->image->convolveImage($m);
433+
}
434+
435+
function compose($overlay, $x, $y, $opacity)
436+
{
437+
// todo
438+
return false;
439+
}
440+
400441
function write($destination_filepath)
401442
{
402443
return $this->image->writeImage($destination_filepath);
@@ -415,16 +456,17 @@ class image_ext_imagick implements imageInterface
415456
var $height = '';
416457
var $commands = array();
417458

418-
function __construct($source_filepath, $imagickdir='')
459+
function __construct($source_filepath)
419460
{
461+
global $conf;
420462
$this->source_filepath = $source_filepath;
421-
$this->imagickdir = $imagickdir;
463+
$this->imagickdir = $conf['ext_imagick_dir'];
422464

423-
$command = $imagickdir.'identify -format "%wx%h" "'.realpath($source_filepath).'"';
465+
$command = $this->imagickdir.'identify -format "%wx%h" "'.realpath($source_filepath).'"';
424466
@exec($command, $returnarray);
425467
if(!is_array($returnarray) or empty($returnarray[0]) or !preg_match('/^(\d+)x(\d+)$/', $returnarray[0], $match))
426468
{
427-
die("[External ImageMagick] Corrupt image");
469+
die("[External ImageMagick] Corrupt image\n" . var_export($returnarray, true));
428470
}
429471

430472
$this->width = $match[1];
@@ -479,6 +521,31 @@ function resize($width, $height)
479521
return true;
480522
}
481523

524+
function sharpen($amount)
525+
{
526+
$m = pwg_image::get_sharpen_matrix($amount);
527+
528+
$param ='convolve "'.count($m).':';
529+
foreach ($m as $line)
530+
{
531+
$param .= ' ';
532+
$param .= implode(',', $line);
533+
}
534+
$param .= '"';
535+
$this->add_command('morphology', $param);
536+
return true;
537+
}
538+
539+
function compose($overlay, $x, $y, $opacity)
540+
{
541+
$param = 'compose dissolve -define compose:args='.$opacity;
542+
$param .= ' '.escapeshellarg(realpath($overlay->image->source_filepath));
543+
$param .= ' -gravity NorthWest -geometry +'.$x.'+'.$y;
544+
$param .= ' -composite';
545+
$this->add_command($param);
546+
return true;
547+
}
548+
482549
function write($destination_filepath)
483550
{
484551
$exec = $this->imagickdir.'convert';
@@ -496,6 +563,8 @@ function write($destination_filepath)
496563
$dest = pathinfo($destination_filepath);
497564
$exec .= ' "'.realpath($dest['dirname']).'/'.$dest['basename'].'"';
498565
@exec($exec, $returnarray);
566+
567+
//echo($exec);
499568
return is_array($returnarray);
500569
}
501570
}
@@ -611,6 +680,33 @@ function resize($width, $height)
611680
return $result;
612681
}
613682

683+
function sharpen($amount)
684+
{
685+
$m = pwg_image::get_sharpen_matrix($amount);
686+
return imageconvolution($this->image, $m, 1, 0);
687+
}
688+
689+
function compose($overlay, $x, $y, $opacity)
690+
{
691+
$ioverlay = $overlay->image->image;
692+
/* A replacement for php's imagecopymerge() function that supports the alpha channel
693+
See php bug #23815: http://bugs.php.net/bug.php?id=23815 */
694+
695+
$ow = imagesx($ioverlay);
696+
$oh = imagesy($ioverlay);
697+
698+
// Create a new blank image the site of our source image
699+
$cut = imagecreatetruecolor($ow, $oh);
700+
701+
// Copy the blank image into the destination image where the source goes
702+
imagecopy($cut, $this->image, 0, 0, $x, $y, $ow, $oh);
703+
704+
// Place the source image in the destination image
705+
imagecopy($cut, $ioverlay, 0, 0, 0, 0, $ow, $oh);
706+
imagecopymerge($this->image, $cut, $x, $y, 0, 0, $ow, $oh, $opacity);
707+
return true;
708+
}
709+
614710
function write($destination_filepath)
615711
{
616712
$extension = strtolower(get_extension($destination_filepath));

admin/themes/default/template/derivatives.tpl

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,42 @@
2323
{/literal}{/html_head}
2424

2525
<form method="post" id="derviativesForm">
26+
<fieldset>
27+
<legend>{'Watermark'|@translate}</legend>
28+
29+
30+
<select name="w[file]" id="wSelect">
31+
{html_options options=$watermark_files selected=$watermark.file}
32+
</select>
33+
34+
<p><img id="wImg"></img></p>
35+
36+
<label>{'Min Width'|@translate}
37+
<input type="text" name="w[minw]" value="{$watermark.minw}"{if isset($ferrors.watermark.minw)}class="dError"{/if}>
38+
</label>
39+
40+
<label>{'Min Height'|@translate}
41+
<input type="text" name="w[minh]" value="{$watermark.minh}"{if isset($ferrors.watermark.minh)}class="dError"{/if}>
42+
</label>
43+
44+
<label>{'X Position'|@translate}
45+
<input type="text" name="w[xpos]" value="{$watermark.xpos}"{if isset($ferrors.watermark.xpos)}class="dError"{/if}>
46+
%</label>
47+
48+
<label>{'Y Position'|@translate}
49+
<input type="text" name="w[ypos]" value="{$watermark.ypos}"{if isset($ferrors.watermark.ypos)}class="dError"{/if}>
50+
%</label>
51+
52+
<label>{'X Repeat'|@translate}
53+
<input type="text" name="w[xrepeat]" value="{$watermark.xrepeat}"{if isset($ferrors.watermark.xrepeat)}class="dError"{/if}>
54+
</label>
55+
56+
<label>{'Opacity'|@translate}
57+
<input type="text" name="w[opacity]" value="{$watermark.opacity}"{if isset($ferrors.watermark.opacity)}class="dError"{/if}>
58+
</label>
59+
60+
</fieldset>
61+
2662
<table class="table2">
2763
<thead>
2864
<tr>
@@ -33,6 +69,8 @@
3369
<td>{'Crop'|@translate} (%)</td>
3470
<td>{'Min Width'|@translate}</td>
3571
<td>{'Min Height'|@translate}</td>
72+
<td>{'Sharpen'|@translate} (%)</td>
73+
<td>{'Quality'|@translate} (%)</td>
3674
</tr>
3775
</thead>
3876
{foreach from=$derivatives item=d key=type}
@@ -65,7 +103,14 @@
65103
<input type="text" name="d[{$type}][minh]" value="{$d.minh}"{if isset($ferrors.$type.minh)}class="dError"{/if}>
66104
{if isset($ferrors.$type.minh)}<span class="dErrorDesc" title="{$ferrors.$type.minh}">!</span>{/if}
67105
{/if}</td>
68-
106+
<td>
107+
<input type="text" name="d[{$type}][sharpen]" value="{$d.sharpen}"{if isset($ferrors.$type.sharpen)}class="dError"{/if}>
108+
{if isset($ferrors.$type.sharpen)}<span class="dErrorDesc" title="{$ferrors.$type.sharpen}">!</span>{/if}
109+
</td>
110+
<td>
111+
<input type="text" name="d[{$type}][quality]" value="{$d.quality}"{if isset($ferrors.$type.quality)}class="dError"{/if}>
112+
{if isset($ferrors.$type.quality)}<span class="dErrorDesc" title="{$ferrors.$type.quality}">!</span>{/if}
113+
</td>
69114
</tr>
70115
{/foreach}
71116
</table>
@@ -76,4 +121,18 @@
76121
jQuery(".dError").bind("focus", function () {
77122
jQuery(this).removeClass("dError");
78123
} );
124+
125+
function onWatermarkChange()
126+
{
127+
var val = jQuery("#wSelect").val();
128+
if (val.length) {
129+
jQuery("#wImg").attr('src', {/literal}'{$ROOT_URL}'{literal}+val).show();
130+
}
131+
else {
132+
jQuery("#wImg").hide();
133+
}
134+
}
135+
136+
onWatermarkChange();
137+
jQuery("#wSelect").bind("change", onWatermarkChange );
79138
{/literal}{/footer_script}

0 commit comments

Comments
 (0)