Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / ColdFusion / Advanced Techniques / April 2007



Tip: Looking for answers? Try searching our database.

Possible to determine image dimensions on cffile upload?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gregory@ETR - 23 Mar 2007 16:47 GMT
Hello,

Is there any way to determine the pixel dimensions of a jpg or gif image file that has been uploaded using cffile?

Regards,
Greg
jdeline - 23 Mar 2007 17:05 GMT
Page 6 of http://www.w3.org/Graphics/JPEG/jfif3.pdf describes the location of
the Xdensity and Ydensity fields (2 bytes each).  
http://www.martinreddy.net/gfx/2d/GIF87a.txt will tell you similar information
about the GIF file format.

If that's too much to digest, get yourself a good custom tag, or wait for
native functionality in ColdFusion 8.  Google coldFusion "custom tag" "image
size"
ghouser - 23 Mar 2007 17:26 GMT
Yes, I use a script that will determine the dimensions so I can put the heigth
and width into database fields for use laster. I will attach the code I use for
you.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
    <title>Upload Event Photos</title>
    <link rel=stylesheet type="text/css" href="style.css">
<cfscript>
/**
 * Returns width and height of images based on image type.
 *
 * @param filename     Absolute or relative path to file. (Required)
 * @param mimetype     Minetype for the file. (Optional)
 * @return Returns a struct containing height and width information, or an
error string.
 * @author Peter Crowley
(&#112;&#99;&#114;&#111;&#119;&#108;&#101;&#121;&#64;&#119;&#101;&#98;&#122;&#11
1;&#110;&#101;&#46;&#105;&#101;)
 * @version 1, August 17, 2006
 */
function ImageSize(filename) {
    // Jpeg variables
    var nFileLength=0; var nBlockLength=0; var nMarker=0;
    var nSOI = 65496; // Start of Image (FFD8)
    var nEOI = 65497; // End of Image (FFD9)
    var nSOF = 65472; // Start of frame nMarker (FFC0)
    var nSOF1 = 65473; // Start of frame extended sequential mode (FFC1)
    var nSOF2 = 65474; // Start of frame progressive mode (FFC2)
    var nSOF3 = 65475; // Start of frame lossless mode (FFC3)
    var nSOS = 65498; // Start of Scan (FFDA)
    var sImageType = "";
    var kCoords = structNew();
    var fInput = 0;
    var sByte=0;
    var sFullPath="";
    var sMimeType = "";
   
    if (Left(filename,1) IS "/" OR Left(filename,1) IS "\" OR MID(filename,2,1)
IS ":")
        sFullPath=filename;
    else
        sFullPath=ExpandPath(filename);

    // Establish image type
    if(arrayLen(arguments) gt 1) {     //optional mimetype
        sMimeType = arguments[2];
        if (LCase(ListFirst(sMimeType,"/")) IS NOT "image") return "Wrong mime type";
        if (ListLen(sMimeType,"/") NEQ 2) return "Invalid mime type";
        sImageType=LCase(ListLast(sMimeType,"/"));
    } else { // work off file extension
        if (ListLen(filename,".") LT 2) return "Unknown image type";
        sImageType=LCase(ListLast(filename,"."));
    }

    if(not fileExists(sFullPath)) return "File does not exist.";
   
    //make a fileInputStream object to read the file into
    fInput = createObject("java","java.io.RandomAccessFile").init(sFullPath,"r");
   
    // Get X,Y resolution sizes for each image type supported
    switch (sImageType) {
    case "jpg": case "jpeg":
        do {
            nMarker = fInput.readUnsignedShort();

            if (nMarker NEQ nSOI AND nMarker NEQ nEOI AND nMarker NEQ nSOS) {

                nBlockLength = fInput.readUnsignedShort();

                if (nMarker EQ nSOF OR nMarker EQ nSOF1 OR nMarker EQ nSOF2 OR nMarker EQ
nSOF3) { // Start of frame
                    fInput.readUnsignedByte(); // skip sample precision in bits
                    kCoords.ImageHeight = fInput.readUnsignedShort();
                    kCoords.ImageWidth = fInput.readUnsignedShort();
                    fInput.close();
                    return kCoords;
                } else {
                    fInput.skipBytes(JavaCast("int",nBlockLength-2));
                }
            }
        } while (BitSHRN(nMarker,8) EQ 255 AND nMarker NEQ nEOI);
        break;
    case "gif":
        fInput.skipBytes(6);

        sByte = fInput.readUnsignedByte();
        kCoords.ImageHeight = fInput.readUnsignedByte() * 256 + sByte;

        sByte = fInput.readUnsignedByte();
        kCoords.ImageWidth = fInput.readUnsignedByte() * 256 + sByte;
   
        fInput.close();
        return kCoords;
    default:
        break;
    }
    //close out this entry
    fInput.close();
    return "Unhandled image type";
}
</cfscript>
</head>

<body bottommargin="0" leftmargin="0" rightmargin="0" topmargin="0"
background="../img/background.jpg">

<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr valign="top">
<td width="100"><BR><font face="Arial Black">Database
<u>Maintenance</u></font><br><br>
<!----- menu, insert links below ------------------------------------------>
<cfinclude template="../dbmaint/dbmaintmenu.cfm">
<!-------------------------------------------------------------------------->
</td>
<td>
    <table width="100%" cellpadding="10" cellspacing="0" border="0>
        <tr valign="top">
            <td width=100%>

<!-------- page data goes below here ------>
<!--------------->   
<div align="center"><h1><font color="#FF0000">Database
Maintenance</font></h1></div>
<br><br><br>

<div align="center">
<table border="0">
<tr>
<td colspan="2">

<div align="center">Click here to upload a new Event Photo:
<cfif isdefined("form.upload_now")>
<cffile action="upload" filefield="ul_path" destination= "*** insert your path
here ****" nameconflict="overwrite">
<cfset newfile = #cffile.serverfile#>

<CFSCRIPT>
kImageSize = ImageSize("../eventpics/#newfile#");
</CFSCRIPT>

<cfquery name="insert_eventpics" datasource="gph">
insert into eventpics
(eventname, picpath, imageheight, imagewidth, pictext)
values
('#eventname#', '#newfile#', '#kImageSize.ImageHeight#',
'#kImageSize.ImageWidth#', '#pictext#')
</cfquery>
</cfif>
<div align="left"><form action="" method="post" name="upload_form"
enctype="multipart/form-data" id="upload_form">
<input type="File" name="ul_path" size="40" id="ul_path"></div>
</td>
</tr>
<tr>
<td><input type="text" name="eventname" id="eventname" size="40">
</td>
<td>
<-- Event name
</td>
</tr>
<tr>
<td> <textarea name="pictext" id="pictext" cols="32" rows="6"
wrap="hard"></textarea>
</td>
<td>
<-- Picture text
</td>
</tr>
<tr>
<td colspan="2">
<div align="center"><input type="submit" name="upload_now"
value="Submit"></div>
</td>
</tr>
</form>
</table>
</div>
<!-------- page data goes above here ------>
<!--------------->           

<br><br>
            </td>
        </tr>
    </table>
</td>
</tr>
</table>

</body>
</html>
Abinidi - 24 Mar 2007 10:13 GMT
or...

<cfset image_name ="C:\pathtofile\imagefile.jpg">

<cfscript>
  imgFile = createObject("java","javax.swing.ImageIcon").init("#image_name#");
  imgFile.getImage();
  w = imgFile.getIconWidth();
  h = imgFile.getIconHeight();
</cfscript>

<cfoutput>width="#w#"  height="#h#</cfoutput>

Adjust code accordingly to what you are doing.
Azadi - 26 Mar 2007 15:56 GMT
>  <cfscript>
>    imgFile = createObject("java","javax.swing.ImageIcon").init("#image_name#");
>    imgFile.getImage();
>    w = imgFile.getIconWidth();
>    h = imgFile.getIconHeight();
>  </cfscript>

this is way cool, man!
do you know if there is a way to check the dimensions of an uploaded
flash (swf) movie ?

Signature

Azadi Saryev
Sabai-dee.com
Vientiane, Laos
http://www.sabai-dee.com

cf_dev2 - 27 Mar 2007 02:53 GMT
>do you know if there is a way to check the dimensions of an uploaded
>flash (swf) movie ?

I have never used it but you might check out FlashInspector.CFC at
http://www.doughughes.net/index.cfm?event=viewEntry&entryId=166
Azadi - 27 Mar 2007 03:58 GMT
exactly what i needed! thanks a bunch!

Signature

Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com

Azadi - 01 Apr 2007 18:04 GMT
> or...
>
[quoted text clipped - 10 lines]
>
>  Adjust code accordingly to what you are doing.

i am trying to use the above code to check for dimensions of uploaded
image and delete the image (through cffile action=delete) if its
dimensions are different from required. the checking part works fine,
but i am unable to delete the image!
CF trows an error:
 ColdFusion could not delete the file "[full path to file]\[filename]"
for an unknown reason.

the path is correct, the file is there, and i can delete it manually
without a problem, but not with cffile action=delete...

any ideas? does that have something to do with the instantiated java
object? how do i de-instantiate it?

Signature

Azadi Saryev
Sabai-dee.com
Vientiane, Laos
http://www.sabai-dee.com

Azadi - 03 Apr 2007 02:00 GMT
well, i have come up with a work-around, but would still like to learn
how (if possible) to release a java object invoked with createObject()...

in case anyone is interested, the work-around i implemented is:
add uploaded files of wrong dimensions to a list, after succefful upload
of correct file pass the list to another page which silently loops
through it and deletes the files from server and returns user to
original calling page...
Signature


Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com

cf_dev2 - 27 Apr 2007 21:08 GMT
Azadi,

I ran a few tests and cffile delete worked. So I'm not sure what the problem
is.  I looked at the cfc and it refers to two java classes.

java.io.ByteArrayInputStream
java.util.zip.InflaterInputStream

Both classes have a close() method,  though ByteArrayInputStream.close()
doesn't do anything.  For grins .. you might try adding a function to the
FlashInspector.cfc that calls the close() method on the java objects. Then call
that function just before attempting to deleting the file.  I don't know that
it will help but its worth a shot.

<!--- add to FlashInspector.cfc --->
    <cffunction name="close" output="false" returntype="void">
        <cftry>   
            <cfset variables.in.close() />
            <cfcatch>
                <!--- ignore --->
            </cfcatch>
        </cftry>
    </cffunction>

<!--- in upload page --->
..
<cfset FlashInspector.close() />
<cffile action="delete" ..>
Azadi - 28 Apr 2007 07:19 GMT
thanks for your input, cf_dev2!

i was actually having the problem with getting jpeg dimensions using
Abinidi's code that uses javax.swing.ImageIcon class, not the
FlashInspector.cfc...

the FlashInspector works like a charm... it's the created java object
that would not release and let me delete the jpeg image if its
dimensions are wrong...

i have since switched to using imagecfc from opensourcecf - a lot more
code, but all packed into a cfc, and lets you resize/crop the image to
correct dimensions instead of just telling the user "sorry, your image
is wrong size, please resize and re-upload"...

but thank for your time looking into this, and your url for the
FlashInspector.cfc was a true lifesaver!

Signature

Azadi Saryev
Sabai-dee.com
Vientiane, Laos
http://www.sabai-dee.com

cf_dev2 - 28 Apr 2007 08:07 GMT
> i was actually having the problem with getting jpeg dimensions

Azadi,

Well that's what I get for trying to read before I've had my morning coffee ;-)

Now that I think about it I remember having a similar problem. Either with
java.swing.ImageIcon or java.awt.Image or both. But it sounds like  imagecfc
has solved your problem and it does have some great features.

Until next time then!
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.