<%@ LANGUAGE = JScript%> <% // JavaScript Scripting Example 02 // // "Center a text in a large image" // // Dimac 2002-02-18 (www.dimac.net) var errormsg = new String(""); errormsg = CenterText(); // Check if errors occured if (errormsg != "") DisplayError(errormsg); // Center a text in the center of a large image function CenterText() { try { var imageobj; var fontobj; var text; var height; var width; var top; var left; var path; // Fix path path = Server.MapPath("..\\..\\images") + "\\"; imageobj = Server.CreateObject("W3Image.Image"); // Create a small surface (needed in order to perform font operations) imageobj.CreateEmptySurface(1,1); // Create and select the font (shadow) fontobj = imageobj.CreateFont("Tahoma",60,0,"normal",0,0x000000,false,false,true); imageobj.SetFont(fontobj); // Text to center text = "w3 Image is the answer..."; // Get the size of the text width = imageobj.GetTextWidth(text); height = imageobj.GetTextHeight(text); // Load the image if (imageobj.LoadImage(path + "sunset.jpg") == false) { // Do your error handling here... return "Error when loading image 'sunset.jpg'. Path " + path + "sunset.jpg is probably invalid."; } // Set back to transparency (JPG is not transparent!) // New 2.0 imageobj.bkMode = 1; // Scale the image (make it double size) imageobj.Scale(200); // Important!!!! Select the font again (it is lost when creating a new surface) imageobj.SetFont(fontobj); top = ((imageobj.height - height)/2); if (top < 0) top = 0; left = ((imageobj.width - width)/2); if (left <= 0) left = 0; // Draw the shadow text imageobj.DrawText(text, left, top); // Create and select a new font fontobj = imageobj.CreateFont("Tahoma",60,0,"normal",0,0xE2A701,false,false,true); imageobj.SetFont(fontobj); // Draw the text again imageobj.DrawText(text, left + 3, top + 3); if (imageobj.StreamImage(Response, "JPG", 24) == false ) { // Do your error handling here... return "Error when streaming image!"; } // Success return ""; } catch (err) { // Do your error handling here... return "Error: " + (err.number & 0xFFFF) + " - " + err.description + "."; } } // Example of a error handler - Displaying the error as a image function DisplayError(msgcode) { // Create an error image var errorimage = Server.CreateObject("W3Image.Image"); errorimage.CreateEmptySurface(1,1); // Create and select the font var fontobj = errorimage.CreateFont("Tahoma",24,0,"normal",0,0x000000,false,false,true); errorimage.SetFont(fontobj); // Get size of the error message var width = errorimage.GetTextWidth(msgcode); var height = errorimage.GetTextHeight(msgcode); // Create a surface as large as the error message errorimage.CreateEmptySurface(width,height); // Select the font again (font is deselected when creating a new surface) errorimage.SetFont(fontobj); // Write the error message errorimage.DrawText(msgcode,0,0); // Stream the image containing the error message errorimage.StreamImage(Response, "JPG", 24); } %>