PHP动态图像处理 原创
创建新图片
画图
<?php
header('content-type:text/html;charset=utf-8');
//1-1.创建一个基于调色板的画布
$img1 = imagecreate(400,400);
//1-2.创建一个真彩色画布
$img2 = imagecreatetruecolor(400,400);
//2.创建一个颜色盒
$red = imagecolorallocate($img2,255,0,0);
$green = imagecolorallocate($img1,0,255,0);
$blue = imagecolorallocate($img2,0,0,255);
$white = imagecolorallocate($img2,255,255,0);
//1-1-1用颜色盒填充真彩色画布
imagefill($img2, 0, 0, $white);
//3.创建各种图形和线段
//画一条线段
imageline($img2, 50, 100, 200, 300, $blue);
//画一条虚线线段
imagedashedline($img2,60,100,210,300,$blue);
//画一个圆
imageellipse($img2,50,100,50,50,$blue);
//画个椭圆
imageellipse($img2,50,100,80,50,$blue);
//画个实心圆
imagefilledellipse($img2,200,110,80,80,$blue);
//画个矩形
imagerectangle($img2,60,60,200,200,$blue);
//画个实心矩形
imagefilledrectangle($img2,200,200,300,300,$blue);
//画一个多边形
$arr6 = array(
40, 50, // Point 1 (x, y)
20, 240, // Point 2 (x, y)
60, 60, // Point 3 (x, y)
240, 20, // Point 4 (x, y)
50, 40, // Point 5 (x, y)
10, 10 );
$arrfill = array(
50, 50, // Point 1 (x, y)
30, 240, // Point 2 (x, y)
70, 60, // Point 3 (x, y)
250, 20, // Point 4 (x, y)
60, 40, // Point 5 (x, y)
20, 10 );
//画一个不填充多边
imagepolygon($img2,$arr6,6,$blue);
//画一个填充的多边形
imagefilledpolygon($img2, $arrfill, 6, $blue);
//画一个弧形
imagearc($img2, 280, 280, 80, 80, 0, 90, $blue);
//画一个填充圆弧
imagefilledarc($img2, 280, 159, 100, 100, 0, 160, $blue, IMG_ARC_PIE);
//创建图形文字
imagestring($img2, 5,129, 380, 'miss you', $blue);
//创建图形文字垂直排序
imagestringup($img2, 6, 149, 350, 'miss you', $blue);
//使用truetype字体向图像写入文本 字体位置要正确
imagettftext($img2, 15, 10, 168,330, $blue, 'STHUPO.TTF', '我是中国人');
//4-1输出到页面(只能同时存在一个4) 输出文件类型必须一致
// header('content-type:image/jpeg');
//header('content-type:image/png');
//header('content-type:image/gif');
// imagejpeg($img2);
// imagepng($img2);
// imagegif($img2);
//4-2保存文件(只能同时存在一个4)
imagejpeg($img2,'cc.jpg');
//5.销毁图像资源
imagedestroy($img2);
imagedestroy($img1);
验证码实例
<?php
header('content-type:text/html;charset=utf-8');
function vido($size=4,$type=3){
$width = $size*18+10;
$img = imagecreatetruecolor($width,30);
$bgcolor = imagecolorallocate($img,225,225,225);
imagefill($img,0,0,$bgcolor);
for($i = 0; $i < 180+($width-80)*10; $i++){
$colordian = imagecolorallocate($img,rand(0,210),rand(0,210),rand(0,210));
imagesetpixel($img, rand(0,$width), rand(0,$width), $colordian);
}
for($j = 0; $j < 10+($width-80)/6; $j++){
$colorline = imagecolorallocate($img,rand(50,210),rand(50,210),rand(50,210));
imageline($img, rand(0,$width), rand(0,30), rand(0,$width), rand(0,30), $colorline);
}
$arr = array(
'ABCDEFGHJKMNPQRSTWSYZ',
'abcdefghjkmnpqrstwsyz',
'23456789',
'ABCDEFGHJKMNPQRSTWSYZabcdefghjkmnpqrstwsyz23456789',
);
$str = substr(str_shuffle($arr[$type]),0,$size);
for ($n=0; $n < strlen($str) ; $n++) {
$colorstr = imagecolorallocate($img,rand(0,189),rand(0,189),rand(0,189));
imagettftext($img, 19, rand(5,13),$n*16+12 , 25, $colorstr, 'simhei.ttf', $str[$n]);
}
header('content-type:image/png');
imagepng($img);
imagedestroy($img);
}
vido(8);
处理原有图片
图片的缩放
图像的固定宽度缩放(不推荐)
<?php
header('content-type:text/html;charset=utf-8');
date_default_timezone_set('PRC');
//1.打开一个已有图像
$img = imagecreatefromjpeg('2.jpg');
//2-1获得图片的宽和高
$width = imagesx($img);
$height = imagesy($img);
//2-2获取图片的宽高第二种方式
//传入一张图片来来获取图片的放入到数组里面 宽 高 类型等
//$imgmess = getimagesize('2.jpg');
//$width = $imgmess[0];
//$height = $imgmess[1];
//3.创建一个为目标缩放固定大小的画布
//图片容易比较不符而变形。
$img2 = imagecreatetruecolor(400,400);
//4.把原图片和裁切图片进行裁切
imagecopyresampled($img2,$img,0,0,0,0,400,400,$width,$height);
//5-1.输出图像到网页保存到本地。
header('content-type:image/jpeg');
imagejpeg($img2);
//5-2.保存使用此函数
//imagejpeg($imgs,'ccc.jpg')
//销毁图像资源。
imagedestroy($img);
imagedestroy($img2);
图像等比缩放
<?php
header('content-type:text/html;charset=utf-8');
date_default_timezone_set('PRC');
//1.打开一个已有图像
$img = imagecreatefromjpeg('2.jpg');
//2-1获得图片的宽和高
$width = imagesx($img);
$height = imagesy($img);
//2-2获取图片的宽高第二种方式
//传入一张图片来来获取图片的放入到数组里面 宽 高 类型等
//$imgmess = getimagesize('2.jpg');
//$width = $imgmess[0];
//$height = $imgmess[1];
//3.创建一个为目标缩放大小的画布
$n_w = $width*0.5;
$n_h = $height*0.5;
$img2 = imagecreatetruecolor($n_w,$n_h);
//4.把原图片和裁切图片进行裁切
imagecopyresampled($img2,$img,0,0,0,0,$n_w,$n_h,$width,$height);
//5-1.输出图像到网页保存到本地。
header('content-type:image/jpeg');
imagejpeg($img2);
//5-2.保存使用此函数
//imagejpeg($imgs,'ccc.jpg')
//销毁图像资源。
imagedestroy($img);
imagedestroy($img2);
图像固定宽度等比缩放(推荐)
<?php
header('content-type:text/html;charset=utf-8');
date_default_timezone_set('PRC');
function suofang($filename,$n_w = 400){
//1.打开一个已有图像
$img = imagecreatefromjpeg($filename);
//2-1获得图片的宽和高
$width = imagesx($img);
$height = imagesy($img);
//2-2获取图片的宽高第二种方式
//传入一张图片来来获取图片的放入到数组里面 宽 高 类型等
//$imgmess = getimagesize('2.jpg');
//$width = $imgmess[0];
//$height = $imgmess[1];
//设定固定宽高
$n_h = &$n_w;
//判断 缩放比例以最长的一边为参照进行缩放
if($width > $height){
$bili = round($n_w/$width,2);
}else{
$bili = round($n_h/$height,2);
}
$new_w = $width*$bili;
$new_h = $height*$bili;
//3.创建一个为目标缩放固定大小的画布
//图片容易比较不符而变形。
$img2 = imagecreatetruecolor($new_w,$new_h);
//4.把原图片和裁切图片进行裁切
imagecopyresampled($img2,$img,0,0,0,0,$new_w,$new_h,$width,$height);
//5-1.输出图像到网页保存到本地。
//header('content-type:image/jpeg');
//imagejpeg($img2);
//5-2.保存使用此函数
imagejpeg($img2,'small'.$filename);
//销毁图像资源。
imagedestroy($img);
imagedestroy($img2);
}
suofang('2.jpg',800);
图像旋转
<?php
header('content-type:text/html;charset=utf-8');
date_default_timezone_set('PRC');
$img = imagecreatefromjpeg('2.jpg');
$red = imagecolorallocate($img,255,255,0);
$img2 = imagerotate($img,185,$red,90);
header('content-type:image/jpeg');
imagejpeg($img2);
imagedestroy($img);
imagedestroy($img2);
图像翻转
垂直翻转
$black = imagecreatefromjpeg('2.jpg');
$width = imagesx($black);
$height = imagesy($black);
$new = imagecreatetruecolor($width,$height);
for($y = 0; $y < $height; $y++){
imagecopy($new,$black,0,$height-$y-1,0,$y,$width,1);
}
header('content-type:image/jpeg');
imagejpeg($new);
imagedestroy($new);
imagedestroy($black);
水平翻转
$black = imagecreatefromjpeg('2.jpg');
$width = imagesx($black);
$height = imagesy($black);
//创建一个画布
$new = imagecreatetruecolor($width,$height);
//通过循环语句把图片一个像素高度或宽度复制一遍。形成新的图片
for($y = 0; $y < $width; $y++){
//返回bool型的值。
//34参数是复制到新新画布上的位置
//56参数是在就图像什么位置开始
//78参数是每次取多少像素
//水平翻转 每次取一个像素高度进行复制
//垂直翻转 每次取一个像素宽度进行复制
imagecopy($new,$black,$width-$y-1,0,$y,0,1,$height);
}
header('content-type:image/jpeg');
imagejpeg($new);
imagedestroy($new);
imagedestroy($black);
图像的裁剪
//打开一个已有图像
$img = imagecreatefromjpeg('2.jpg');
//获取宽和高
$width = imagesx($img);
$height = imagesy($img);
//创建一个画布宽200 高200
$img2 = imagecreatetruecolor(200, 200);
//把$img2作为背景。 把$img拷贝上去。
//34参数拷贝在$img2从什么位置开始放置
//56参数原图片在什么位置开始截取
//78在$img2画布上占用的宽和高
//9 10 参数 在$img 上剪切的宽和高
imagecopyresampled($img2, $img, 10, 10, 20, 20, 300, 400, 600, 300);
header('content-type:image/jpeg');
imagejpeg($img2);
imagedestroy($img);
imagedestroy($img2);
图像的加水印
<?php
header('content-type:text/html;charset=utf-8');
date_default_timezone_set('PRC');
function shuiyin($filename='2.jpg',$wz=1){
$img = imagecreatefromjpeg($filename);
$width = imagesx($img);
$height = imagesy($img);
$img2 = imagecreatefromjpeg('hongchun.jpg');
$o_w = imagesx($img2);
$o_h = imagesy($img2);
//用switch 语句来控制水印在图片中的位置。
switch($wz){
case 1:
$x = 10;
$y = 10;
break;
case 2:
$x = ($width-$o_w)/2;
$y = 10;
break;
case 3:
$x = $width-$o_w-10;
$y = 10;
break;
case 4:
$x = 10;
$y = ($height-$o_h)/2;
break;
case 5:
$x = ($width-$o_w)/2;
$y = ($height-$o_h)/2;
break;
case 6:
$x = $width-$o_w-10;
$y = ($height-$o_h)/2;
break;
case 7:
$x = 10;
$y = $height-$o_h-10;
break;
case 8:
$x = ($width-$o_w)/2;
$y = $height-$o_h-10;
break;
default:
$x = $width-$o_w-10;
$y = $height-$o_h-10;
break;
}
//设置水印复杂在图片中的位置。
imagecopyresampled($img,$img2,$x,$y,0,0,$o_w,$o_h,$o_w,$o_h);
header('content-type:image/jpeg');
imagejpeg($img);
imagedestroy($img);
imagedestroy($img2);
}
shuiyin('2.jpg',9);