Lasso Soft Inc. > Home

[vz_ckimageresize]

Linkvz_ckimageresize
AuthorPier Kuipers
CategoryImage
Version8.5.x
LicensePublic Domain
Posted09 Nov 2010
Updated09 Nov 2010
More by this author...

Description

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 tag in the HTML with the new image source. If the image is resized again, the tag attempts to re-use the original image, and not the current one which may have been resized already.

Example -

original image: /Vhosts/ckeditor/filemanager/images/myniceimage.jpg

resized image: /Vhosts/ckeditor/filemanager/images/myniceimage_vzimg_200x345.jpg

Requires ImageMagick and Shell tag, or someone to rewrite it using Lasso image tags :-)

Sample Usage

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');

Source Code

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;

Related Tags

Comments

10 Nov 2010, Dominique Guardiola Falco

thanks

Pier to share this, exactly the kind of thing I delay since one or two years...

Please log in to comment

Subscribe to the LassoTalk mail list

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2015 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft