龙空技术网

PHP 图像处理函数

寒笛过霜天 9

前言:

目前咱们对“php使图片旋转”大致比较关怀,大家都想要剖析一些“php使图片旋转”的相关知识。那么小编在网摘上网罗了一些有关“php使图片旋转””的相关文章,希望朋友们能喜欢,朋友们快快来学习一下吧!

(1)创建画布 --- 创建资源类型 --- 高度 宽度

resource imagecreate ( int x_size, int y_size ) 创建一个基于普通调色板的图像, 通常支持256色(png、gif、jepg等都支持)

resource imagecreatetruecolor ( int x_size, int y_size ) 创建一个真彩色的图像, 但不能用于gif格式

(2)绘制图像

制定各种颜色 int imagecolorallocate ( resource $image , int $red , int $green , int $blue )

用imagecreate()建立的图像, 也可以填充为背景颜色(注意:第一次调用即可为画布设置背景色),兼有代替函数imagefill的填充功能

(理解:第一次对 imagecolorallocate() 的调用会给基于调色板的图像填充背景色, 即用于imagecreate()建立的画布,但不能用于

imagecreatetruecolor()创建的画布)

图形区域填充 bool imagefill ( resource $image , int $x , int $y , int $color )

可以填充为背景颜色,只能用于imagecreatetruecolor()创建的真彩色图像;

矩形, 圆, 点, 线段, 扇形, 画字(字符, 字符串, freetype)

每一个图像对应一个函数

imagerectangle() 画一个矩形

bool imagerectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $col )

imagefilledrectangle() 画一矩形并填充

bool imagefilledrectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )

imageellipse() 画一个椭圆

bool imageellipse ( resource $image , int $cx , int $cy , int $w , int $h , int $color )

imagefilledellipse() 画一个椭圆并填充filled

bool imagefilledellipse ( resource $image , int $cx , int $cy , int $w , int $h , int $color )

imagesetpixel() — 画一个单一像素

bool imagesetpixel ( resource $image , int $x , int $y , int $color )

imageline() — 画一条线段

bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )

imagearc() — 画椭圆弧

bool imagearc ( resource $image , int $cx , int $cy , int $w , int $h , int $s , int $e , int $color )

imagearc() 以 cx,cy(图像左上角为 0, 0)为中心在 image 所代表的图像中画一个椭圆弧。

w和 h 分别指定了椭圆的宽度和高度,起始和结束点以 s 和 e参数以角度指定。0°位于三点钟位置,以顺时针方向绘画。

imagefilledarc() 画椭圆弧并填充

bool imagefilledarc ( resource $image , int $cx , int $cy , int $w , int $h , int $s , int $e , int $color , int $style )

style:

IMG_ARC_PIE 则产生圆形边界

IMG_ARC_CHORD 只是用直线连接了起始和结束点(IMG_ARC_PIE 和 IMG_ARC_CHORD是互斥的,如果两个都用,IMG_ARC_CHORD生效)。

IMG_ARC_NOFILL 指明弧或弦只有轮廓,不填充。

IMG_ARC_EDGED 指明用直线将起始和结束点与中心点相连,和 IMG_ARC_NOFILL一起使用是画饼状图轮廓的好方法(而不用填充)。

(chord:和弦 pie:馅饼(扇形图) nofill:不填充 edged: 有刃的;有边的)

imagepolygon() — 画一个多边形

bool imagepolygon ( resource $image , array $points , int $num_points , int $color )

imagefilledpolygon() — 画一多边形并填充

bool imagefilledpolygon ( resource $image , array $points , int $num_points , int $color )

<?php// 创建画布$image = imagecreatetruecolor(400, 300);// 填充背景颜色$bg = imagecolorallocate($image, 0, 0, 0);// 分配颜色$col_poly = imagecolorallocate($image, 255, 255, 255);// 绘制多边形imagepolygon($image,array (0, 0,100, 200,300, 200),3,$col_poly);// 输出图像header("Content-type: image/png");imagepng($image);?>

imagechar() — 水平地画一个字符

bool imagechar ( resource $image , int $font , int $x , int $y , string $c , int $color )

imagechar() 将字符串 c 的第一个字符画在 image 指定的图像中,其左上角位于 x,y(图像左上角为 0, 0),颜色为 color。

如果 font是 1,2,3,4, 5(1最小,5最大),则使用内置的字体(更大的数字对应于更大的字体)。

实例:

<?php$im = imagecreate ( 100 , 100 );$string = 'PHP' ;$bg = imagecolorallocate ( $im , 255 , 255 , 255 );$black = imagecolorallocate ( $im , 0 , 0 , 0 );imagechar ( $im , 1 , 0 , 0 , $string , $black );header ( 'Content-type: image/png' );imagepng ( $im );?>

imagecharup() — 垂直地画一个字符

bool imagecharup ( resource $image , int $font , int $x , int $y , string $c , int $color )

imagestring() — 水平地画一行字符串

bool imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col )

实例:

<?php// 建立一幅 100X30 的图像$im = imagecreate ( 100 , 30 );// 白色背景和蓝色文本$bg = imagecolorallocate ( $im , 255 , 255 , 255 );$textcolor = imagecolorallocate ( $im , 0 , 0 , 255 );// 把字符串写在图像左上角imagestring ( $im , 5 , 0 , 0 , "Hello world!" , $textcolor );// 输出图像header ( "Content-type: image/png" );imagepng ( $im );?>

imagestringup() — 垂直地画一行字符串

bool imagestringup ( resource $image , int $font , int $x , int $y , string $s , int $col )

注意:以上四个函数均不支持中文,支持中文则使用imagettftext()函数

imagettftext() — 用 TrueType 字体向图像写入文本

该函数与分辨率无关,输出时总是按照打印机的分辨率输出,无论是放大或缩小,字符总是光滑的,不会有锯齿出现。

array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )

size:字体的尺寸。根据 GD 的版本,为像素尺寸(GD1)或点(磅)尺寸(GD2)。(1磅等于4/3像素,约1.33像素)

angle:角度制表示的角度,0 度为从左向右读的文本。更高数值表示逆时针旋转。例如 90 度表示从下向上读的文本。

fontfile:是指要使用的 TrueType 字体的路径,很多情况下字体都放在脚本的同一个目录下。

imagettftext()函数只支持utf-8编码,所以如果创建的网页的编码格式使用GB2312,那么在应用imagettftext()函数输出中文字符串时,

必须应用iconv()函数将字符串的编码格式由GB2312转换为utf-8,否则在输出时将会出现乱码。

注意:如果网页是uft-8的编码,就没必要用iconv()函数,否则就会出现错误, $text=iconv("GB2312", "UTF-8", "LAMP编程--Linux操作系统!");

只能转出"LAMP",而后面的汉字"编程--Linux操作系统!"转不出来

(3)输出图像/保存处理好的图像

1.通过 header() 发送 Content-type: image/jpeg 可以使 PHP 脚本直接输出 JPEG 图像。

注意:header头不要在前面有任何输出 header("content-type:image/jpeg");

2. 输出各种类型(gif, png, jpeg)

imagegif(); 以 GIF 格式将图像输出到浏览器或文件

bool imagegif ( resource $image [, string $filename ] )

imagejpeg(); 以 JPEG 格式将图像输出到浏览器或文件

bool imagejpeg ( resource $image [, string $filename [, int $quality ]] )

imagejpeg() 从 image图像以 filename 为文件名创建一个 JPEG 图像。image参数是 imagecreatetruecolor() 函数的返回值。

filename:参数为可选,如果省略,则原始图像流将被直接输出。(如果保存图片的话,就不需要使用函数header()发送头信息)

要省略 filename 参数而提供 quality 参数, 使用空字符串('',注意不是空格字符' ')或者null。

quality:为可选项,范围从 0(最差质量,文件更小)到 100(最佳质量,文件最大),默认的质量值(大约 75)。

imagepng(); 以 PNG 格式将图像输出到浏览器或文件

bool imagepng ( resource $image [, string $filename ] )

(4)释放资源

imagedestroy() — 销毁一图像

bool imagedestroy ( resource $image )

<?php$im = imagecreatetruecolor(400, 30); //创建400x300像素大小的画布$white = imagecolorallocate($im, 255, 255, 255); //创建白色$grey = imagecolorallocate($im, 128, 128, 128); //创建灰色$black = imagecolorallocate($im, 0, 0, 0); //创建黑色imagefilledrectangle($im, 0, 0, 399, 29, $white); //输出一个使用白色填充的矩形作为背景//如果有中文输出,需要将其转码,转换为UTF-8的字符串才可以直接传递$text=iconv("GB2312", "UTF-8", "LAMP兄弟连--无兄弟,不编程!");$font = 'simsun.ttc'; //指定字体,将系统中与simsum.ttc对应的字体复制到当前目录下imagettftext($im, 20, 0, 12, 21, $grey, $font, $text); //输出一个灰色的字符串作为阴影imagettftext($im, 20, 0, 10, 20, $black, $font, $text); //在阴影之上输出一个黑色的字符串header("Content-type: image/png"); //通知浏览器将输出格式为PNG的图像imagepng($im); //向浏览器中输出PNG格式的图像imagedestroy($im); //销毁资源,释放内存占用的空间?>

图像处理第二种方法:

imagecreatefromjpeg() 从 JPEG 文件或 URL 新建一图像

resource imagecreatefromjpeg ( string $filename )

该函数返回一图像标识符, 代表了从给定的文件名取得的图像。

在失败时返回一个空字符串, 并且输出一条错误信息, 不幸地在浏览器中显示为断链接。

为减轻调试工作下面的例子会产生一个错误 JPEG:

<?phpfunction LoadJpeg($imgname){    $im = @imagecreatefromjpeg($imgname); /* Attempt to open */    if(!$im) { /* See if it failed */        $im = imagecreatetruecolor(150, 30); /* Create a blank image */        $bgc = imagecolorallocate($im, 255, 255, 255);        $tc = imagecolorallocate($im, 0, 0, 0);        imagefilledrectangle($im, 0, 0, 150, 30, $bgc);        /* Output an errmsg */        imagestring($im, 1, 5, 5, "Error loading $imgname", $tc);    }    return $im;}?>

imagecreatefrompng() — 从 PNG 文件或 URL 新建一图像

resource imagecreatefrompng ( string $filename )

imagecreatefromgif() -从 GIF 文件或 URL 新建一图像

resource imagecreatefromgif ( string $filename )

其他图像处理函数

获取图像的大小

imagesx() 获取画布的宽度

imagesy() 获取画布的高度

<?php$img = imagecreatetruecolor(300,200);echo imagesx($img);echo imagesy($img);?>

查看GD库信息

<?php//从服务器获取GD库的信息if(function_exists("gd_info")){    $gd = gd_info(); //gd_info()函数返回的数组    $gdinfo = $gd["GD Version"];}else{    $gdinfo="未知";}//从GD库中查看是否支持FreeType字体$freetype = $gd["FreeType"]?"支持":"不支持";echo $gdinfo;echo "<br />";echo $freetype;?>

标签: #php使图片旋转