Monday, November 08, 2010 at 12:03 AM.


on getImageData (imageSpec, pageTable=nil) {
	<<Get image data: URL, path, address, file type, suffix, height, and width.
		<<Writes the image file to disk.
		<<Returns a table containing:
				<<Relative URL.
				<<Path to file on disk.
				<<The address of the image in the odb.
				<<Mac file type.
				<<".gif" or ".jpg"
				<<The actual height of the image.
				<<The actual width of the image.
			<<Sunday, January 18, 1998 at 3:43:02 PM by PBS
			<<10/3/98; 8:00:58 AM by DW
				<<if there there's a pref "noImageExtensions", then we empty out the extension
				<<we need this to support rendering from within mainResponder
			<<PBS 1/11/99
				<<The pref is changed from "noImageExtensions" to "imageExtensions" so
				<<it can default to true.
				<<If imageExtensions is false, then empty out the extension,
				<<to support rendering from mainResponder.
			<<06/01/00; 6:39:47 PM by PBS
				<<Send the pageTable parameter to html.getPref when getting the imageFolderSubtables pref.
			<<06/17/00; 7:40:10 PM by PBS
				<<Don't write to disk if this is a dynamic page.
			<<07/05/00; 5:45:35 PM by PBS
				<<Get the JPEG height and width from the JPEG object in Frontier, not from the JPEG file on disk -- because this script may be running in a dynamic webserver context and there may not be a JPEG file on disk.
	if pageTable == nil {
		try {
			pagetable = html.getPageTableAddress ()}
		else {
			pagetable = @websites.["#data"]}};
	local (nomad = pageTable^.adrObject);
	local (urlPrefix, extension = "");
	local (folder = file.folderFromPath (pageTable^.f));
	local (binType, adrImage);
	local (pc = file.getPathChar ());
	local (fname, f, macFileType);
	local (dataTable, cachedImageData);
	local (flcache = html.getPref ("useImageCache", pageTable));
	local (height, width, hwlist = {0, 0}, url);
	local (imageFolderName = html.getPref ("imageFolderName", pageTable));
	local (lowerFolderName = string.lower (imageFolderName));
	local (flSubFolders = html.getPref ("imageFolderSubtables", pageTable)); //PBS 06/01/00: send the pageTable parameter to html.getPref. Minor bug fix.
	bundle { //build the path to the image
		case (typeOf (imageSpec)) {
			addressType {
				adrImage = imageSpec};
			stringType {
				loop {
					on checkTable (adrTable) {
						if defined (adrTable^.[imageSpec]) and typeOf (adrTable^.[imageSpec]) != tableType {
							adrImage = @adrTable^.[imageSpec];
							return (true)};
						if flSubFolders {
							local (i);
							for i = 1 to sizeOf (adrTable^) {
								if typeOf (adrTable^ [i]) == tableType {
									if checkTable (@adrTable^ [i]) {
										return (true)}}}};
						return (false)};
					if nomad == pageTable^.adrSiteRootTable { //don't pop out past the root table
						scriptError ("Can't locate an image object named \"" + imageSpec + "\".")};
					nomad = parentOf (nomad^);
					if nomad == nil { //PBS 12/24/98
						scriptError ("Can't locate an image object named \"" + imageSpec + "\".")};
					try {
						if checkTable (@nomad^.["#" + imageFolderName]) {
					if lowerFolderName == "images" {
						try {
							if checkTable (@nomad^.[imageFolderName]) {
					folder = file.folderFromPath (folder);
					urlPrefix = "../" + urlPrefix};
				folder = folder + imageFolderName + pc;
				urlPrefix = urlPrefix + imageFolderName + "/";
				if flSubFolders {
					local (nomadList = string.parseAddress (nomad));
					local (imageList = string.parseAddress (adrImage));
					local (i);
					for i = sizeOf (nomadList) + 2 to sizeOf (imageList) - 1 {
						local (oneFolder = imageList [i]);
						if oneFolder beginsWith "#" {
							oneFolder = string.delete (oneFolder, 1, 1)};
						folder = folder + oneFolder + pc;
						urlPrefix = urlPrefix + oneFolder + "/"}}}}
		else {
			scriptError ("Imagespec param must be an address or a string.")}};
	bundle { //add file extension to path
		binType = getBinaryType (adrImage^);
		binType = string.lower (string.popTrailing (binType, ' '));
		case binType {
			"giff" {
				extension = ".gif";
				macFileType = 'GIFf'};
			"jpeg" {
				extension = ".jpg";
				macFileType = 'JPEG'}};
		<<We might be running in the website framework responder.
		if html.inResponder (pageTable) {
			extension = ".wsf"};
		if not (html.getPref ("imageExtensions", pageTable)) { //10/3/98; 7:58:52 AM by DW, to support mainResponder images
			extension = ""};
		fname = nameOf (adrImage^) + extension;
		f = folder + fname;
		url = urlPrefix + fname};
	bundle { //write file to disk
		local (lowerWriter = string.lower (pageTable^.fileWriter));
		if not (defined (pageTable^.requestHeaders) and macFileType == 'GIFf') {
			if lowerWriter != "file" {
				html.writeFile (f, adrImage, macFileType, html.getPref ("imgFileCreator", pageTable), (), pageTable)};
			if (not flcache) or (not (file.exists (f))) { //if caching is off, or the file doesn't exist, write to disk.
				<<We need to write to disk to get the size info.
				if not defined (pageTable^.requestHeaders) { //PBS 06/17/00: don't write to disk if this is a dynamic page
					file.sureFilePath (f);
					file.writeWholeFile (f, adrImage^, macFileType, html.getPref ("imgFileCreator", pageTable), ())}}}};
	bundle { //get height and width of image
		local (flfound = false);
		if flcache { // get height and width from the image cache
			<<Thursday, January 15, 1998 at 3:55:21 PM by PBS
			local (adrCache = @system.temp.htmlImageCache);
			if not defined (adrCache^) {
				new (tableType, adrCache)};
			cachedImageData = @adrCache^.[adrImage];
			if defined (cachedImageData^) {
				height = cachedImageData^.height;
				width = cachedImageData^.width;
				flfound = true}};
		if not flfound {
			case macFileType {
				'GIFf' {
					hwList = html.getGifHeightWidth (adrImage)};
				'JPEG' {
					hwList = html.getJpegHeightWidth (adrImage)}}; //PBS 07/05/00: use the address of the JPEG object, not the file path, to get the height and width. Because this may be a dynamic page, the JPEG may not exist on disk.
			height = hwList [1];
			width = hwList [2];
			if flcache {
				new (tableType, cachedImageData);
				cachedImageData^.height = height;
				cachedImageData^.width = width}}};
	bundle { //build returned data table
		new (tableType, @dataTable);
		dataTable.url = url;
		dataTable.path = f;
		dataTable.adrImage = adrImage;
		dataTable.macFileType = macFileType;
		dataTable.suffix = extension;
		dataTable.height = height;
		dataTable.width = width};
	return (dataTable)}

This listing is for code that runs in the OPML Editor environment. I created these listings because I wanted the search engines to index it, so that when I want to look up something in my codebase I don't have to use the much slower search functionality in my object database. Dave Winer.