文字

GD 和图像处理 函数

Table of Contents

  • gd_info — 取得当前安装的 GD 库的信息
  • getimagesize — 取得图像大小
  • getimagesizefromstring — 从字符串中获取图像尺寸信息
  • image_type_to_extension — 取得图像类型的文件后缀
  • image_type_to_mime_type — 取得 getimagesize,exif_read_data,exif_thumbnail,exif_imagetype 所返回的图像类型的 MIME 类型
  • image2wbmp — 以 WBMP 格式将图像输出到浏览器或文件
  • imageaffine — 返回经过仿射变换后的图像,剪切区域可选
  • imageaffinematrixconcat — Concat two matrices (as in doing many ops in one go)
  • imageaffinematrixget — Return an image containing the affine tramsformed src image, using an optional clipping area
  • imagealphablending — 设定图像的混色模式
  • imageantialias — 是否使用抗锯齿(antialias)功能
  • imagearc — 画椭圆弧
  • imagechar — 水平地画一个字符
  • imagecharup — 垂直地画一个字符
  • imagecolorallocate — 为一幅图像分配颜色
  • imagecolorallocatealpha — 为一幅图像分配颜色 + alpha
  • imagecolorat — 取得某像素的颜色索引值
  • imagecolorclosest — 取得与指定的颜色最接近的颜色的索引值
  • imagecolorclosestalpha — 取得与指定的颜色加透明度最接近的颜色
  • imagecolorclosesthwb — 取得与给定颜色最接近的色度的黑白色的索引
  • imagecolordeallocate — 取消图像颜色的分配
  • imagecolorexact — 取得指定颜色的索引值
  • imagecolorexactalpha — 取得指定的颜色加透明度的索引值
  • imagecolormatch — 使一个图像中调色板版本的颜色与真彩色版本更能匹配
  • imagecolorresolve — 取得指定颜色的索引值或有可能得到的最接近的替代值
  • imagecolorresolvealpha — 取得指定颜色 + alpha 的索引值或有可能得到的最接近的替代值
  • imagecolorset — 给指定调色板索引设定颜色
  • imagecolorsforindex — 取得某索引的颜色
  • imagecolorstotal — 取得一幅图像的调色板中颜色的数目
  • imagecolortransparent — 将某个颜色定义为透明色
  • imageconvolution — 用系数 div 和 offset 申请一个 3x3 的卷积矩阵
  • imagecopy — 拷贝图像的一部分
  • imagecopymerge — 拷贝并合并图像的一部分
  • imagecopymergegray — 用灰度拷贝并合并图像的一部分
  • imagecopyresampled — 重采样拷贝部分图像并调整大小
  • imagecopyresized — 拷贝部分图像并调整大小
  • imagecreate — 新建一个基于调色板的图像
  • imagecreatefromgd2 — 从 GD2 文件或 URL 新建一图像
  • imagecreatefromgd2part — 从给定的 GD2 文件或 URL 中的部分新建一图像
  • imagecreatefromgd — 从 GD 文件或 URL 新建一图像
  • imagecreatefromgif — 由文件或 URL 创建一个新图象。
  • imagecreatefromjpeg — 由文件或 URL 创建一个新图象。
  • imagecreatefrompng — 由文件或 URL 创建一个新图象。
  • imagecreatefromstring — 从字符串中的图像流新建一图像
  • imagecreatefromwbmp — 由文件或 URL 创建一个新图象。
  • imagecreatefromwebp — 由文件或 URL 创建一个新图象。
  • imagecreatefromxbm — 由文件或 URL 创建一个新图象。
  • imagecreatefromxpm — 由文件或 URL 创建一个新图象。
  • imagecreatetruecolor — 新建一个真彩色图像
  • imagecrop — Crop an image using the given coordinates and size, x, y, width and height
  • imagecropauto — Crop an image automatically using one of the available modes
  • imagedashedline — 画一虚线
  • imagedestroy — 销毁一图像
  • imageellipse — 画一个椭圆
  • imagefill — 区域填充
  • imagefilledarc — 画一椭圆弧且填充
  • imagefilledellipse — 画一椭圆并填充
  • imagefilledpolygon — 画一多边形并填充
  • imagefilledrectangle — 画一矩形并填充
  • imagefilltoborder — 区域填充到指定颜色的边界为止
  • imagefilter — 对图像使用过滤器
  • imageflip — Flips an image using a given mode
  • imagefontheight — 取得字体高度
  • imagefontwidth — 取得字体宽度
  • imageftbbox — 给出一个使用 FreeType 2 字体的文本框
  • imagefttext — 使用 FreeType 2 字体将文本写入图像
  • imagegammacorrect — 对 GD 图像应用 gamma 修正
  • imagegd2 — 将 GD2 图像输出到浏览器或文件
  • imagegd — 将 GD 图像输出到浏览器或文件
  • imagegif — 输出图象到浏览器或文件。
  • imagegrabscreen — Captures the whole screen
  • imagegrabwindow — Captures a window
  • imageinterlace — 激活或禁止隔行扫描
  • imageistruecolor — 检查图像是否为真彩色图像
  • imagejpeg — 输出图象到浏览器或文件。
  • imagelayereffect — 设定 alpha 混色标志以使用绑定的 libgd 分层效果
  • imageline — 画一条线段
  • imageloadfont — 载入一新字体
  • imagepalettecopy — 将调色板从一幅图像拷贝到另一幅
  • imagepalettetotruecolor — Converts a palette based image to true color
  • imagepng — 以 PNG 格式将图像输出到浏览器或文件
  • imagepolygon — 画一个多边形
  • imagepsbbox — 给出一个使用 PostScript Type1 字体的文本方框
  • imagepsencodefont — 改变字体中的字符编码矢量
  • imagepsextendfont — 扩充或精简字体
  • imagepsfreefont — 释放一个 PostScript Type 1 字体所占用的内存
  • imagepsloadfont — 从文件中加载一个 PostScript Type 1 字体
  • imagepsslantfont — 倾斜某字体
  • imagepstext — 用 PostScript Type1 字体把文本字符串画在图像上
  • imagerectangle — 画一个矩形
  • imagerotate — 用给定角度旋转图像
  • imagesavealpha — 设置标记以在保存 PNG 图像时保存完整的 alpha 通道信息(与单一透明色相反)
  • imagescale — Scale an image using the given new width and height
  • imagesetbrush — 设定画线用的画笔图像
  • imagesetinterpolation — Set the interpolation method
  • imagesetpixel — 画一个单一像素
  • imagesetstyle — 设定画线的风格
  • imagesetthickness — 设定画线的宽度
  • imagesettile — 设定用于填充的贴图
  • imagestring — 水平地画一行字符串
  • imagestringup — 垂直地画一行字符串
  • imagesx — 取得图像宽度
  • imagesy — 取得图像高度
  • imagetruecolortopalette — 将真彩色图像转换为调色板图像
  • imagettfbbox — 取得使用 TrueType 字体的文本的范围
  • imagettftext — 用 TrueType 字体向图像写入文本
  • imagetypes — 返回当前 PHP 版本所支持的图像类型
  • imagewbmp — 以 WBMP 格式将图像输出到浏览器或文件
  • imagewebp — 将 WebP 格式的图像输出到浏览器或文件
  • imagexbm — 将 XBM 图像输出到浏览器或文件
  • iptcembed — 将二进制 IPTC 数据嵌入到一幅 JPEG 图像中
  • iptcparse — 将二进制 IPTC 块解析为单个标记
  • jpeg2wbmp — 将 JPEG 图像文件转换为 WBMP 图像文件
  • png2wbmp — 将 PNG 图像文件转换为 WBMP 图像文件

用户评论:

[#1] str_replace('o', 'x', 'nitrogen'); [2015-04-03 00:31:31]

I was after a technique to produce an image by using the image resources available to PHP and to have access to the image's binary data directly in PHP WITHOUT saving to a temporary file.

My purpose of this was to produce a directory of images with small image thumbnails (16x16) next to the filenames. Instead of having countless tiny images saved, I wanted to print out the image data into the <img> HTML tag (inline) using the data URI scheme.

I found no such code or suggestion for this, and I've done some digging around at likely approaches and finally came up with a solution. There may or may not be a better way of doing this, but this way works for me.

The guts of the approach started here with the example of registering a class as a stream wrapper: http://php.net/manual/en/stream.streamwrapper.example-1.php - this way we have access to, and can over-ride, the behaviour of "file" I/O with a custom wrapper.

<?php

// first, we define our class for the stream wrapper
class VariableStream {
  var 
$position;
  var 
$varname;

  function 
stream_open($path$mode$options, &$opened_path) {
    
$url parse_url($path);
    
$this->varname $url['host'];
    
$this->position 0;
    return 
TRUE;
  }

  function 
stream_write($data) {
    
$left substr(@$GLOBALS[$this->varname], 0$this->position);
    
$right substr(@$GLOBALS[$this->varname], $this->position+strlen($data));
    
$GLOBALS[$this->varname] = $left.$data.$right;
    
$this->position+= strlen($data);
    return 
strlen($data);
  }

  function 
stream_eof() {
    return 
$this->position>=strlen($GLOBALS[$this->varname]);
  }
}

// register the class as a stream wrapper ("var://asdf")
stream_wrapper_register('var''VariableStream');

// create a really simple dynamically-generated image
// in my case, I used imagecreatefromjpeg to create 16x16 thumbnails
$im imagecreatetruecolor(13036);
imagefill($im000xAAAAAA);
imagestring($im51010'Hello world!'0x0000FF);

// and output it to our wrapper!
imagepng($im'var://asdf/'); // $asdf now contains our image binary!
imagedestroy($im); // free memory

$asdf base64_encode($asdf); // base64 encode our data for...
printf("<img alt=\"\" src=\"data:image/png;base64,%s\"/>"$asdf); 

?>


The class 'VariableStream' lacks ability to seek, ftell, and read for the sake of simple code for my purpose. More complete code for I/O operations can be found in the example document I references above. You could also then use fopen using the wrapper and variable name to access it like a file again.

This technique can be applied to other functions that don't allow direct access to the data generated to files or sockets.

Hope this helps!

[#2] ingo at jache dot de [2011-10-14 03:16:17]

I know this might look somewhat superfluous to others, but i once came across a situation where i needed a *strong* blur on an image without having ImageMagick installed. Executing the convolution-filter several times on the same image is awfully slow and still doesn't give a good blur.

The function below accepts a truecolor-image and a blur-factor between 0.0 and 1.0. Beware: It's still quite slow.

<?php

    
function blurImage($srcimg,$blur)
    {
        
$blur $blur*$blur;
        
$blur max(0,min(1,$blur));
    
        
$srcw imagesx($srcimg);
        
$srch imagesy($srcimg);
        
        
$dstimg imagecreatetruecolor($srcw,$srch);
    
        
$f1a $blur;
        
$f1b 1.0 $blur;

    
        
$cr 0$cg 0$cb 0;
        
$nr 0$ng 0$nb 0;

        
$rgb imagecolorat($srcimg,0,0);
        
$or = ($rgb >> 16) & 0xFF;
        
$og = ($rgb >> 8) & 0xFF;
        
$ob = ($rgb) & 0xFF;

        
//-------------------------------------------------
        // first line is a special case
        //-------------------------------------------------
        
$x $srcw;
        
$y $srch-1;
        while (
$x--)
        {
            
//horizontal blurren
            
$rgb imagecolorat($srcimg,$x,$y);
            
$cr = ($rgb >> 16) & 0xFF;
            
$cg = ($rgb >> 8) & 0xFF;
            
$cb = ($rgb) & 0xFF;
    
            
$nr = ($cr $f1a) + ($or $f1b);
            
$ng = ($cg $f1a) + ($og $f1b);
            
$nb = ($cb $f1a) + ($ob $f1b);    

            
$or $nr;
            
$og $ng;
            
$ob $nb;
            
            
imagesetpixel($dstimg,$x,$y,($nr << 16) | ($ng << 8) | ($nb));
        }
        
//-------------------------------------------------

        //-------------------------------------------------
        // now process the entire picture
        //-------------------------------------------------
        
$y $srch-1;
        while (
$y--)
        {

            
$rgb imagecolorat($srcimg,0,$y);
            
$or = ($rgb >> 16) & 0xFF;
            
$og = ($rgb >> 8) & 0xFF;
            
$ob = ($rgb) & 0xFF;

            
$x $srcw;
            while (
$x--)
            {
                
//horizontal 
                
$rgb imagecolorat($srcimg,$x,$y);
                
$cr = ($rgb >> 16) & 0xFF;
                
$cg = ($rgb >> 8) & 0xFF;
                
$cb = ($rgb) & 0xFF;
                
                
$nr = ($cr $f1a) + ($or $f1b);
                
$ng = ($cg $f1a) + ($og $f1b);
                
$nb = ($cb $f1a) + ($ob $f1b);    
    
                
$or $nr;
                
$og $ng;
                
$ob $nb;
                
                
                
//vertical 
                
$rgb imagecolorat($dstimg,$x,$y+1);
                
$vr = ($rgb >> 16) & 0xFF;
                
$vg = ($rgb >> 8) & 0xFF;
                
$vb = ($rgb) & 0xFF;
                
                
$nr = ($nr $f1a) + ($vr $f1b);
                
$ng = ($ng $f1a) + ($vg $f1b);
                
$nb = ($nb $f1a) + ($vb $f1b);    
    
                
$vr $nr;
                
$vg $ng;
                
$vb $nb;
                
                
imagesetpixel($dstimg,$x,$y,($nr << 16) | ($ng << 8) | ($nb));
            }
        
        }
        
//-------------------------------------------------
        
return $dstimg;        

    }

    
    
$srcimg imagecreatefromjpeg("test.jpg");
    
$dstimg blurImage($srcimg,0.2);

    
header('Content-type: image/jpeg');
    echo( 
imagejpeg($dstimg) );
    exit();
    
    
?>

[#3] chuckstudios at gmail dot com [2008-11-25 21:18:26]

I wrote a simple function to convert an image resource to PGM (portable graymap) in order to feed it to an OCR program. It works just like the rest of the image output functions, and will convert to grayscale for you:

<?php
    
function imagepgm($image$filename null)
    {
        
$pgm "P5 ".imagesx($image)." ".imagesy($image)." 255\n";
        for(
$y 0$y imagesy($image); $y++)
        {
            for(
$x 0$x imagesx($image); $x++)
            {
                
$colors imagecolorsforindex($imageimagecolorat($image$x$y));
                
$pgm .= chr(0.3 $colors["red"] + 0.59 $colors["green"] + 0.11 $colors["blue"]);
            }
        }
        if(
$filename != null)
        {
            
$fp fopen($filename"w");
            
fwrite($fp$pgm);
            
fclose($fp);
        }
        else
        {
            return 
$pgm;
        }
    }
?>

[#4] dev at kingthief dot com [2008-03-28 08:20:39]

I've developed a well-documented, fairly rock-solid API for creating on the fly, anti-aliased, rounded corner images, including full alpha transparency support for all you PNG lovers.

go here to download the package:
http://sourceforge.net/projects/roundedphp/

go here for a live demo:
http://dev.kingthief.com/demos/roundedphp/

Installation is similar to PEAR.

Enjoy!

[#5] ph_corp at yahoo dot fr [2007-12-23 03:19:42]

<?php



function RBGtoHSL $R$G$B )
{

    
$var_R = ( $R 255 );
    
$var_G = ( $G 255 );
    
$var_B = ( $B 255 );

    
$var_Min min$var_R$var_G$var_B )
    
$var_Max max$var_R$var_G$var_B )
    
$del_Max $var_Max $var_Min

    $L 
= ( $var_Max $var_Min ) / 2;

    if ( 
$del_Max == )
    {
       
$H 0
       $S 
0
    
}
    else
    {
        if ( 
$L 0.5 )
        {
            
$S $del_Max / ( $var_Max $var_Min );
        }
        else
        {
            
$S $del_Max / ( $var_Max $var_Min );
        }

        
$del_R = ( ( ( $var_Max $var_R ) / ) + ( $del_Max ) ) / $del_Max;
        
$del_G = ( ( ( $var_Max $var_G ) / ) + ( $del_Max ) ) / $del_Max;
        
$del_B = ( ( ( $var_Max $var_B ) / ) + ( $del_Max ) ) / $del_Max;

        if ( 
$var_R == $var_Max )
        {
            
$H $del_B $del_G;
        }
        else if ( 
$var_G == $var_Max )
        {
            
$H = ( ) + $del_R $del_B;
        }
        else if ( 
$var_B == $var_Max )
        {
            
$H = ( ) + $del_G $del_R;
        }

        if ( 
$H )
        {
            
$H += 1;
        }
        if ( 
$H )
        {
            
$H -= 1
        
}

    }

    return array( 
$H$S$L );

}

function 
HuetoRGB$v1$v2$vH )
{
    if ( 
$vH )
    {
        
$vH += 1;
    }
    if ( 
$vH )
    {
        
$vH -= 1;
    }
    if ( ( 
$vH ) < )
    {
        return ( 
$v1 + ( $v2 $v1 ) * $vH );
    }
    if ( ( 
$vH ) < )
    {
        return ( 
$v2 );
    }
    if ( ( 
$vH ) < )
    {
        return ( 
$v1 + ( $v2 $v1 ) * ( ( ) - $vH ) * );
    }
    return ( 
$v1 )
}

function 
HSLtoRGB $H$S$L )
{

    if ( 
$S == )
    {
        
$R $L 255;
        
$G $L 255;
        
$B $L 255;
    }
    else
    {
        if ( 
$L 0.5 )
        {
            
$var_2 $L * ( $S );
        }
        else
        {
            
$var_2 = ( $L $S ) - ( $S $L );
        }

        
$var_1 $L $var_2;

        
$R 255 HuetoRGB$var_1$var_2$H + ( ) );
        
$G 255 HuetoRGB$var_1$var_2$H );
        
$B 255 HuetoRGB$var_1$var_2$H - ( ) );
    }

    return array( 
$R$G$B );

}

function 
distance $R1$G1$B1$R2$G2$B2 )
{
    
$result sqrt ( ( $R1 $R2 )*( $R1 $R2 ) + ( $G1 $G2 )*( $G1 $G2 ) + ( $B1 $B2 )*( $B1 $B2 ) );
    return ( 
$result );
}

?>

[#6] shd at earthling dot net [2006-03-28 04:44:43]

If you happen to need a way to output a Windows BMP file (e.g. when using the PEAR ExcelWriter), feel free to use the following code:

<?php
function imagebmp ($im$fn false)
{
    if (!
$im) return false;
            
    if (
$fn === false$fn 'php://output';
    
$f fopen ($fn"w");
    if (!
$f) return false;
            
    
//Image dimensions
    
$biWidth imagesx ($im);
    
$biHeight imagesy ($im);
    
$biBPLine $biWidth 3;
    
$biStride = ($biBPLine 3) & ~3;
    
$biSizeImage $biStride $biHeight;
    
$bfOffBits 54;
    
$bfSize $bfOffBits $biSizeImage;
            
    
//BITMAPFILEHEADER
    
fwrite ($f'BM'2);
    
fwrite ($fpack ('VvvV'$bfSize00$bfOffBits));
            
    
//BITMAPINFO (BITMAPINFOHEADER)
    
fwrite ($fpack ('VVVvvVVVVVV'40$biWidth$biHeight1240$biSizeImage0000));
            
    
$numpad $biStride $biBPLine;
    for (
$y $biHeight 1$y >= 0; --$y)
    {
        for (
$x 0$x $biWidth; ++$x)
        {
            
$col imagecolorat ($im$x$y);
            
fwrite ($fpack ('V'$col), 3);
        }
        for (
$i 0$i $numpad; ++$i)
            
fwrite ($fpack ('C'0));
    }
    
fclose ($f);
    return 
true;
}
?>


It works the same way as regular imagejpeg/imagepng do and only supports GD2.0 true colour bitmaps (which is what's required by ExcelWriter).

[#7] felipensp at gmail dot com [2006-03-12 21:17:37]

Representation decimal of a color in hexadecimal for use on functions of library GD.

<?php

    
// Representation hexadecimal
    
$var '#FFFFFF';

    function 
getRgbFromGd($color_hex) {

        return 
array_map('hexdec'explode('|'wordwrap(substr($color_hex1), 2'|'1)));

    }
    
    
print_r(getRgbFromGd($var));

    
// Output: Array ( [0] => 255 [1] => 255 [2] => 255 )

?>

[#8] peter dot hulstaert at gmail dot com [2006-01-05 17:18:08]

While I was searching for a good way to draw a graph, I stumbled on skumar2k15's script.

I have taken the liberty to improve multiple aspects of it.

1. The array can grow and shrink in size, the graph will adjust accordingly.
2. All the values in the array are recalculated so they won't get bigger than the height of the graph.
3. I inserted the possibility to keep a percentage off the height away from the edge.
4. You can adjust the size of the grid.
5. Everything will adjust when you change the height of width.

<?php 
header
("Content-type: image/png");

// Define variables
$Values=array(50,90,30,155,50,40,320,50,40,86,240,128,650,540,320);
$imgWidth=500;
$imgHeight=200;
$grid=25;
$graphspacing=0.05;

//Creation of new array with hight adjusted values
while (list($key$val) = each($Values)) 
    {if(
$val>$max){$max=$val;}}

for (
$i=0$i<count($Values); $i++){
$graphValues[$i] = $Values[$i] * (($imgHeight*(1-$graphspacing))/$max);
}
// Create image and define colors

$image=imagecreate($imgWidth$imgHeight);
$colorWhite=imagecolorallocate($image255255255);
$colorGrey=imagecolorallocate($image192192192);
$colorBlue=imagecolorallocate($image00255);

// Create border around image
imageline($image000$imgHeight$colorGrey);
imageline($image00$imgWidth0$colorGrey);
imageline($image$imgWidth-10$imgWidth-1$imgHeight-1$colorGrey);
imageline($image0$imgHeight-1$imgWidth-1$imgHeight-1$colorGrey);

// Create grid
for ($i=1$i<($imgWidth/$grid); $i++)
    {
imageline($image$i*$grid0$i*$grid$imgHeight$colorGrey);}
for (
$i=1$i<($imgHeight/$grid); $i++)
    {
imageline($image0$i*$grid$imgWidth$i*$grid$colorGrey);}

// Create line graph
if($imgWidth/$grid>count($graphValues)){$space=$grid;}
else{
$space $imgWidth/(count($graphValues)-1);}

for (
$i=0$i<count($graphValues)-1$i++)
    {
imageline($image$i*$space, ($imgHeight-$graphValues[$i]), ($i+1)*$space, ($imgHeight-$graphValues[$i+1]), $colorBlue);}

// Output graph and clear image from memory
imagepng($image);
imagedestroy($image);
?>

[#9] michal-ok at o2 dot pl [2005-10-18 14:29:58]

The image sharpen function (by Alex R. Austin) provided below seems to be very resource hungry and I couldn't make it work on two different servers - trying to sharpen a 413 x 413 image I ended up with "Fatal error: Allowed memory size of 8388608 bytes exhausted" or "Internal Server Error" or the script terminated without notice. Because I had no priviliges to change the default memory limit on these servers I started looking for other sharpen functions. I have come across a php Unsharp Mask function which works like a charm on both of the servers I dealt with. It can be found at http://vikjavev.no/hovudsida/umtestside.php.

[#10] mslemko [2005-10-09 12:09:53]

For fedora core 4 users that find that the gd library isn't installed, you can issue the command (as root)

# yum install php-gd

it should download and install the gd library. You will need to restart apache... phpinfo() should then tell you "GD Support  enabled".

[#11] timeshifting at gmail dot com [2005-08-24 15:39:09]

To sharpen an image, rather than using the code below that produces a sharpening filter with php, use the built-in GD function "imageconvolution" which is designed for this purpose. Matrices can be used for sharpening, blurring, edge detection, etc, ala Photoshop.

A sharpening example:

<?php

$sharpenMatrix 
= array(-1,-1,-1,-1,16,-1,-1,-1,-1);
$divisor 8;
$offset 0;

imageconvolution($myImage$sharpenMatrix$divisor$offset);

?>


Below is some information on building different kinds of matrices. (If you have photoshop (or PSP, GIMP) you can test out your matrices before applying them in PHP)

http://loriweb.pair.com/8udf-basics.html (covers blurs)
http://loriweb.pair.com/8udf-sharpen.html
http://loriweb.pair.com/8udf-edges.html
http://loriweb.pair.com/8udf-emboss.html

[#12] jeff at lushmedia dot com [2003-11-04 08:52:54]

I wrote an online overview of the image functions that people might find useful. In addition to a general overview of the various function categories and code samples, I have included many interactive examples of the functions, allowing viewers to experiment with the parameters, and seeing the results in real time. The presentation is located at New York PHP
http://www.nyphp.org/content/presentations/GDintro/

[#13] sellout at NoSpAm dot dharmadevil dot com [2003-04-16 10:06:15]

A fun little function to output UPC-A 11-digit barcodes.
Thanks to barcodeisland.com for the specs.

<?php

function UPCAbarcode($code) {
  
$lw 2$hi 100;
  
$Lencode = array('0001101','0011001','0010011','0111101','0100011',
                   
'0110001','0101111','0111011','0110111','0001011');
  
$Rencode = array('1110010','1100110','1101100','1000010','1011100',
                   
'1001110','1010000','1000100','1001000','1110100');
  
$ends '101'$center '01010';
  

  
if ( strlen($code) != 11 ) { die("UPC-A Must be 11 digits."); }
  

  
$ncode '0'.$code;
  
$even 0$odd 0;
  for (
$x=0;$x<12;$x++) {
    if (
$x 2) { $odd += $ncode[$x]; } else { $even += $ncode[$x]; }
  }
  
$code.=(10 - (($odd $even) % 10)) % 10;
  

  
$bars=$ends;
  
$bars.=$Lencode[$code[0]];
  for(
$x=1;$x<6;$x++) {
    
$bars.=$Lencode[$code[$x]];
  }
  
$bars.=$center;
  for(
$x=6;$x<12;$x++) {
    
$bars.=$Rencode[$code[$x]];
  }
  
$bars.=$ends;
  

  
$img ImageCreate($lw*95+30,$hi+30);
  
$fg ImageColorAllocate($img000);
  
$bg ImageColorAllocate($img255255255);
  
ImageFilledRectangle($img00$lw*95+30$hi+30$bg);
  
$shift=10;
  for (
$x=0;$x<strlen($bars);$x++) {
    if ((
$x<10) || ($x>=45 && $x<50) || ($x >=85)) { $sh=10; } else { $sh=0; }
    if (
$bars[$x] == '1') { $color $fg; } else { $color $bg; }
    
ImageFilledRectangle($img, ($x*$lw)+15,5,($x+1)*$lw+14,$hi+5+$sh,$color);
  }
  

  
ImageString($img,4,5,$hi-5,$code[0],$fg);
  for (
$x=0;$x<5;$x++) {
    
ImageString($img,5,$lw*(13+$x*6)+15,$hi+5,$code[$x+1],$fg);
    
ImageString($img,5,$lw*(53+$x*6)+15,$hi+5,$code[$x+6],$fg);
  }
  
ImageString($img,4,$lw*95+17,$hi-5,$code[11],$fg);
  

  
header("Content-Type: image/png");
  
ImagePNG($img);
}

UPCAbarcode('12345678901');

?>

上一篇: 下一篇: