一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

php图形处理教程-php生成图片

时间:2010-06-06 编辑:简简单单 来源:一聚教程网

在web应用中经常会用到生成图片这一功能,在php教程 中创建图片需要gd库的支持才能创建图形,有了这个图形功能,我们就可以方便的生成缩图,验证码,给图片加水印等。

在php中要安装gd库才能正常运行创建图片功能,方法如下,在win系统,找到php.ini把

;extension=php_gd2.dll前面的";"去了,重起apache就OK了。

下面我们一看实例

php的gd库可以生成多种图像文件,如gif,png,jpg,wbmp,xpm等,下面来看一个生成正方形的文件。

$;
$;
//创建背景图
$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文件就可以了,如果要图像调用

实例二,使用基本函数创建图片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,)

 

下面来看实例

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的图片重新创建一次,

resource imageCrrateFromJpge(string,imageName);

imageCrateFromPng();

resource imageCrrateFromJpge(string,imageName);

看实例

//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);
 ?>

 

$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函数

$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);
?>

 

创建一个简单的图像

  //创建 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图形

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);
?>

给图片背景色彩

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);
?>

热门栏目