Link | vz_ckimageresize |
Author | Pier Kuipers |
Category | Image |
Version | 8.5.x |
License | Public Domain |
Posted | 09 Nov 2010 |
Updated | 09 Nov 2010 |
More by this author... |
CKeditor is a wonderful addition to any CMS. It allows users to insert images which can then be resized using a lovely GUI. The only problem is, that the images are simply displayed at different dimensions using HTML or CSS tags - the original image remains untouched, and this can be a problem where users upload very large photographs and then resize them in CKeditor.
This tag "saves" the original image and creates a new image in the same location, appending "_vzimg_" plus the new dimensions to the filename. the tag then replaces the
var('mycontent'=(action_param('ckeditor_content'))); $mycontent=(vz_ckimageresize(-html=#mycontent, -webroot='///Library/WebServer/Documents/yadayada/')); var('dbupdate'='UPDATE cmstable SET pagecontent="'+$mycontent+'" WHERE yadada');
Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.
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=']*>')); // Start by isolating all 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;
©LassoSoft Inc 2015 | Web Development by Treefrog Inc | Privacy | Legal terms and Shipping | Contact LassoSoft
thanks