Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions
 Discussion Forums
 HTML, XML, JavaScript...
 Software Reviews
 Editors,Others...
 Top100
 JavaScript Tutorials, ...
 Tutorials
 ASP, CSS, Databases...
 Discussion List
 FAQ, Roundup, Configure ...
 Authoring
 HTML, JavaScript, CSS...
 Design
 Layout, Navigation,...
 Graphics
 Tools, Colors, Images...
 Software
 Browsers, Editors, XML...
 Internet
 Domains, E-Commerce, ...
 WDVL Resources
  Intermdiate, Tutorials,...
 WDVL
 Discussion Lists, Top 100,...
 Technology Jobs


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Top 10 Articles
  1. Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions
  2. JavaScript Tutorial for Programmers
  3. Design
  4. JavaScript Tutorial for Programmers - Objects
  5. JavaScript Tutorial for Programmers - JavaScript Grammar
  6. JavaScript Tutorial for Programmers - Versions of JavaScript
  7. Cascading Style Sheets
  8. JavaScript Tutorial for Programmers - Embedding JavaScript
  9. JavaScript Tutorial for Programmers - Functions
  10. Authoring JavaScript
Domain Name Lookup
Search to find the availability of a domain name. Just enter the complete domain name with extension (.com, .net, .edu)

Automating Image Manipulation with GD - Page 14

October 22, 2001

This is the first article in a series that will explore concepts and techniques that can automate many image manipulation tasks. As with the last series, the goal of this series is to reduce the amount of work that you do on a regular basis as a developer, administrator, or Webmaster.

The GD library

We will be utilizing the GD image manipulation library, which was written in C by Thomas Boutell. The Perl adaptation was written by Lincoln Stein.

The GD library is capable of drawing primitive shapes such as circles, squares, and lines. It's also capable of drawing text strings, as well as performing image manipulation such as combining and resizing images. The library can read and write JPEG, PNG, and XPM graphic formats. It supported GIF until Unisys, who owns the patent for the format, decided to start charging for the format's use. Because of that incident, Thomas Boutell decided to remove GIF support from the library.

Drawing borders

The first thing we'll learn to do is draw several different types of borders around an existing image. These exercises will help you become more familiar with the GD library syntax.

Drawing a 1-pixel border with rectangle()

In this first example, we will draw a black border around an image. With the GD library, we do this with the rectangle() function, which draws a rectangle given the starting x and y coordinates, the ending x and y coordinates, and the color of the border.

1  use GD;
2  my $image = GD::Image->newFromJpeg("66044.jpg");
3  my $black = $image->colorResolve(0,0,0);
4  my ($width,$height) = $image->getBounds();
5  $image->rectangle(0,0,($width-1),($height-1),$black);
6  open(FILE, ">border1.jpg") || die "Cannot open border1.jpg: $!\n";
7  print FILE $image->jpeg;

The first line loads the JPEG file 66044.jpg into a GD object. The second line allocates an index in the color map for black. Images can contain a maximum of 256 colors, so when modifying an existing image, you will either need to create a new mapping for a color, use an existing color in the map, or delete an existing color to make room for the new color. The colorResolve() function on the second line looks for the existence of a color in the color map (0,0,0 in this case which are the red, green, and blue values). If the color does not exist in the map, it will create it if there is room. You can check the number of colors in the map by calling the colorsTotal() function. The getBounds() function returns the width and height of the image that was just loaded. These values will be used to calculate the coordinates for the rectangle that is drawn on the next line.

The beginning coordinates for an image are 0,0. To draw the border, we call the rectangle() function. The first two arguments are the x,y coordinates where the rectangle will be drawn from. The second two parameters specify the ending two coordinates for the rectangle. The last parameter is the color map index for the color of the rectangle. The rectangle will be 1 pixel wide. The last two lines of the script write the new image with the border out to the border1.jpg file.

Drawing wide borders

Since the border is only 1 pixel wide, it may be a bit narrow to be noticeable. We can however draw a wider border by drawing multiple rectangles next to each other. The for loop below draws three consecutive rectangles, one inside of the other from the outer edge of the image.

1  use GD;
2  my $image = GD::Image->newFromJpeg("66044.jpg");
3  my $black = $image->colorResolve(0,0,0);
4  my ($width,$height) = $image->getBounds();
5  for (my $i=0; $i <= 3; $i++) {
6      $image->rectangle($i,$i,($width-1-$i),($height-1-$i),$black);
7  }
8  open(FILE, ">border2.jpg") || die "Cannot open border2.jpg: $!\n";
9  print FILE $image->jpeg;

The difference between the previous example and this one is the for loop on lines 5 through 7. The loop iterates 3 times, drawing a black rectangle one pixel wide in each direction for each iteration.

Processing URLs - Page 13
Weaving Magic With Regular Expressions
Using a Style to Draw a Dotted Line - Page 15


Up to => Home / Authoring / Languages / Perl / Weave




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers