Click here to Skip to main content
11,704,509 members (45,269 online)
Click here to Skip to main content

GD library wrapper

, 18 May 2005 93.1K 1.5K 30
Rate this:
Please Sign up or sign in to vote.
GD library wrapper (for ASP developers with PHP vs ASP examples).

Online demo.

(Hit F5 to see a new 'randomized' GIF generated!)

Introduction

This wrapper was developed by Trevor Herselman. GD is copyright 2005 Boutell.com, Inc. GD was developed by Thomas Boutell.

Files:

  • bgd.dll -- GD library binary, downloadable from here. N.B.: Not my work!!!
  • GDLibrary.dll -- ActiveX wrapper DLL, developed in VB6 with full source code! Needs to be registered before use!

VB Source Code

  • GDLibrary.vbp -- Visual Basic 6 project file.
  • gdImage.cls -- Class module file.
  • modGDLibrary.bas -- Standard VB module file, with VB function declarations of the bgb.dll functions.
  • modAPI.bas -- Standard VB module file, declaration of some Windows API functions, e.g. copyMemory.

ASP Examples

  • DynamicGif.asp -- Dynamic GIF example.
  • DynamicPng.asp -- Dynamic true color PNG example.
  • LoadPng.asp -- Example of loading a PNG file.

What is GD Library?

Edited quote from www.boutell.com:

"GD is an open source code library for the dynamic creation of images by programmers. GD creates PNG, JPEG and GIF images, and is commonly used to generate charts, graphics, thumbnails, graphs, text and almost anything on the fly."

FAQ

Why another wrapper?

Actually, it's the first of its kind and the only wrapper available for ASP developers. GD comes standard (built-in) with PHP 4.3.x.

Why use GD?

GD is a very mature and stable project. It's unbelievably fast and easy to use! PHP developers can easily see the similarities and ASP developers will have a smoother transition to PHP by using similar code! Take advantage of tutorials written for PHP with only syntax differences!

Why use this wrapper?

An image can be created/loaded, drawn on, saved to file or sent to a browser in less than 5 lines of code! There are several advantages ASP developers have, such as being able to read/write directly to BLOB or Image types in SQL. This wrapper creates an ADODB.Stream (ADO 2.8) object for internal use and is fully available to the client (object.Stream) with all the features and functionality that the Stream object brings!

What can GD do?

  • Create Palette/True color images
  • GetPixel, SetPixel
  • Draw lines, rectangles, arcs
  • Fill areas to borders
  • Filled arcs, filled ellipses
  • Write horizontal/vertical text

What else can the wrapper do?

Gradient fills (horizontal and vertical), load/save from/to a file, memory or database (using ADO 2.8 Stream object). Output dynamic image directly to client.

Current wrapper limitations?

To draw various fonts, GD uses FreeType library, I'm having problems with the function declarations. I have problems with the declaration of the variable length polygon drawing functions.

Example 1

Create a 500x500 PNG image and draw a blue rectangle.

PHP

<?php
    // Create a blank 500x500 pixel image. 
    $im = imagecreate(500, 500);
    // Allocate $white to the white color in $im. 
    $white = imagecolorallocate($im, 255, 255, 255);
    // Allocate $blue to the blue color in $im. 
    $blue = imagecolorallocate($im, 0, 0, 255);
    // Create a rectangle starting at (3, 15), the upper
    // left corner, that goes down to (390, 440),
    // the lower right corner. 
    imagerectangle($im, 3, 15, 390, 440);
    // Send the PNG content type header
    // so the browser knows what it's getting. 
    header('Content-Type: image/png');
    // Output the image to the browser.
    imagepng($im);
?>

ASP (JavaScript)

<%
    var gdImage = Server.CreateObject("GDLibrary.gdImage");
    gdImage.Create(500, 500);
    gdImage.ColorAllocate(255, 255, 255);
    var Blue = gdImage.ColorAllocate(0, 0, 255);
    gdImage.Rectangle(3, 15, 390, 440, Blue);
    Response.ContentType = "image/png";
    Response.BinaryWrite(gdImage.ToPngStream().Read);
%>

Example 2

Create a 230x20 image and write "My first Program with GD".

PHP

<?php
    header ("Content-type: image/png"); 
    $img_handle = ImageCreate (230, 20) or die ("Cannot Create image"); 
    $back_color = ImageColorAllocate ($img_handle, 0, 10, 10); 
    $txt_color = ImageColorAllocate ($img_handle, 233, 114, 191); 
    ImageString ($img_handle, 31, 5, 5,  
                 "My first Program with GD", $txt_color); 
    ImagePng ($img_handle); 
?>

ASP (JavaScript)

<%
    Response.ContentType = "image/png";
    var gdImage = Server.CreateObject("GDLibrary.gdImage");
    gdImage.Create(230, 20);
    gdImage.ColorAllocate(0, 10, 10);
    var TextColor = gdImage.ColorAllocate(233, 114, 191);
    gdImage.Chars(gdImage.FontGetLarge(), 5, 5, 
                 "My first Program with GD", TextColor);
    Response.BinaryWrite(gdImage.ToPngStream().Read);
%>

Example 3

Draw lines 10 pixels apart in a for loop.

PHP

<?php 
    Header("Content-type: image/png"); 
    $height = 300; 
    $width = 300; 
    $im = ImageCreate($width, $height); 
    $bck = ImageColorAllocate($im, 10,110,100); 
    $white = ImageColorAllocate($im, 255, 255, 255); 
    ImageLine($im, 0, 0, $width, $height, $white); 
    for($i=0;$i<=299;$i=$i+10) { 
    ImageLine($im, 0, $i, $width, $height, $white); }     
    ImagePNG($im); 
?>

ASP (JavaScript)

<%
    Response.ContentType = "image/png";
    var height = 300;
    var width = 300;
    var gdImage = Server.CreateObject("GDLibrary.gdImage");
    gdImage.Create(width, height);
    gdImage.ColorAllocate(10, 110, 100);
    var white = gdImage.ColorAllocate(255, 255, 255);
    gdImage.Line(0, 0, width, height, white);
    for (var i = 0; i < width; i += 10)
        gdImage.Line(0, i, width, height, white);
    Response.BinaryWrite(gdImage.ToPngStream().Read);
%>

Example 4

Draw an ellipse (oval)

PHP

<?php 
    Header("Content-type: image/png"); 
    $height = 300; 
    $width = 300; 
    $im = ImageCreate($width, $height); 
    $bck = ImageColorAllocate($im, 10,110,100); 
    $white = ImageColorAllocate($im, 255, 255, 255); 
    imageellipse ($im, 150, 150, 100, 200, $white); 
    ImagePNG($im); 
?>

ASP (JavaScript)

<%
    Response.ContentType = "image/png";
    var height = 300;
    var width = 300;
    var gdImage = Server.CreateObject("GDLibrary.gdImage");
    gdImage.Create(width, height);
    gdImage.ColorAllocate(10, 110, 100);
    var white = gdImage.ColorAllocate(255, 255, 255);
    gdImage.Ellipse(150, 150, 100, 200, white);
    Response.BinaryWrite(gdImage.ToPngStream().Read);
%>

Example 5

Load a PNG from file, if the file is not found, create a new image with the message "Image Not Found".

PHP

<? 
    header ("Content-type: image/png"); 
    $im = @ImageCreateFromPNG ("php.png"); 
    if(!$im) { 
        $img_handle = ImageCreate (200, 20) or die ("Cannot Create image"); 
        $back_color = ImageColorAllocate ($img_handle, 0, 0, 0); 
        $txt_color = ImageColorAllocate ($img_handle, 255, 255, 255); 
        ImageString ($img_handle, 10, 25, 5,  "Image Not Found", $txt_color); 
        ImagePng ($img_handle); } 
    Else { 
        echo "Image is Found"; } 
?>

ASP (JavaScript)

<%
    Response.ContentType = "image/png";
    var gdImage = Server.CreateObject("GDLibrary.gdImage");
    if (!gdImage.LoadFromFile(Server.MapPath("dynamicpng.png"))) {
        gdImage.Create(200, 20);
        gdImage.ColorAllocate(0, 0, 0);
        var TextColor = gdImage.ColorAllocate(255, 255, 255);
        gdImage.Chars(gdImage.FontGetMediumBold(), 25, 5, 
                              "Image Not Found", TextColor);
        Response.BinaryWrite(gdImage.ToPngStream().Read); }
    else {
        Response.Write("Image is Found"); }
%>

This wrapper was developed by Trevor Herselman. GD is copyright 2005 Boutell.com, Inc. GD was developed by Thomas Boutell.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Dade Murphy
Web Developer
South Africa South Africa
Professional award winning developer. He has been programming for over 14 years. With professional experience in C/C++ (Borland & Microsoft), Assembler (IA-32), VB6, ASP and SQL among others.

His programming interests include cryptography (AES, SSL, SHA etc.), compression algorithms (LZW etc.), AI (Genetic algorithms, Path finding and Neural Networks), WinSock, assembler (or lowest level development) and performance programming.

He is currently the Webmaster and senior developer for a multi-million dollar IT group.

You may also be interested in...

Comments and Discussions

 
QuestionServer.CreateObject Access Error Pin
David Jehoon Lee26-Feb-13 14:54
memberDavid Jehoon Lee26-Feb-13 14:54 
QuestionCan I use the Korean fonts? Pin
Member 835246226-Oct-11 22:07
memberMember 835246226-Oct-11 22:07 
Generalnew function resize e resample Pin
giaza8-Oct-09 7:28
membergiaza8-Oct-09 7:28 
GeneralHi Dade Pin
DaKhucBuon19-Jul-09 23:15
memberDaKhucBuon19-Jul-09 23:15 
Generalresize image and use jpg Pin
Atlantis788-Apr-07 11:08
memberAtlantis788-Apr-07 11:08 
QuestionTransparency, how do you do it? Pin
wvankuyk18-Dec-06 8:51
memberwvankuyk18-Dec-06 8:51 
QuestionHow to set this up? Pin
sp@k3-Nov-06 10:50
membersp@k3-Nov-06 10:50 
QuestionFonts Pin
jigas13-Sep-06 4:05
memberjigas13-Sep-06 4:05 
GeneralDetecting how many frames in GIF animation Pin
rbalboa11-Sep-06 7:12
memberrbalboa11-Sep-06 7:12 
Questiongdwrapper - Write file to disk Pin
jkirkerx4-Jun-06 11:18
memberjkirkerx4-Jun-06 11:18 
GeneralFile not found Pin
kenzoj18-Apr-06 2:19
memberkenzoj18-Apr-06 2:19 
GeneralRe: File not found Pin
petapetes3-May-06 5:03
memberpetapetes3-May-06 5:03 
GeneralRe: File not found Pin
JohnSmith3kk48du23-Sep-06 11:33
memberJohnSmith3kk48du23-Sep-06 11:33 
GeneralImageresizing Pin
xayide12-Apr-06 22:28
memberxayide12-Apr-06 22:28 
GeneralTTF(FT) not support,not support for chinese etc Pin
jiangjiayun5-Sep-05 17:17
memberjiangjiayun5-Sep-05 17:17 
GeneralAnother gd wrapper (VBScript) Pin
Lutz Tautenhahn9-Jun-05 10:12
memberLutz Tautenhahn9-Jun-05 10:12 
GeneralRe: Another gd wrapper (VBScript) Pin
Dade Murphy9-Jun-05 20:07
memberDade Murphy9-Jun-05 20:07 
GeneralASP Captcha Pin
CSShyamSundar3-Jun-05 21:17
sussCSShyamSundar3-Jun-05 21:17 
GeneralCannot work Pin
donno2028-May-05 20:46
memberdonno2028-May-05 20:46 
GeneralRe: Cannot work Pin
Dade Murphy29-May-05 20:11
memberDade Murphy29-May-05 20:11 
Generalgreat work! Pin
frumbert24-May-05 13:15
memberfrumbert24-May-05 13:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150819.1 | Last Updated 18 May 2005
Article Copyright 2005 by Dade Murphy
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid