Last active
November 8, 2023 05:59
-
-
Save cflove/2d73ae2834d1aa9944b221032dcb673b to your computer and use it in GitHub Desktop.
create a favicon.icon file with coldfusion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cffunction name="createFavicon" access="public" returntype="void"> | |
<cfargument name="letter" type="string" required="true"> | |
<cfargument name="saveLocation" type="string" required="true"> | |
<!--- Set image dimensions (16x16 pixels) ---> | |
<cfset width = 16> | |
<cfset height = 16> | |
<!--- Create a buffered image with transparency ---> | |
<cfset BufferedImage = createObject("java", "java.awt.image.BufferedImage")> | |
<cfset image = BufferedImage.init(width, height, BufferedImage.TYPE_INT_ARGB)> | |
<!--- Get the Graphics2D object to draw on the image ---> | |
<cfset Graphics2D = createObject("java", "java.awt.Graphics2D")> | |
<cfset g2d = image.createGraphics()> | |
<!--- Enable anti-aliasing for smoother text rendering ---> | |
<cfset RenderingHints = createObject("java", "java.awt.RenderingHints")> | |
<cfset g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)> | |
<!--- Enable text anti-aliasing for even smoother text rendering ---> | |
<cfset g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON)> | |
<!--- Set background color ---> | |
<cfset Color = createObject("java", "java.awt.Color")> | |
<cfset backgroundColor = Color.decode("##00BBF2")> | |
<cfset g2d.setColor(backgroundColor)> | |
<cfset g2d.fillRoundRect(0, 0, width, height, 4, 4)> | |
<!--- Set font and color ---> | |
<cfset Font = createObject("java", "java.awt.Font")> | |
<cfset font = Font.init("Arial", Font.BOLD, 15)> | |
<cfset g2d.setFont(font)> | |
<cfset g2d.setColor(Color.white)> | |
<!--- Calculate the position for center-aligned text ---> | |
<cfset fontMetrics = g2d.getFontMetrics()> | |
<cfset stringWidth = fontMetrics.stringWidth(arguments.letter)> | |
<cfset stringHeight = fontMetrics.getAscent() - fontMetrics.getDescent()> | |
<cfset x = (width - stringWidth) / 2> | |
<cfset y = (height + stringHeight) / 2> | |
<!--- Draw the input letter onto the image ---> | |
<cfset g2d.drawString(arguments.letter, x, y)> | |
<!--- Dispose the graphics context and release resources ---> | |
<cfset g2d.dispose()> | |
<!--- Save the image as .ico file ---> | |
<cfset File = createObject("java", "java.io.File")> | |
<cfset ImageIO = createObject("java", "javax.imageio.ImageIO")> | |
<cfset icoFile = File.init(arguments.saveLocation)> | |
<cfset ImageIO.write(image, "png", icoFile)> | |
</cffunction> | |
<cfset createFavicon("M", "D:\iis\....\favicon.ico")> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment