0
Get image height and width.
Participant
,
/t5/coldfusion-discussions/get-image-height-and-width/td-p/878676
Sep 29, 2006
Sep 29, 2006
Copy link to clipboard
Copied
Is there a method to get the the height and width of a .gif
or .jpg? I plan to use cffile to grab the file name to place in a
db record. This record also needs to contain the heigth and width
of the graphic.
I attempted to use the Java written by Peter Crowley to do this but get the following error when I upload to my host server. It works fine on my localhost however.
Error: You have attempted to dereference a scalar variable of type class java.lang.String as a structure with members.
I attempted to use the Java written by Peter Crowley to do this but get the following error when I upload to my host server. It works fine on my localhost however.
Error: You have attempted to dereference a scalar variable of type class java.lang.String as a structure with members.
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Participant
,
/t5/coldfusion-discussions/get-image-height-and-width/m-p/878677#M80935
Sep 29, 2006
Sep 29, 2006
Copy link to clipboard
Copied
Post the code that is giving you the errors. It doesn't seem
like something is right.
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
ghouser
AUTHOR
Participant
,
LATEST
/t5/coldfusion-discussions/get-image-height-and-width/m-p/878678#M80936
Sep 29, 2006
Sep 29, 2006
Copy link to clipboard
Copied
No prob. I am going to omit the actual domain in the code
however. I'll post both the "uplaod.cfm" and the resulting page
"imagesize.cfm". You can find the download on cflib.org at this
URL:
http://www.cflib.org/udf.cfm?ID=1019
<--- upload.cfm ---->
<center>Click here to upload:
<cfif isdefined("form.upload_now")>
<cffile action="upload" filefield="ul_path" destination= "d:\web\vhosts\aasrdanville.com\httpdocs\eventpics\" nameconflict="overwrite">
</cfif>
<form action="imagesize.cfm" method="post" name="upload_form" enctype="multipart/form-data" id="upload_form">
<input type="File" name="ul_path" size="90" id="ul_path">
<input type="submit" name="upload_now" value="Submit"></center>
</form>
<---- imagesize.cfm ---->
<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 (pcrowley@webzone.ie)
* @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>
<CFSCRIPT>
kImageSize = ImageSize("./images/decembers_rose_cast.jpg.jpg");
</CFSCRIPT>
<CFOUTPUT>
#kImageSize.ImageWidth#<BR/>
#kImageSize.ImageHeight#
</CFOUTPUT>
<--- upload.cfm ---->
<center>Click here to upload:
<cfif isdefined("form.upload_now")>
<cffile action="upload" filefield="ul_path" destination= "d:\web\vhosts\aasrdanville.com\httpdocs\eventpics\" nameconflict="overwrite">
</cfif>
<form action="imagesize.cfm" method="post" name="upload_form" enctype="multipart/form-data" id="upload_form">
<input type="File" name="ul_path" size="90" id="ul_path">
<input type="submit" name="upload_now" value="Submit"></center>
</form>
<---- imagesize.cfm ---->
<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 (pcrowley@webzone.ie)
* @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>
<CFSCRIPT>
kImageSize = ImageSize("./images/decembers_rose_cast.jpg.jpg");
</CFSCRIPT>
<CFOUTPUT>
#kImageSize.ImageWidth#<BR/>
#kImageSize.ImageHeight#
</CFOUTPUT>
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

