php4

创建图像

imagecreatetruecolor函数创建图像模型
imagecolorallocate 创建颜色为RGB类型
imagefill 填充图片
imagejpeg 保存图片
imagedestroy 销毁图片

<?php
header('Content-type:image/jpegs');//默认情况header('Content-type:text/html');
$img=imagecreatetruecolor(200,200);//新建一个长和高都为200像素的真彩色图像
$color1=imagecolorallocate($img,50,50,50);//分配颜色
$color2=imagecolorallocate($img,229,36,36);//分配颜色
$color3=imagecolorallocate($img,46,219,50);//分配颜色
imagefill($img,0,0,$color3);
if(imagejpeg($img,'./picture.jpeg')){//保存图像
	echo '保存成功!';
}
imagedestroy($img);
?>

生成验证码

<?php
header('Content-type:image/jpeg');
$width=120;
$height=40;
$element=array('a','b','c','d','e','f','g','h','i','j','k','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
$string='';
#生成随机的5个英文字母
for ($i=0;$i<5;$i++){
	$string.=$element[rand(0,count($element)-1)];
}
#新建一个长120和高40都为像素的真彩色图像
$img=imagecreatetruecolor($width, $height);
#分配颜色
$colorBg=imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));
$colorBorder=imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));
$colorString=imagecolorallocate($img,rand(10,100),rand(10,100),rand(10,100));
imagefill($img,0,0,$colorBg);
#生成边缘矩形框
imagerectangle($img,0,0,$width-1,$height-1,$colorBorder);
#生成100个随机点
for($i=0;$i<100;$i++){
	imagesetpixel($img,rand(0,$width-1),rand(0,$height-1),imagecolorallocate($img,rand(100,200),rand(100,200),rand(100,200)));
}
#生成3条线
for($i=0;$i<3;$i++){	imageline($img,rand(0,$width/2),rand(0,$height),rand($width/2,$width),rand(0,$height),imagecolorallocate($img,rand(100,200),rand(100,200),rand(100,200))); 
}
//imagestring($img,5,0,0,'abcd',$colorString);
#填充那5个字符
imagettftext($img,14,rand(-5,5),rand(5,15),rand(30,35),$colorString,'font/SketchyComic.ttf',$string);
imagejpeg($img);
imagedestroy($img);

?>

在这里插入图片描述
在这里插入图片描述

水印

imagecreatefromjpeg 载入jpg图片
imagecreatefromgif 载入gif图片
imagesx 返回图片宽度

<?php 
header('Content-type:image/jpeg');
$img=imagecreatefromjpeg('images/zcx.jpg'); #载入原图
$waterMark=imagecreatefromgif('images/watermark.gif'); #载入水印图片
$color=imagecolorallocate($img,255,255,255);

$width=imagesx($img);
$height=imagesy($img);

$waterMarkWidth=imagesx($waterMark);
$waterMarkHeight=imagesy($waterMark);

$position=imagettfbbox(20,0,'font/china1.TTF','小刚/周传雄');
$stringWidth=$position[2]-$position[0]; #右下角减去左下角
//文字水印
//imagettftext($img,20,0,$width-1-$stringWidth-($width/30),$height-1-($height/30), $color,'font/china1.TTF','小刚/周传雄');

/*
imagecopy($img,$waterMark,100,100,0,0,$waterMarkWidth,$waterMarkHeight);
参数说明:
$img:目标图像资源
$waterMark:水印的图像资源
100:所要拷贝到目标图像资源上面的坐标(x轴位置)
100:所要拷贝到目标图像资源上面的坐标(y轴位置)
0:从水印的图像资源的x坐标为0的位置开始拷贝
0:从水印的图像资源的y坐标为0的位置开始拷贝
$waterMarkWidth:所要拷贝的水印图像的长度
$waterMarkHeight:所要拷贝的水印图像的高度
*/

imagecopy($img,$waterMark,$width-1-$waterMarkWidth,$height-1-$waterMarkHeight,0,0,$waterMarkWidth,$waterMarkHeight);

imagejpeg($img);
imagedestroy($img);
?>

在这里插入图片描述
imagettfbbox在这里插入图片描述

图像裁剪

<?php
/*
等比例缩放
*/
header('Content-type:image/jpeg');
$width=300;


$img=imagecreatefromjpeg('images/zcx.jpg');


$imgWidth=imagesx($img);
$imgHeight=imagesy($img);

$height=$width/($imgWidth/$imgHeight);
$img1=imagecreatetruecolor($width,$height);
/*
imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)
参数说明:
$dst_image:目标图像资源
$src_image:源图像资源(你要采样的那个图像资源)
$dst_x:
$dst_y:与上面的$dst_x确定了一个坐标,把采样到的部分 放到目标图像资源的什么位置
$src_x:
$src_y:与上面的$src_y确定了一个坐标,你要采样的原图像资源的 某个部分的起始坐标
$dst_w:
$dst_h:与上面的$dst_w确定了 放到目标图像资源上面的尺寸
$src_w:
$src_h:与上面的$src_w确定了 采样原图像资源的 某个部分

*/
imagecopyresampled($img1,$img,0,0,0,0,$width,$height,$imgWidth,$imgHeight);
//裁剪
//imagecopyresampled($img1,$img,0,0,0,0,100,100,100,100);
if(imagejpeg($img1)){
	imagejpeg($img1,'images/zoom_zcx.jpg');
}
imagedestroy($img);
imagedestroy($img1);

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值