/[drupal]/contributions/modules/imagemagick/imagemagick.module
ViewVC logotype

Contents of /contributions/modules/imagemagick/imagemagick.module

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


Revision 1.1 - (show annotations) (download) (as text)
Tue Aug 22 17:08:17 2006 UTC (3 years, 3 months ago) by aravaphani
Branch: MAIN
CVS Tags: HEAD
File MIME type: text/x-php
Removed the include file.

Added the main module file. Module is incomplete (No delete,vie node support). Images creation working
1 <?php
2
3 /*
4 *
5 * Database definition:
6 * @code
7 CREATE TABLE magick_files (
8 imid int(10) unsigned NOT NULL default '0',
9 nid int(10) unsigned NOT NULL default '0',
10 imagename varchar(255) NOT NULL default '',
11 imagepath varchar(255) NOT NULL default '',
12 imagemime varchar(255) NOT NULL default '',
13 imagesize int(10) unsigned,
14 PRIMARY KEY (imid),
15 )
16 * @endcode
17
18
19
20 */
21
22 function magickimage_help(){
23 switch($section){
24 case 'admin/modules#description':
25 return t('Allows you to apply Imagemagick functions on Images.');
26 case 'node/add/magickimage#description':
27 return t('Allows you to apply Imagemagick functions on Images.');
28 break;
29 }
30 }
31
32 function magickimage_node_info(){
33 return array('magickimage'=>array('name'=>t('Imagemagick Images'),'base'=>'magickimage'));
34 }
35
36 function magickimage_access($op,$node){
37 global $user;
38
39 if($op=='create'){
40 return user_access('create magick images');
41 }
42
43 if($op=='update' || $op=='delete'){
44 if($user->uid==$node->uid){
45 return TRUE;
46 }
47 }
48 }
49
50
51 function magickimage_perm(){
52 $perms[]='create magick images';
53 return $perms;
54 }
55
56
57
58 function magickimage_profiles(){
59 $magickprofiles=array();
60 if(empty($magickprofiles)){
61 $magickprofiles=module_invoke_all('magickprofiles');
62 }
63 return $magickprofiles;
64 }
65
66 function magickimage_effects(){
67 $magickeffects=array();
68 if(empty($magickeffects)){
69 $magickeffects=module_invoke_all('magickeffects');
70 }
71 return $magickeffects;
72 }
73
74
75 function magickimage_magickeffects(){
76 $effects=array();
77
78 $effect=new StdClass();
79 $effect->name='flip';
80 $effect->params=array();
81 $effect->conjure=TRUE;
82 $effects[]=$effect;
83
84 $effect=new StdClass();
85 $effect->name='flop';
86 $effect->params=array();
87 $effect->conjure=TRUE;
88 $effects[]=$effect;
89
90
91 $effect=new StdClass();
92 $effect->name='crop';
93 $effect->params=array('width','height');
94 $effect->conjure=TRUE;
95 $effects[]=$effect;
96
97 $effect=new StdClass();
98 $effect->name='blur';
99 $effect->params=array('radius','sigma');
100 $effect->conjure=TRUE;
101 $effects[]=$effect;
102
103 $effect=new StdClass();
104 $effect->name='charcoal';
105 $effect->params=array('radius','sigma');
106 $effect->conjure=TRUE;
107 $effects[]=$effect;
108
109
110 $effect=new StdClass();
111 $effect->name='emboss';
112 $effect->params=array('radius','sigma');
113 $effect->conjure=TRUE;
114 $effects[]=$effect;
115
116 $effect=new StdClass();
117 $effect->name='sharpen';
118 $effect->params=array('radius','sigma');
119 $effect->conjure=TRUE;
120 $effects[]=$effect;
121
122 $effect=new StdClass();
123 $effect->name='swirl';
124 $effect->params=array('degrees');
125 $effect->conjure=TRUE;
126 $effects[]=$effect;
127
128 $effect=new StdClass();
129 $effect->name='spread';
130 $effect->params=array('degrees');
131 $effect->conjure=TRUE;
132 $effects[]=$effect;
133
134 $effect=new StdClass();
135 $effect->name='solarize';
136 $effect->params=array('degrees');
137 $effect->conjure=TRUE;
138 $effects[]=$effect;
139
140
141 $effect=new StdClass();
142 $effect->name='minify';
143 $effect->params=array();
144 $effect->conjure=TRUE;
145 $effects[]=$effect;
146
147 $effect=new StdClass();
148 $effect->name='magnify';
149 $effect->params=array();
150 $effect->conjure=TRUE;
151 $effects[]=$effect;
152
153
154 $effect=new StdClass();
155 $effect->name='normalize';
156 $effect->params=array();
157 $effect->conjure=TRUE;
158 $effects[]=$effect;
159
160 $effect=new StdClass();
161 $effect->name='trim';
162 $effect->params=array();
163 $effect->conjure=TRUE;
164 $effects[]=$effect;
165
166 $effect=new StdClass();
167 $effect->name='transparent';
168 $effect->params=array('color');
169 $effect->conjure=TRUE;
170 $effects[]=$effect;
171
172 $effect=new StdClass();
173 $efefct->name='raise';
174 $effect->params=array('value');
175 $effect->conjure=FALSE;
176 $efefcts[]=$effect;
177
178
179 $effect=new StdClass();
180 $efefct->name='thumbnail';
181 $effect->params=array('geomtery');
182 $effect->conjure=FALSE;
183 $efefcts[]=$effect;
184
185 $effect=new StdClass();
186 $effect->name='resize';
187 $effect->params=array('geometry');
188 $effect->conjure=FALSE;
189 $effects[]=$effect;
190
191
192 return $effects;
193
194 }
195
196 function magickimage_magickprofiles(){
197 $profiles=array();
198
199 $profile=new StdClass();
200 $profile->name='thumbnail';
201 $profile->filters=array();
202 $profile->filters[]=array('effect'=>'thumbnail','params'=>array('geometry'=>'100x100'));
203 $profiles[]=$profile;
204
205
206 $profile=new StdClass();
207 $profile->name='preview';
208 $profile->filters=array();
209 $profile->filters[]=array('effect'=>'resize','params'=>array('geometry'=>'400x400\>'));
210 $profiles[]=$profile;
211
212 $profile=new StdClass();
213 $profile->name='thumbnailflip';
214 $profile->filters=array();
215 $profile->filters[]=array('effect'=>'resize','params'=>array('width'=>100,'height'=>100));
216 $profile->filters[]=array('effect'=>'flip');
217 $profiles[]=$profile;
218
219 $profile=new StdClass();
220 $profile->name='resize200raise';
221 $profile->filters=array();
222 $profile->filters[]=array('effect'=>'resize','params'=>array('geometry'=>'200x200\>'));
223 $profile->filters[]=array('effect'=>'raise','params'=>array('value'=>'10x10'));
224 $profiles[]=$profile;
225
226 return $profiles;
227
228 }
229
230 function magickimage_menu(){
231 if($may_cache){
232 $items[]=array('path'=>'node/add/magickimage','title'=>t('magick image'),'access'=>user_access('create magick images'));
233 $items[] = array('path' => 'image/view', 'title' => t('image'),
234 'access' => user_access('access content'),
235 'type' => MENU_CALLBACK,
236 'callback' => 'magick_fetch');
237 }
238 return $items;
239 }
240
241
242
243 function magickimage_load(&$node){
244 $result=db_query("SELECT imagename,imagepath from {magick_files} WHERE nid=%d",$node->nid);
245
246 $node->images=array();
247
248 while ($file = db_fetch_object($result)) {
249 // $node->images[$file->filename] = $file->$filepath;
250 }
251
252 }
253
254
255
256 function magickimage_form(&$node,&$params){
257 $form['#attributes'] = array('enctype' => 'multipart/form-data');
258 $form['title']=array('#type'=>'textfield','#title'=>t('Title'),'#required'=>TRUE);
259 $form['file']=array('#type' => 'file', '#title' => t('Image'), '#description' => t('Click "Browse..." to select an image to upload.'), '#weight' => -3);
260
261 $form['images']=array('#type'=>'fieldset','#collapsible'=>TRUE,'#title'=>t('Imagemagick Profiles'));
262 $profiles=magickimage_profiles();
263 foreach($profiles as $profile){
264 $form['images'][$profile->name] = array('#type' => 'checkbox', '#title'=>t($profile->name),'#default_value' => $node->images[$profile->name]);
265
266 }
267
268
269 return $form;
270 }
271
272
273 function magickimage_validate(&$node){
274
275 if ($file = file_check_upload('file')) {
276 $node->file = $file;
277 }
278
279 // Make sure there is an upload or an existing file.
280 if (!$file && !file_exists($node->current_file)) {
281 form_set_error('file', t('A file must be provided.'));
282 }
283 $ret=FALSE;
284 $profiles=magickimage_profiles();
285 foreach($profiles as $profile){
286 $prfname=$profile->name;
287 if($node->$prfname){
288 $ret=TRUE;
289 }
290 }
291
292 if(!$ret){
293 drupal_set_message('Select One of the Profiles');
294 form_set_error('images',t('Select One of the profiles'));
295 }
296 }
297
298
299 function _derive_image($node,$original_image,$label){
300 $file=magickimage_magick($original_image,$label);
301 if(!$file){
302 drupal_set_message('Image Profile not created');
303 return FALSE;
304 }
305 $fid = db_next_id('{magick_files}_imid');
306 db_query("INSERT INTO {magick_files} (imid, nid, imagename, imagepath, imagemime, imagesize) VALUES (%d, %d, '%s', '%s', '%s', '%s')",$fid,$node->nid,$file->filename,$file->filepath,$file->filemime,$file->filesize);
307 return TRUE;
308 }
309
310
311 function magickimage_insert(&$node){
312 if ($file = file_check_upload('file')) {
313 $node->file = file_save_upload($file, file_directory_path(), false);
314 }
315
316 $file=$node->file;
317 $original_image=$file;
318 $fid = db_next_id('{magick_files}_imid');
319
320 db_query("INSERT INTO {magick_files} (imid, nid, imagename, imagepath, imagemime, imagesize) VALUES (%d, %d, '%s', '%s', '%s', '%s')",$fid,$node->nid,'_original',$file->filepath,$file->filemime,$file->filesize);
321
322 $profiles=magickimage_profiles();
323
324 foreach($profiles as $profile){
325 $prfname=$profile->name;
326 if($node->$prfname){
327 if(!_derive_image($node,$original_image,$profile->name)){
328 drupal_set_message($profile->name." not created");
329 }
330 }
331 }
332 }
333 function magickimage_view(&$node, $teaser = 0, $page = 0){
334 $request = ($_GET['image']) ? $_GET['image'] : 'preview';
335 $request = check_plain($request);
336 $node = node_prepare($node, $teaser);
337 # $node->teaser = theme('magickimage_teaser', $node);
338 $node->body = theme('magickimage_body', $node, $request);
339 }
340
341 function theme_magickimage_body($node, $size) {
342 return theme('magickimage_display',$node,$label).$node->body;
343 }
344
345 function theme_magickimage_display($node, $label, $url, $attributes) {
346 return '<img src="'. check_url($url) .'" alt="'. check_plain($node->title) .'" title="'. check_plain($node->title) .'" '. drupal_attributes($attributes) .' />';
347 }
348
349
350
351
352 function magickeffect_load($effectname){
353 $effects=magickimage_effects();
354
355 foreach($effects as $effect){
356 if($effect->name==$effectname){
357 return $effect;
358 }
359 }
360 return FALSE;
361 }
362
363
364 function magickprofile_load($profilename){
365 $profiles=magickimage_profiles();
366 foreach($profiles as $profile){
367 if($profile->name==$profilename){
368 return $profile;
369 }
370 }
371 return FALSE;
372 }
373
374 /* Imagemagick Core . Creating MSL Files. Uding conjure and convert to create imagemagick images */
375
376 function _magickimage_mslfile($effect,$filter){
377
378 $mslfile.="<".$effect->name." ";
379
380 foreach($effect->params as $param){
381 if(!is_null($filter['params'][$param])){
382 $mslfile.=$param.'='.'"'.$filter['params'][$param].'" ';
383 }
384 }
385 $mslfile.='/>';
386 return $mslfile;
387 }
388
389 function _magick_filename($imagepath,$label){
390 $pos = strrpos($imagepath, '.');
391 $filename = substr($imagepath, 0, $pos) .'_'. $label . substr($imagepath, $pos);
392 return $filename;
393 }
394
395
396
397 function magickimage_magick($image,$profilename){
398
399 $profile=magickprofile_load($profilename);
400 if(!$profile){
401 drupal_set_message('No Such profile called '.$profilename);
402 return FALSE;
403 }
404
405 foreach($profile->filters as $filter){
406 $effectname=$filter['effect'];
407 $effectparams=$filter['params'];
408 $effect=magickeffect_load($effectname);
409
410 if($effect->conjure){
411 $mslfile_.=_magickimage_mslfile($effect,$filter);
412
413 }else{
414
415 $filtertxt="-".$filter['effect']." ";
416
417 foreach($filter['params'] as $param){
418 $filtertxt.=$param." " ;
419 }
420 $image=_magickimage_convert($image,$filtertxt);
421 }
422 }
423 $mslfile=start_read_image($image->filepath);
424 $mslfile.=$mslfile_;
425 $mslfile.=close_read_image(_magick_filename($image->filepath,$profilename));
426
427 if(!_magickimage_conjure($mslfile)){
428 drupal_set_message('Something wrong when applying the Imagemagick Filter');
429 return FALSE;
430 }
431 if(file_exists(_magick_filename($image->filepath,$profilename))){
432 $image_new= new StdClass();
433 $image_new->filename=$profilename;
434 $image_new->filepath=_magick_filename($image->filepath,$profilename);
435 $imagedetails=image_get_info(_magick_filename($image->filepath,$profilename));
436 $image_new->filesize=$imagedetails['file_size'];
437 $image_new->filemime=$imagedetails['mime_type'];
438 return $image_new;
439 }
440 else{
441 return FALSE;
442 }
443 }
444
445
446
447
448 function _magickimage_convert($image,$filter){
449 $convert_path=variable_get('convert_binary','/usr/bin/convert');
450 if (!file_exists($convert_path)) {
451 return false;
452 }
453
454 $filter = preg_replace("/[^A-Za-z0-9\.\-\+\040]/", '', $filter);
455 $source = _magickimage_shell($image->filepath);
456 $dest = _magickimage_shell($image->filepath);
457 $err = _magickimage_exec("$convert_path $filter $source $dest");
458
459 if ($err) {
460 return false;
461 }
462 if (!file_exists(trim($dest, "'"))) {
463 return false;
464 }
465
466
467 $image_new=new StdClass();
468 $image_new->filename=$image->filename;
469 $image_new->filepath=$image->filepath;
470 $imagedetails=image_get_info($image->filepath);
471 $image_new->filemime=$imagedetails['mime_type'];
472 $image_new->filesize=$imagedetails['file_size'];
473
474 return $image_new;
475 }
476
477 function _magickimage_shell($filename){
478 if (strstr($_SERVER['SERVER_SOFTWARE'], 'Win32') || strstr($_SERVER['SERVER_SOFTWARE'], 'IIS')) {
479 return '"' . addslashes($filename) . '"';
480 } else {
481 return escapeshellarg($filename);
482 }
483
484 }
485
486 function mslfile($mslcontents){
487 $i=0;
488 $tmp_mslfile='files/tmp/tmp_file_'.$i;
489 while(file_exists($tmp_mslfile)){
490 $tmp_mslfile='files/tmp/tmp_file_'.$i;
491 $i++;
492 }
493 $fh=fopen($tmp_mslfile,'w');
494 fwrite($fh,$mslcontents);
495 fclose($fh);
496 return $tmp_mslfile;
497 }
498
499 function start_read_image($source){
500 $mslfile='<?xml version="1.0" encoding="UTF-8"?>'."\n";
501 $mslfile.='<image>'."\n";
502 $mslfile.='<read filename="'.$source.'" />'."\n";
503 return $mslfile;
504 }
505
506 function close_read_image($dest){
507 $mslfile='<write filename="'.$dest.'" />'."\n";
508 $mslfile.="</image>";
509 return $mslfile;
510 }
511
512
513
514 function _magickimage_escape_shell($filename) {
515 if (strstr($_SERVER['SERVER_SOFTWARE'], 'Win32') || strstr($_SERVER['SERVER_SOFTWARE'], 'IIS')) {
516 return '"' . addslashes($filename) . '"';
517 } else {
518 return escapeshellarg($filename);
519 }
520 }
521
522 /**
523 * Calls the conjure binary the mslfile.
524 */
525 function _magickimage_conjure($mslcontents) {
526 $conjure_path = variable_get('imagemagick_conjure', '/usr/bin/conjure');
527 if (!file_exists($conjure_path)) {
528 return false;
529 }
530
531 $mslfile=mslfile($mslcontents);
532 $err = _magickimage_exec("$conjure_path $mslfile");
533
534 /* After finishing the conversion delete the mslfile . Instead of cleaning up using a cron job.*/
535
536 unlink($mslfile);
537 if ($err) {
538 return false;
539 }
540
541 return true;
542 }
543
544 function _magickimage_exec($cmd) {
545 if (substr(php_uname(), 0, 7) == "Windows"){
546 if ($h = popen("start \"bla\" $cmd", "r")) {
547 pclose($h);
548 return true;
549 } else {
550 return false;
551 }
552 } else {
553 return exec($cmd);
554 }
555 }
556
557
558 ?>

  ViewVC Help
Powered by ViewVC 1.1.2