PHP确实是一个简单的语言,PHP能够那么流行也是因为该语言能够从简单出发,用最简单的语句就实现功能,摒弃了过多的繁琐配置,寥寥几句就可以实现JAVA需要写一堆才能实现的功能;这个既是有点也是缺点,不过无论如何,PHP引领了一种潮流,就是简单,simple is all;
php操作图像最简单的,就是给图像加水印和加文字;
1,加水印:
$one_path = 'be.jpg';
$two_path = 's.png';
//创建图片的实例
$one = imagecreatefromstring(file_get_contents($one_path));
$two = imagecreatefromstring(file_get_contents($two_path));
//获取水印图片的宽高
list($two_w, $two_h) = getimagesize($two_path);
// 将水印图片复制到目标图片上,最后个参数50是设置透明度,这里实现半透明效果
// imagecopymerge($one, $two, 800, 300, 0, 0, $two_w, $two_h, 100);
// 如果水印图片本身带透明色,则使用imagecopy方法
imagecopy($one, $two, 800, 300, 0, 0, $two_w, $two_h);
//输出图片
list($one_w, $one_h, $one_type) = getimagesize($one_path);
switch ($one_type) {
case 1://GIF
header('Content-Type: image/gif');
imagegif($one);
break;
case 2://JPG
header('Content-Type: image/jpeg');
imagejpeg($one);
//$filename='mess.jpg';
//imagejpeg($one,$filename);
break;
case 3://PNG
header('Content-Type: image/png');
imagepng($one);
break;
default:
break;
}
imagedestroy($one);
imagedestroy($two);
imagejpeg($img,$filename,qulity):这个关键方法有三个参数,第一个是图像对象,第二个是图像保存路径,第三个是图像质量;
如果直接显示,只填第一个参数;如果保存成文件,填上第二个参数,比如D:/img/i.jpg,就会保存下来;
2,加文字:
$dst_path = 'be.jpg';
//创建图片的实例
$dst = imagecreatefromstring(file_get_contents($dst_path));
//打上文字
$font = './tt.ttf';//字体
$black = imagecolorallocate($dst, 0x00, 0x00, 0x00);//字体颜色
imagefttext($dst, 100, 0, 100, 500, $black, $font, '快乐编程');
//输出图片
list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path);
switch ($dst_type) {
case 1://GIF
header('Content-Type: image/gif');
imagegif($dst);
break;
case 2://JPG
header('Content-Type: image/jpeg');
imagejpeg($dst);
break;
case 3://PNG
header('Content-Type: image/png');
imagepng($dst);
break;
default:
break;
}
imagedestroy($dst);
imagefttext ( resource $image , float $size , float $angle , int $x , int $y ,int $col , string $font_file , string $text [, array $extrainfo ] ):这个函数是关键
3,使用jpgraph快速生产验证码
// Antispam example using a random string
require_once "jpgraph/jpgraph_antispam.php";
// Create new anti-spam challenge creator
// Note: Neither '0' (digit) or 'O' (letter) can be used to avoid confusion
$spam = new AntiSpam();
// Create a random 5 char challenge and return the string generated
$chars=$spam->Rand(5);
// var_dump($chars);
// Stroke random cahllenge
if( $spam->Stroke() === false ) {
die('Illegal or no data to plot');
}
?>
使用验证码antispamex01与水印图像:
YAN ZHEN MA
4,PHP给报告盖章,根据报告大小跳转图章大小(实际上使用的反向思维,图章大小不变,改变报告大小)
$one_path ='D:\检验报告\PicData\2013-11\280_1926_213101004_213101004-吡拉-1~1.jpg';
$one_path = iconv("UTF-8","gb2312",$one_path);
$two_path = '0.png';
//定义报告的缩放大小,W宽值
$ones=1240;
//定义印章的缩放大小;
$twos=300;
//创建图片的实例
$one = imagecreatefromstring(file_get_contents($one_path));
$two = imagecreatefromstring(file_get_contents($two_path));
//获取水印图片的宽高
list($one_w, $one_h) = getimagesize($one_path);
list($two_w, $two_h) = getimagesize($two_path);
$one_new_w=$ones;
//缩放:现在的高度h=原图宽度w*(现在的宽度w/原图的宽度w)
$one_new_h=$one_w*($one_new_w/$one_w);
//创建印章新图像
$w=$twos;
$h=$twos;
$two_new=imagecreatetruecolor($w, $h);
//分配颜色 + alpha,将颜色填充到新图上
$alpha = imagecolorallocatealpha($two_new, 0, 0, 0, 127);
imagefill($two_new, 0, 0, $alpha);
imagesavealpha($two_new, true);
//缩放
//关键函数,参数(目标资源,源,目标资源的开始坐标x,y, 源资源的开始坐标x,y,目标资源的宽高w,h,源资源的宽高w,h)
imagecopyresampled($two_new, $two, 0, 0, 0, 0, $w, $h, $two_w, $two_h);
// 将水印图片复制到目标图片上,最后个参数50是设置透明度,这里实现半透明效果
// imagecopymerge($one, $two_new, 800, 300, 0, 0, $two_w, $two_h, 10);
// 如果水印图片本身带透明色,则使用imagecopy方法
imagecopy($one, $two_new, 800, 400, 0, 0, $w, $h);
//输出图片
list($one_new_w, $one_new_h, $one_type) = getimagesize($one_path);
switch ($one_type) {
case 1://GIF
header('Content-Type: image/gif');
imagegif($one);
break;
case 2://JPG
header('Content-Type: image/jpeg');
// $filename='mess.jpg';
// imagejpeg($one,$filename);
imagejpeg($one);
break;
case 3://PNG
header('Content-Type: image/png');
imagepng($one);
break;
default:
break;
}
imagedestroy($one);
imagedestroy($two);