1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
define_tag('ckimageresize', -namespace='vz_',
-required='html',
-required='webroot',
-optional='username',
-optional='password',
-optional='pathto_imagemagick',
-description='This tag physically resizes images that have been resized using ckeditor and are displayed at different dimensions from the original.');
local('output'=#html);
!local_defined('pathto_imagemagick') ? local('pathto_imagemagick' = string);
!local_defined('username') ? local('username' = string);
!local_defined('password') ? local('password' = string);
local('imagearray')=(string_findregexp((#html),-find='<img[^>]*>'));
// Start by isolating all <img> tags in the HTML and looping through them
iterate(#imagearray, local('n'));
local('dimensions'=string);
// use regex to establish the image source
local('src'=(string_findregexp(#n,-find='src="[^"]+"'))->first);
#src->removeleading('src="');
#src->removetrailing('"');
// find dimensions at which the image is displayed, keeping in mind that
// HTML tags may have been used, or CSS tags
local('display_width'=(string_findregexp(#n, -find='width="[^"]+"|width:[^p]+px;'))->first);
#display_width->removeleading('width="');
#display_width->removeleading('width:');
#display_width->removetrailing('"');
#display_width->removetrailing('px;');
#display_width->trim;
local('display_height'=(string_findregexp(#n, -find='height="[^"]+"|height:[^p]+px;'))->first);
#display_height->removeleading('height="');
#display_height->removeleading('height:');
#display_height->removetrailing('"');
#display_height->removetrailing('px;');
#display_height->trim;
if(file_exists(#src));
// only do something if we can actually find the source
local('location'=(#src)->split('/'));
// get the filename by splitting the path
local('file'=#location->last);
local('path'=#src);
// get the path by removing the filename from the end
#path->removetrailing(#file);
local('location'=(#path));
// get the extension - to be used by ImageMagick
local('ext'=((#file -> split('.')))->last);
// get filename without the extension
local('filename'=#file);
#filename->removetrailing('.'+#ext);
// Locate original, if applicable, by splitting image name on the _vzimg_ marker
local('originalroot'=((#filename -> split('_vzimg_'))->first));
local('original' = (#originalroot+'.'+#ext));
// log_critical('Original is '+#original);
if(file_exists(#location + #original));
local('convertthis' = (#location + #original));
else;
// if original can't be found, just use the current source
local('convertthis' = #src);
/if;
// log_critical('Going to use: '+#convertthis);
// create new filename, appending the original name with a _vzimg_ marker plus the display dimensions
local('newfilename'=(#originalroot+'_vzimg_'+#display_width+'x'+#display_height+'.'+#ext));
// use ImageMagick to establish the actual dimensions of the image
inline(-username=#username, -password=#password);
local('dimensions') = shell(#pathto_imagemagick+'identify -format "%w\n%h" "'+#webroot+#src+'" ');
/inline;
#dimensions = (#dimensions -> split('\n'));
local('actual_width'=(#dimensions -> first));
local('actual_height'=(#dimensions -> second));
if((#actual_width == #display_width) && (#actual_height == #display_height));
// Don't do anything if the image display hasn't been resized by FCK Editor
// log_critical(#src+' unchanged! '+#actual_width'x'#actual_height+' -> '+#display_width+'x'+#display_height+'.........'+#ext+'..........'+#filename);
else;
// image display has been resized by FCK Editor, so we create a new image in the same location at those exact dimensions
shell(#pathto_imagemagick+'convert -size '+#display_width+'x'+#display_height+'! -resize '+#display_width+'x'+#display_height+'! "'+#webroot+#convertthis+'" "'+#webroot+#location+#newfilename+'"');
// log_critical(#src+': '+(file_exists(#src))+' - '+#actual_width'x'#actual_height+' -> '+#display_width+'x'+#display_height);
// log_critical(#pathto_imagemagick+'convert -size '+#display_width+'x'+#display_height+' -resize '+#display_width+'x'+#display_height+' "'+#webroot+#convertthis+'" "'+#webroot+#location+#newfilename+'"');
// log_critical('find: '#src' - replace:'+#location+#newfilename);
// Using regex to replace the image source with the newly created file - no need to replace anything else!
#output = (string_replaceregexp(#output, -find=#src, -replace=(#location+#newfilename)));
/if;
/if;
/iterate;
return(@#output);
/define_tag;
|
thanks
Pier to share this, exactly the kind of thing I delay since one or two years...