Shaun Mccran

My digital playground
 
07
M
A
Y
2010

ImageCFC losing file permissions on image manipulation

This article deals with a work around for a problem I had been experiencing with the imageCFC ( http://www.opensourcecf.com/imagecfc/ ) open source project. When resizing an image the file permissions were being lost on the edited file. This meant that the server operating system could not read or serve up the file to the browser.

I've been using imageCFC as I have a variety of coldfusion server versions (7 and 8), and I like the fact that in the resize method you can specify the maximum size for the width and height, and it will resize down to that size, and keep the aspect ratio of width-to-height.

[More]

 
06
N
O
V
2009

Issues dealing with large text files? Use a file splitter App!

I've been dealing with bulk data recently, CSV files of around 500mb or more. These can seriously stress out your pc. In the process of trying to write data import scripts I found an application that has proven its worth a few times over. This application allows you to split text files at points of your choosing.

File Splitter Pro, from wonderwebware. It doesn't look amazing, and it has a relatively simple interface, but after going through three or four alternatives that for the most part couldn't even open a 500mb csv file this has come through every time. What I really like though is the function that allows you to select N number of lines and move them to another file.

There is a trial version available too:

http://wonderwebware.com/file-splitter-pro/

(P.S I don't get commission or anything.)

 
14
O
C
T
2009

Uploading an image and previewing the thumbnail in one hit

A recent piece of functionality required a user to be able to upload an image, and view a thumbnail of that image alongside all the other related details that they were entering (item specific data etc).

After having a look through several forums it seemed that people's opinions were split on whether this could be accomplished using Ajax at all. So I decided to go old school and use an iFrame. I'm not a big fan of frames in any incarnation, but in this case I was willing to make an exception.

So firstly there is an upload form. It is a pretty standard ColdFusion form. Just remember when uploading a file to set the enctype="multipart/form-data". Also the submit button does not submit the form, it fires a JavaScript function 'changeFrame()'. The JavaScript function sets the target of the form submission to the iFrame, and then submits it.

view plain print about
1<div>
2    <iframe id="uploadFrame" name="uploadFrame" src="action.cfm" height="110" width="90" scrolling="no" frameborder="0"></iframe>
3    
4<form name="uploader" action="action.cfm" method="post" enctype="multipart/form-data">
5        <input type="file" name="image" size="5">
6        <input type="button" name="action" value="Upload" onclick="changeFrame()"/>
7    </form>
8</div>
9
10<s/cript>
11function changeFrame()
12    {
13        document.getElementById('uploader').target = 'uploadFrame';
14        document.getElementById('uploader').submit();
15    }
16</s/cript>

The form submits to the 'action.cfm' template. This template displays a placeholder image thumbnail initially, but when the form is submitted it performs the file upload using cffile, then displays the newly uploaded file instead of the placeholder. Ideally at this point I would like to resize the image with cfimage, but my hosting company is still using ColdFusion 7, so I may have to use a third party tag to do the same.

view plain print about
1<body style="margin: 0;">
2<cfset dest = "webroot/root/tmp/">
3
4<cfif isdefined('form.image')>
5    <cffile action="upload" filefield="form.image" destination="#dest#" nameconflict="makeunique">
6    <cfset variables.img = cffile.clientFile>
7<cfelse>
8    <cfset variables.img = "holder.gif">
9    
10</cfif>
11
12<cfoutput><img src="tmp\#variables.img#" height="100" width="75" border="1"></cfoutput>
13</body>

It is a shame I had to use an iFrame, and it would be really interesting to see if this is possible in a more web 2.0 scripted way.

 
01
O
C
T
2009

Introducing third party applications into your frameworks, good practice?

A recent development involved some changes to a large script that was using a product called ImageMagick (link). It is an image manipulation tool that allows you to perform transformations and other actions on image files. The product seems stable enough, and can be executed from a web interface using cfexecute, in a kind of command line prompt method.

It got me thinking as to why the application wasn't using cfimage, and then lead me to examine the wider topic of whether to use ColdFusion's in built functionality, or opt for other third party products.

I can think of a few obvious deciding factors for both pro and con, the first that springs to mind is performance.

Is there a marked performance difference between a ColdFusion function, and the third party application? Is it better to pass the load of to the operating system, rather than have ColdFusion perform whatever processing function it is supposed to do? In this case it would be a race between cfexecute, and cfimage, so there may be very little difference in it.

The second major point that springs to mind is the knowledge base of the developer, and the structure of whatever framework you are using. What I really mean by this is 'if your chosen technology can perform a function, why not utilise it to its full potential?' . It seems a bizarre choice indeed to deliberately not harness a function that your platform can already provide, and instead introduce another code base or application into the framework.

It also introduces another hurdle for the development staff, they may well be familiar with how an existing Tag works, but be totally unaware of the third party application, as was my case here.

A long winded intro, but here is the code I used:

view plain print about
1<!--- setup vars --->
2<cfset variables.destination = "C:\dev\images\testimage.jpg">
3<cfset variables.source = "C:\dev\images\testimage.jpg">
4<cfset variables.width = "100">
5<cfset variables.height = "100">
6<cfset variables.exec = "C:\dev\apps\imageMagick\convert.exe">
7
8<!--- executing an external application version --->
9<cfexecute name="#variables.exec#" arguments="-size #variables.width# #variables.source# -geometry #variables.height# -strip #variables.destination#" variable="imageinfo" timeout="3" />
10
11<!--- CFimage version --->
12<cfimage source="#variables.source#" action="resize" width="#variables.width#" height="#variables.height#" destination="#variables.destination#">

 
01
S
E
P
2009

Incorrect error message reporting using CFFile action rename

During a batch process that I was writing recently I was using a CFfile operation to rename some files.

When running it I came across this error message:

view plain print about
1Attribute validation error for tag CFFILE.
2The value of the attribute source, which is currently "C:\fullUrl.jpg", is invalid.

This is the calling cfml code. The actual variable values aren't important.

view plain print about
1<cffile action="rename" source="#variables.tmpFileLocation#" destination="#variables.dir#/#variables.photoid#.jpg">

So I checked and doubled check the source url, and manually browsed to the directory and viewed it. The file was right there! I had a wander around online and found that this isn't a new issue, but more a bad error report.

The error isn't actually relating to the 'source' attribute at all, but rather the 'destination' attribute. In this case it was a file/folder permissions problem. (I didn't have write access). It was simply a case of Coldfusion server reporting it incorrectly.

 
13
A
U
G
2009

Importing large datasets into MySQL - Packet too large errors

I was recently migrating some data from one application to another and came across a stumbling block in MySQL. If you are trying to run a large SQL file backup restoration and the source file is quite large you may encounter the error : 'Packet too large' or 'MySQL server has gone away'. This is cuased by a MySQL server memory error.

If you look in the mySQLserver installation path directory you should see a 'my.ini' file. This controls the default settings for SQL server when you start the service.

Add in a line of code like this:

view plain print about
1max_allowed_packet=250MB

under the value of [mysqld]. Save the file and then restart mySQL server.

You should now be able to run:

show variables like 'max_allowed_packet';

And have it return your ini value.

As long as the value you have set it to is greater than the file size it should run successfully.

You can also run it as a set session variable command in your SQL environment, like this:

view plain print about
1set session max_allowed_packet=45777216

This will set the same value for that instance of the sql server.

Update:

There also seems to be a massive difference in performance in running the imports directly into a GUI environment, or through a shell command. I've had a 600mb file fail several times in the GUI, but running:

view plain print about
1path/mysql -u USER -p DBNAME < file.sql

It has imported within around 10 to 15 minutes.

 
28
J
U
L
2009

Finding the system file storage in AIR

When progamming an AIR application, you may want to make use of the applicationStorageDirectory available via the flash.filesystem package to store temporary files/folders. You can find where your system is storing these files by doing something like the following:

view plain print about
1var f:File = File.applicationStorageDirectory.resolvePath("Test.txt");
2trace(f.nativePath + ' is where my file is stored');

This will give you an absolute path to the local system file storage location. Handy for multi platform applications, as Pc and MAC based systems will use different default storage directories.

 
03
J
U
L
2009

Function for getting the last modified date of a template

Whilst looking at creating dynamic Sitemaps for Google bot spidering I found that I needed to populate an XML node with the last modified date of the templates. I figured there must be a programmatic way of doing this, so after some searching around this is what I ended out with:

view plain print about
1<cfset mod_time = createObject("java", "java.util.Date").init(createObject("java", "java.io.File").init(getcurrenttemplatepath()).lastModified())>

This creates a java object, using the date and file utilities we can use 'getcurrenttemplatepath()' to provide the path data, finally referencing the lastModified property.

A handy and relatively quick way of getting the last modified date.

It is a bit of a mouthful though, so might be easier to create it as a handy referencable object:

view plain print about
1<cffunction name="modStamp" access="public" returntype="date" output="no" hint="Gets the last mod date of a file, returns a ts">
2    <cfargument name="file" type="string" required="yes" hint="File to get mod date">
3        <cfset var mod_time = now()>
4        
5        <cfif fileexists(arguments.file)>
6            <cfset mod_time = createObject("java", "java.util.Date").init(createObject("java", "java.io.File").init(arguments.file).lastModified())>
7        </cfif>
8        
9    <cfreturn mod_time>
10</cffunction>

Then you can just do:

view plain print about
1<cfset variables.fileMod = modStamp()>


This content is purely my opinon, any offence or errors are unintentional, please comment your views appropriately
Aggregated by ColdfusionBloggers.org Powered by Coldfusion

Technology & Science Blogs - BlogCatalog Blog Directory Blog Directory & Search engine