GD 库包装器






2.67/5 (3投票s)
2005年5月18日
3分钟阅读

121675

1630
GD库封装 (适用于ASP开发者,包含PHP与ASP示例)。
在线演示.
(按F5查看新生成的“随机”GIF图片!)
引言
此封装由Trevor Herselman开发。GD版权归2005 Boutell.com, Inc.所有。GD由Thomas Boutell开发。
文件
- bgd.dll -- GD库二进制文件,可从这里下载。 注意:不是我的作品!!!
- GDLibrary.dll -- ActiveX 封装 DLL,在VB6中开发,包含完整源代码!使用前需要注册!
VB 源代码
- GDLibrary.vbp -- Visual Basic 6 项目文件。
- gdImage.cls -- 类模块文件。
- modGDLibrary.bas -- 标准 VB 模块文件,包含bgb.dll函数的VB函数声明。
- modAPI.bas -- 标准 VB 模块文件,包含一些Windows API函数的声明,例如
copyMemory
。
ASP 示例
- DynamicGif.asp -- 动态 GIF 示例。
- DynamicPng.asp -- 动态真彩色 PNG 示例。
- LoadPng.asp -- 加载 PNG 文件示例。
什么是 GD 库?
“GD 是一个开源代码库,供程序员动态创建图像。 GD 可以创建 PNG、JPEG 和 GIF 图像,通常用于动态生成图表、图形、缩略图、文本以及几乎任何东西。”
为什么需要另一个封装?
实际上,这是同类产品中的第一个,也是唯一可用于 ASP 开发人员的封装。 GD 是 PHP 4.3.x 的标准(内置)功能。
为什么要使用 GD?
GD 是一个非常成熟且稳定的项目。 它速度快且易于使用! PHP 开发人员可以很容易地看到相似之处,而 ASP 开发人员通过使用类似的代码可以更平稳地过渡到 PHP! 只需语法上的差异,即可利用为 PHP 编写的教程!
为什么要使用此封装?
可以在少于 5 行代码中创建/加载、绘制、保存到文件或发送到浏览器的图像! ASP 开发人员有几个优势,例如能够直接在 SQL 中读/写BLOB
或Image
类型。 此封装创建一个ADODB.Stream
(ADO 2.8)对象供内部使用,并且客户端完全可以使用(object.Stream
),并具有Stream
对象的所有特性和功能!
GD 可以做什么?
- 创建调色板/真彩色图像
GetPixel
,SetPixel
- 绘制线条、矩形、弧线
- 填充区域到边界
- 填充弧线、填充椭圆
- 写入水平/垂直文本
封装还能做什么?
渐变填充(水平和垂直),从/向文件、内存或数据库(使用 ADO 2.8 Stream
对象)加载/保存。 将动态图像直接输出到客户端。
当前封装的局限性?
要绘制各种字体,GD 使用 FreeType 库,我在函数声明方面遇到问题。 我在可变长度多边形绘图函数的声明方面遇到问题。
示例 1
创建一个 500x500 的 PNG 图像并绘制一个蓝色矩形。
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);
%>
示例 2
创建一个 230x20 的图像并写入 "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);
%>
示例 3
在 for
循环中以 10 像素的间隔绘制线条。
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);
%>
示例 4
绘制一个椭圆(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);
%>
示例 5
从文件加载 PNG,如果找不到该文件,则创建一个新图像,并显示消息 "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"); }
%>
此封装由Trevor Herselman开发。GD版权归2005 Boutell.com, Inc.所有。GD由Thomas Boutell开发。