在 web 利用中常常会用到生成图片这一功效,在 php 教程 中创立图片须要 gd 库的支撑才干创立图形,有了这个图形功效,我们就能够便利的生成缩图,验证码,给图片加水印等。
在 php 中要安装 gd 库才干正常运行创立图片功效,方式以下,在 win 体系,找到 php.ini 把
;extension=php_gd2.dll 前面的 ";" 去了,重起 apache 就 OK 了。
下面我们一望实例
php 的 gd 库可以生成多种图象文件,如 gif,png,jpg,wbmp,xpm 等,下面来望一个生成正方形的文件。
演示图片:

<?php
$height = 300;
$width = 300;
// 创立布景图
$im = ImageCreateTrueColor($width, $height);
// 分派色彩
$white = ImageColorAllocate ($im, 255, 255, 255);
$blue = ImageColorAllocate ($im, 0, 0, 64);
// 绘制色彩至图象中
ImageFill($im, 0, 0, $blue);
// 绘制字符串: Hello,PHP
ImageString($im, 10, 100, 120, 'Hello,PHP', $white);
// 输出图象,界说头
Header ('Content-type: image/png');
// 将图象发送至阅读器
ImagePng($im);
// 肃清资本
ImageDestroy($im);
?>
查望成果只要阅读 php 文件就能够了,若是要图象挪用 <img src=a.php />
实例二,应用基础函数创立图片 imagecreate()
resource imagescreate(int x,inty)
imagedestroy 是放图片所占内存空间
int ingaedestroy( image)
imagecopy()
int imagecopy( dst_im,sr_im,int x,int y,int x,int y,)
下面来看实例:
<?php
header("Content-type: image/jpeg");
// 载入图象
$imagen1 = imagecreatefromjpeg("imagen1.jpg");
$imagen2 = imagecreatefromjpeg("imagen2.jpg");
// 复制图象
imagecopy($imagen1,$imagen2,0,0,0,0,200,150);
// 输出 jpeg 图象
imagejpeg($imagen1);
// 释放内存
imagedestroy($imagen2);
imagedestroy($imagen1);
?>
实例三在图片上图片文字
在这个功效上我们会用到 imageCreateFromJpeg 这个函数是,把来自文件或 form 的图片从头创立一次,
<?php resource imageCrrateFromJpge(string,imageName); imageCrateFromPng(); resource imageCrrateFromJpge(string,imageName); ?>
看实例:
<?php
//PNG 格局图象处置函数
function Loadpng ($imgname) {
$im = @ImageCreateFromPNG ($imgname);
if (!$im) { // 载入图象掉败
$im = ImageCreate (400, 30);
$bgc = ImageColorAllocate ($im, 255, 255, 255);
$tc = ImageColorAllocate ($im, 0, 0, 0);
ImageFilledRectangle ($im, 0, 0, 150, 30, $bgc);
ImageString($im, 4, 5, 5, "Error loading: $imgname", $tc);
}
return $im;
}
$imgPng=Loadpng("./karte.png");
/* 输出图象到阅读器 */
header("Content-type: image/png");
imagePng($imgPng);
?>
<?php $img = imageCreate(100, 100); $black = imageColorAllocate($img, 0, 0, 0); $white = imageColorAllocate($img, 255, 255, 255); $orange = imageColorAllocate($img, 255, 128, 64); $lightorange = imageColorAllocate($img, 255, 220, 164); imageFilledRectangle($img, 0, 0, 100, 100, $white); imageRectangle($img, 0, 0, 99, 99, $black); imageRectangle($img, 5, 5, 94, 94, $black); $points = Array(12,10, 20,15,15,20); $nump = (int) count($points)/2; imagePolygon($img, $points, $nump, $orange); imageLine($img, 17, 18, 27, 33, $orange); imageLine($img, 18, 18, 28, 33, $lightorange); imageLine($img, 19, 18, 29, 33, $orange); imageRectangle($img, 15, 33, 80, 75, $orange); imageFill($img, 14, 14, $lightorange); imageFill($img, 20, 40, $lightorange); imageString ($img, 2, 20, 40, "I'm a PHP", $black); imageString ($img, 2, 30, 55, "image", $black); imagePNG($img); ?>
在图象上写文字 imagechar imageloadfont imagefontheight, imagefontwidth 函数
<?php
$imagen = imagecreate(350,76);
$bg = imagecolorallocate($imagen,255,255,255);
$negro = imagecolorallocate($imagen,0,0,0);
/*
获得内置字体宽度和高度
*/
$w1 = imagefontwidth(1); // 字体 1 字体宽度
$h1 = imagefontheight(1); // 字体 1 字体高度
$w2 = imagefontwidth(2); // 字体 2 字体宽度
$h2 = imagefontheight(2); // 字体 2 字体高度
$w3 = imagefontwidth(3); // 字体 3 字体宽度
$h3 = imagefontheight(3); // 字体 3 字体高度
$w4 = imagefontwidth(4); // 字体 4 字体宽度
$h4 = imagefontheight(4); // 字体 4 字体高度
$w5 = imagefontwidth(5); // 字体 5 字体宽度
$h5 = imagefontheight(5); // 字体 5 字体高度
imagestring($imagen,1,0,0,"FontType 1: width ".$w1."px ,height y ".$h1."px ",$negro);
imagestring($imagen,2,0,11," FontType 2: width ".$w2."px ,height y ".$h2."px ",$negro);
imagestring($imagen,3,0,26," FontType 3: width ".$w3."px ,height y ".$h3."px ",$negro);
imagestring($imagen,4,0,42," FontType 4: width ".$w4."px ,height y ".$h4."px ",$negro);
imagestring($imagen,5,0,60," FontType 5: width ".$w5."px,height y ".$h5."px ",$negro);
/* 输出图象至阅读器 */
header("Content-type: image/gif");
imagegif($imagen);
imagedestroy($imagen);
?>
创立一个简略的图象
<?php
// 创立 100*30 图象
$im = imagecreate(100, 30);
// white background and blue text
$bg = imagecolorallocate($im, 200, 200, 200);
$textcolor = imagecolorallocate($im, 0, 0, 255);
// write the string at the top left
imagestring($im, 5, 0, 0, "Hello world!", $textcolor);
// output the image
header ("Content-type: image/jpeg");
imagejpeg ($im);
imagedestroy($im);
?>
php 创立一个 gif 图形
<?php
header("Content-type: image/gif");
$imagen = imagecreate(400,200);
$bgcolor = imagecolorallocate($imagen,230,230,230);
$azul = imagecolorallocate($imagen,0,140,255);
$negro = imagecolorallocate($imagen,0,0,0);
// Coordenadas del primer pentágono
$coords1 = array(100,25,24,80,53,170,147,170,
176,80);
// Coordenadas del segundo pentágono
$coords2 = array(300,25,224,80,253,170,347,170,376,80);
imagepolygon($imagen,$coords1,5,$negro);
imagefilledpolygon($imagen,$coords2,5,$azul);
imagegif($imagen);
imagedestroy($imagen);
?>
给图片布景颜色
<?php
header("Content-type: image/png");
$im = imagecreate(200, 200);
$red = imagecolorallocate($im, 255, 0, 0);
// create a shade of blue
$offblue = imagecolorallocate($im, 90, 90, 200);
imagefill($im, 0, 0, $red);
imagefilledrectangle($im, 10, 10, 40, 40, $offblue);
// get closest color to actual blue from image
$newblue = imagecolorclosest($im, 0, 0, 255);
imagefilledrectangle($im, 100, 100, 140, 140, $newblue);
imagepng($im);
imagedestroy($im);
?>
支付宝扫一扫
微信扫一扫
最新评论