/[drupal]/contributions/modules/imagecache_actions/canvasactions.inc
ViewVC logotype

Contents of /contributions/modules/imagecache_actions/canvasactions.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Wed May 28 11:45:08 2008 UTC (18 months ago) by dman
Branch: MAIN
CVS Tags: DRUPAL-5--2-2, DRUPAL-5--2-3, DRUPAL-5--2-1, DRUPAL-5--2-0, DRUPAL-5--1-0, HEAD
Branch point for: DRUPAL-5--2, DRUPAL-6--1
File MIME type: text/x-php
First commit of the imageapi extensions as per:
http://drupal.org/node/184816
1 <?php
2 // $ID: $
3 /**
4 * Helper functions for the text2canvas action for imagecache
5 *
6 */
7
8 /**
9 * Implementation of imagecache_hook_form()
10 *
11 * Settings for preparing a canvas.
12 *
13 * @param $action array of settings for this action
14 * @return a form definition
15 */
16 function canvasactions_definecanvas_form($action) {
17 $form = array(
18 'RGB' => imagecache_rgb_form($action['RGB']),
19 'help' => array(
20 '#type' => 'markup',
21 '#value' => '<p>'. t('Enter no color value for transparent. This will have the effect of adding clear margins around the image.') .'</p>',
22 ),
23 'under' => array(
24 '#type' => 'checkbox',
25 '#title' => t('Resize canvas under image (possibly cropping)'),
26 '#default_value' => $action['under'],
27 '#description' => t('If not set, this will create a solid flat layer, probably totally obscuring the source image'),
28 ),
29 );
30 $form['info'] = array('#value' => t('Enter values in ONLY ONE of the below options. Either exact or relative. Most values are optional - you can adjust only one dimension as needed. If no useful values are set, the current base image size will be used.'));
31 $form['exact'] = array(
32 '#type' => 'fieldset',
33 '#collapsible' => true,
34 '#title' => 'Exact size',
35 'help' => array(
36 '#type' => 'markup',
37 '#value' => '<p>'. t('Set the canvas to a precise size, possibly cropping the image. Use to start with a known size.'). '</p>',
38 ),
39
40 'width' => array(
41 '#type' => 'textfield',
42 '#title' => t('Width'),
43 '#default_value' => $action['exact']['width'],
44 '#description' => t('Enter a value in pixels or percent'),
45 '#size' => 5,
46 ),
47 'height' => array(
48 '#type' => 'textfield',
49 '#title' => t('Height'),
50 '#default_value' => $action['exact']['height'],
51 '#description' => t('Enter a value in pixels or percent'),
52 '#size' => 5,
53 ),
54 );
55 $form['exact'] = array_merge($form['exact'], canvasactions_pos_form($action['exact']));
56 if(! $action['exact']['width'] && !$action['exact']['height']) {
57 $form['exact']['#collapsed'] = true;
58 }
59
60 $form['relative'] = array(
61 '#type' => 'fieldset',
62 '#collapsible' => true,
63 '#title' => t('Relative size'),
64 'help' => array(
65 '#type' => 'markup',
66 '#value' => '<p>'. t('Set the canvas to a relative size, based on the current image dimensions. Use to add simple borders or expand by a fixed amount. Negative values may crop the image.') .'</p>',
67 ),
68 'leftdiff' => array(
69 '#type' => 'textfield',
70 '#title' => t('left difference'),
71 '#default_value' => $action['relative']['leftdiff'],
72 '#size' => 6,
73 '#description' => t('Enter an offset in pixels.'),
74 ),
75 'rightdiff' => array(
76 '#type' => 'textfield',
77 '#title' => t('right difference'),
78 '#default_value' => $action['relative']['rightdiff'],
79 '#size' => 6,
80 '#description' => t('Enter an offset in pixels.'),
81 ),
82 'topdiff' => array(
83 '#type' => 'textfield',
84 '#title' => t('top difference'),
85 '#default_value' => $action['relative']['topdiff'] ,
86 '#size' => 6,
87 '#description' => t('Enter an offset in pixels.'),
88 ),
89 'bottomdiff' => array(
90 '#type' => 'textfield',
91 '#title' => t('bottom difference'),
92 '#default_value' => $action['relative']['bottomdiff'],
93 '#size' => 6,
94 '#description' => t('Enter an offset in pixels.'),
95 ),
96 );
97 if(! $action['relative']['leftdiff'] && !$action['relative']['rightdiff'] && !$action['relative']['topdiff'] && !$action['relative']['bottomdiff']) {
98 $form['relative']['#collapsed'] = true;
99 }
100
101 $form['#submit'][] = 'canvasactions_definecanvas_form_submit';
102 return $form;
103 }
104
105 /**
106 * Implementation of theme_hook() for imagecache_ui.module
107 */
108 function theme_canvasactions_definecanvas($element) {
109 $action = $element['#value'];
110 if ($action['exact']['width'] || $action['exact']['width']) {
111 $output = $action['exact']['width'] .'x'. $action['exact']['height'];
112 }
113 else {
114 $output = ' left:'. $action['relative']['leftdiff'];
115 $output .= ' right:'. $action['relative']['rightdiff'];
116 $output .= ' top:'. $action['relative']['topdiff'];
117 $output .= ' bottom:'. $action['relative']['bottomdiff'];
118
119 }
120 $output .= theme_canvasactions_rgb($action['RGB']);
121 return $output ;
122 }
123
124 /**
125 * Implementation of hook_image()
126 *
127 * Creates a solid background canvas
128 *
129 * Process the imagecache action on the passed image
130 *
131 * @param $image
132 * array defining an image file, including :
133 *
134 * $image- >source as the filename,
135 *
136 * $image->info array
137 *
138 * $image->res handle on the image object
139 */
140 function canvasactions_definecanvas_image(& $image, $action = array()) {
141
142 // May be given either exact or relative dimensions.
143 if ($action['exact']['width'] || $action['exact']['width']) {
144 // Allows only one dimension to be used if the other is unset.
145 if (! $action['exact']['width']) $action['exact']['width'] = $image->info['width'];
146 if (! $action['exact']['height']) $action['exact']['height'] = $image->info['height'];
147
148 $targetsize['width'] = _imagecache_percent_filter($action['exact']['width'], $image->info['width']);
149 $targetsize['height'] = _imagecache_percent_filter($action['exact']['height'], $image->info['height']);
150
151 $targetsize['left'] = _imagecache_keyword_filter($action['exact']['xpos'], $targetsize['width'], $image->info['width']);
152 $targetsize['top'] = _imagecache_keyword_filter($action['exact']['ypos'], $targetsize['height'], $image->info['height']);
153
154 }
155 else {
156 // calculate relative size
157 $targetsize['width'] = $image->info['width'] + $action['relative']['leftdiff'] + $action['relative']['rightdiff'];
158 $targetsize['height'] = $image->info['height'] + $action['relative']['topdiff'] + $action['relative']['bottomdiff'];
159 $targetsize['left'] = $action['relative']['leftdiff'];
160 $targetsize['top'] = $action['relative']['topdiff'];
161 }
162
163 $newcanvas = imagecreatetruecolor($targetsize['width'], $targetsize['height']);
164 $RGB = $action['RGB'];
165
166 // convert from hex (as it is stored in the UI)
167 if($RGB['HEX'] && $deduced = hex_to_rgb($RGB['HEX'])) {
168 $RGB = array_merge($RGB, $deduced);
169 }
170
171 if ($RGB['red'] || $RGB['green'] || $RGB['blue']) { // one may be zero...
172 $background = imagecolorallocate($newcanvas, $RGB['red'], $RGB['green'], $RGB['blue']);
173 }
174 else {
175 // No color, attempt transparency, assume white
176 $background = imagecolorallocatealpha($newcanvas, 255, 255, 255, 127);
177 imagesavealpha($newcanvas, TRUE);
178 imagealphablending($newcanvas, false);
179 imagesavealpha($image->res, TRUE);
180 }
181 imagefilledrectangle($newcanvas, 0, 0, $targetsize['width'], $targetsize['height'], $background);
182
183 if ($action['under']) {
184 require_once('watermark.inc');
185 $watermark = new watermark();
186 $image->res = $watermark->create_watermark($newcanvas, $image->res, $targetsize['left'], $targetsize['top'], 100);
187 imagesavealpha($image->res, TRUE);
188 }
189 else {
190 $image->res = $newcanvas ;
191 }
192
193 $image->info['width'] = $targetsize['width'];
194 $image->info['height'] = $targetsize['height'];
195 return TRUE;
196 }
197
198 ////////////////////////////////////////////////
199
200 /**
201 * Place a given image under the current canvas
202 *
203 * Implementation of imagecache_hook_form()
204 *
205 * @param $action array of settings for this action
206 * @return a form definition
207 */
208 function canvasactions_canvas2file_form($action) {
209 $form = array(
210 'xpos' => array(
211 '#type' => 'textfield',
212 '#title' => t('X offset'),
213 '#default_value' => $action['xpos'],
214 '#size' => 6,
215 '#description' => t('Enter an offset in pixels or use a keyword: <em>left</em>, <em>center</em>, or <em>right</em>.'),
216 ),
217 'ypos' => array(
218 '#type' => 'textfield',
219 '#title' => t('Y offset'),
220 '#default_value' => $action['ypos'],
221 '#size' => 6,
222 '#description' => t('Enter an offset in pixels or use a keyword: <em>top</em>, <em>center</em>, or <em>bottom</em>.'),
223 ),
224 'alpha' => array(
225 '#type' => 'textfield',
226 '#title' => t('opacity'),
227 '#default_value' => isset($action['alpha']) ? $action['alpha'] : 100,
228 '#size' => 6,
229 '#description' => t('Opacity: 0-100.'),
230 ),
231 'path' => array(
232 '#type' => 'textfield',
233 '#title' => t('file name'),
234 '#default_value' => $action['path'],
235 '#description' => t('File may be in the "files/" folder, or relative to the Drupal siteroot.'),
236 ),
237 );
238 return $form;
239 }
240
241 /**
242 * Implementation of theme_hook() for imagecache_ui.module
243 */
244 function theme_canvasactions_canvas2file($element) {
245 $data = $element['#value'];
246 return 'xpos:'. $data['xpos'] .', ypos:'. $data['ypos'] .' alpha:'. $data['alpha'] .'%' ;
247 }
248
249 /**
250 * Place the source image on the current background
251 *
252 * Implementation of hook_image()
253 *
254 *
255 * @param $image
256 * @param $action
257 */
258 function canvasactions_canvas2file_image(&$image, $action = array()) {
259 // search for full (siteroot) paths, then file dir paths, then relative to the current theme
260 if (file_exists($action['path'])) {
261 $underlay = imageapi_image_open($action['path']);
262 }
263 else if (file_exists(file_create_path($action['path']))) {
264 $underlay = imageapi_image_open(file_create_path($action['path']));
265 }
266 // This func modifies the underlay image by ref, placing the current canvas on it
267 if (imageapi_image_overlay($underlay, $image, $action['xpos'], $action['ypos'], $action['alpha'])) {
268 $image->res = $underlay->res;
269 //$image = $underlay;
270 return TRUE;
271 }
272 }
273
274 ////////////////////////////////////////////////
275
276
277 /**
278 * Place a given image on top of the current canvas
279 *
280 * Implementation of imagecache_hook_form()
281 *
282 * @param $action array of settings for this action
283 * @return a form definition
284 */
285 function canvasactions_file2canvas_form($action) {
286 $form = array(
287 'help' => array(
288 '#type' => 'markup',
289 '#value' => t('Note that this action may require a lot of processing as transparency blends require that every pixel be re-calculated for each image. This can be a server-intensive process and generate a bit of load time.'),
290 ),
291 'xpos' => array(
292 '#type' => 'textfield',
293 '#title' => t('X offset'),
294 '#default_value' => $action['xpos'],
295 '#size' => 6,
296 '#description' => t('Enter an offset in pixels or use a keyword: <em>left</em>, <em>center</em>, or <em>right</em>.'),
297 ),
298 'ypos' => array(
299 '#type' => 'textfield',
300 '#title' => t('Y offset'),
301 '#default_value' => $action['ypos'],
302 '#size' => 6,
303 '#description' => t('Enter an offset in pixels or use a keyword: <em>top</em>, <em>center</em>, or <em>bottom</em>.'),
304 ),
305 'alpha' => array(
306 '#type' => 'textfield',
307 '#title' => t('opacity'),
308 '#default_value' => $action['alpha'],
309 '#size' => 6,
310 '#description' => t('Opacity: 0-100.'),
311 ),
312 'path' => array(
313 '#type' => 'textfield',
314 '#title' => t('file name'),
315 '#default_value' => $action['path'],
316 '#description' => t('File may be in the "files/" folder, or relative to the Drupal siteroot.'),
317 ),
318 );
319 return $form;
320 }
321
322 /**
323 * Implementation of theme_hook() for imagecache_ui.module
324 */
325 function theme_canvasactions_file2canvas($element) {
326 $action = $element['#value'];
327 return '<strong>'. basename($action['path']) . '</strong> x:'. $action['xpos'] .', y:'. $action['ypos'] .' alpha:'. $action['alpha'] .'%' ;
328 }
329
330 /**
331 * Place the source image on the current background
332 *
333 * Implementation of hook_image()
334 *
335 *
336 * @param $image
337 * @param $action
338 */
339 function canvasactions_file2canvas_image(&$image, $action = array()) {
340 // search for full (siteroot) paths, then file dir paths, then relative to the current theme
341 if (file_exists($action['path'])) {
342 $overlay = imageapi_image_open($action['path']);
343 }
344 else if (file_exists(file_create_path($action['path']))) {
345 $overlay = imageapi_image_open(file_create_path($action['path']));
346 }
347 return imageapi_image_overlay($image, $overlay, $action['xpos'], $action['ypos'], $action['alpha']);
348 }
349
350 ///////////////////////////////////////////////////////////////////
351 /**
352 * Place the source image on top of the current canvas
353 *
354 * Implementation of imagecache_hook_form()
355 *
356 *
357 *
358 * @param $action array of settings for this action
359 * @return a form definition
360 */
361 function canvasactions_source2canvas_form($action) {
362 $form = array(
363 'xpos' => array(
364 '#type' => 'textfield',
365 '#title' => t('X offset'),
366 '#default_value' => $action['xpos'],
367 '#size' => 6,
368 '#description' => t('Enter an offset in pixels or use a keyword: <em>left</em>, <em>center</em>, or <em>right</em>.'),
369 ),
370 'ypos' => array(
371 '#type' => 'textfield',
372 '#title' => t('Y offset'),
373 '#default_value' => $action['ypos'],
374 '#size' => 6,
375 '#description' => t('Enter an offset in pixels or use a keyword: <em>top</em>, <em>center</em>, or <em>bottom</em>.'),
376 ),
377 'alpha' => array(
378 '#type' => 'textfield',
379 '#title' => t('opacity'),
380 '#default_value' => $action['alpha'] ? $action['alpha'] : 100,
381 '#size' => 6,
382 '#description' => t('Opacity: 0-100.'),
383 ),
384 );
385 return $form;
386 }
387
388 /**
389 * Implementation of theme_hook() for imagecache_ui.module
390 */
391 function theme_canvasactions_source2canvas($element) {
392 $data = $element['#value'];
393 return 'xpos:'. $data['xpos'] .', ypos:'. $data['ypos'] .' alpha:'. $data['alpha'] .'%' ;
394 }
395
396 /**
397 * Place the source image on the current background
398 *
399 * Implementation of hook_image()
400 *
401 *
402 * @param $image
403 * @param $action
404 */
405 function canvasactions_source2canvas_image(&$image, $action = array()) {
406 $overlay = imageapi_image_open($image->source); // this probably means opening the image twice. c'est la vie
407 return imageapi_image_overlay($image, $overlay, $action['xpos'], $action['ypos'], $action['alpha']);
408 }
409
410 ////////////////////////////////////////////////

  ViewVC Help
Powered by ViewVC 1.1.2