01.php
<?php
/****
燕十八 公益PHP讲堂
论 坛: http://www.zixue.it
微 博: http://weibo.com/Yshiba
YY频道: 88354001
****/
/***
====笔记部分====
图像复制(水印)
图像半透明提制
图像的按比例复制(缩略)
***/
/*
bool imagecopy ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h )
*/
/**
把一幅小图复制到大图上,复制2份,形成证件照片的效果
小图:330*379
大图的宽 330*2+20, 高 379
**/
$sw = 330; // 小图的宽
$sh = 379; // 小图的高
// 创建大图的画面
$big = imagecreatetruecolor($sw*2+20,$sh);
// 创建灰色
$gray = imagecolorallocate($big,200,200,200);
// 填充大图
imagefill($big,0,0,$gray);
// 再读小图
$small = imagecreatefrompng('./feng.png');
// 复制小图
imagecopy($big,$small,0,0,0,0,330,379);
imagecopy($big,$small,$sw+20,0,0,0,330,379);
// 输出
header('content-type: image/png;');
imagepng($big);
// 销毁
imagedestroy($im);
02.php
<?php
/****
燕十八 公益PHP讲堂
论 坛: http://www.zixue.it
微 博: http://weibo.com/Yshiba
YY频道: 88354001
****/
/***
====笔记部分====
问:复制的图片能否小一点呢?
复制的图片能否带点透明效果呢?
答:能
imagecopyresampled
imagecopymerge
***/
/*
bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
复制图片并允许调整大小(可以做缩略图)
*/
$ow = 330; // 原图宽度
$oh = 379; // 原图高度
$nw = (int)$ow/2; // 缩略宽度
$nh = (int)$oh/2; // 缩略高度
// 创建缩略图画面
$dst = imagecreatetruecolor($nw,$nh);
// 读取原始图
$src = imagecreatefrompng('./feng.png');
// 复制完毕
imagecopyresampled($dst,$src,0,0,0,0,$nw,$nh,$ow,$oh);
// 输出
imagepng($dst,'./smallfeng.png');
imagedestroy($dst);
03.php
<?php
/****
燕十八 公益PHP讲堂
论 坛: http://www.zixue.it
微 博: http://weibo.com/Yshiba
YY频道: 88354001
****/
/***
====笔记部分====
透明复制
bool imagecopymerge ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h , int $pct )
***/
// 读取大图
$dst = imagecreatefromjpeg('home.jpg');
// 读取小图
$src = imagecreatefrompng('smallfeng.png');
imagealphablending ( $src , true);
imagealphablending ( $dst , true);
imagecopymerge($dst,$src,410,63,0,0,165,189,40);
echo imagejpeg($dst,'./ad.jpeg')?'OK':'FAIL';
04.php
<?php
/****
燕十八 公益PHP讲堂
论 坛: http://www.zixue.it
微 博: http://weibo.com/Yshiba
YY频道: 88354001
****/
/**
如何知道图片的大小和类型
因为在缩略图,不知道大小,我们无法确定比例
不知道类型,我们无法确认调用的函数,如 imagecreatefrompng/jpeg...
之前的学习 我们是人为的读出图片的宽和高
又通过后缀知道了类型,
相当于宽高,类型是已知的
我们既然准备写一个图片处理类,要处理的图片是各种大小,各种类型都可能的,
如何知道大小及功能???
引出一个重要函数 getimagesize()
**/
$arr = getimagesize('./feng.png');
//print_r($arr);
echo '你是',image_type_to_mime_type($arr[2]),'类型';
$arr = getimagesize('./home.jpg');
echo '你是',image_type_to_mime_type($arr[2]),'类型';
print_r($arr);
$arr = getimagesize('./01.php');
var_dump($arr);
/*
Array
(
[0] => 330 宽
[1] => 379 高
[2] => 3 图片类型(根据这个参数,知道该调用imagecreatefrompng/jpeg/gif)
[3] => width="330" height="379"
[bits] => 8
[mime] => image/png
)
*/
05.php
<?php
/****
燕十八 公益PHP讲堂
论 坛: http://www.zixue.it
微 博: http://weibo.com/Yshiba
YY频道: 88354001
****/
/***
想操作图片
先得把图片的大小,类型信息得到
水印:就是把指定的水印复制到目标上,并加透明效果
缩略图:就是把大图片复制到小尺寸画面上
***/
class ImageTool {
// imageInfo 分析图片的信息
// return array()
public static function imageInfo($image) {
// 判断图片是否存在
if(!file_exists($image)) {
return false;
}
$info = getimagesize($image);
if($info == false) {
return false;
}
// 此时info分析出来,是一个数组
$img['width'] = $info[0];
$img['height'] = $info[1];
$img['ext'] = substr($info['mime'],strpos($info['mime'],'/')+1);
return $img;
}
/*
加水印功能
parm String $dst 等操作图片
parm String $water 水印小图
parm String $save,不填则默认替换原始图
*/
public static function water($dst,$water,$save=NULL,$pos=2,$alpha=50) {
// 先保证2个图片存在
if(!file_exists($dst) || !file_exists($water)) {
return false;
}
// 首先保证水印不能比待操作图片还大
$dinfo = self::imageInfo($dst);
$winfo = self::imageInfo($water);
if($winfo['height'] > $dinfo['height'] || $winfo['width'] > $dinfo['width']) {
return false;
}
// 两张图,读到画布上! 但是图片可能是png,可能是jpeg,用什么函数读?
$dfunc = 'imagecreatefrom' . $dinfo['ext'];
$wfunc = 'imagecreatefrom' . $winfo['ext'];
if(!function_exists($dfunc) || !function_exists($wfunc)) {
return false;
}
// 动态加载函数来创建画布
$dim = $dfunc($dst); // 创建待操作的画布
$wim = $wfunc($water); // 创建水印画布
// 根据水印的位置 计算粘贴的坐标
switch($pos) {
case 0: // 左上角
$posx = 0;
$posy = 0;
break;
case 1: // 右上角
$posx = $dinfo['width'] - $winfo['width'];
$posy = 0;
break;
case 3: // 左下角
$posx = 0;
$posy = $dinfo['height'] - $winfo['height'];
break;
default:
$posx = $dinfo['width'] - $winfo['width'];
$posy = $dinfo['height'] - $winfo['height'];
}
// 加水印
imagecopymerge ($dim,$wim, $posx , $posy , 0 , 0 , $winfo['width'] , $winfo['height'] , $alpha);
// 保存
if(!$save) {
$save = $dst;
unlink($dst); // 删除原图
}
$createfunc = 'image' . $dinfo['ext'];
$createfunc($dim,$save);
imagedestroy($dim);
imagedestroy($wim);
return true;
}
/**
thumb 生成缩略图
等比例缩放,两边留白
**/
public static function thumb($dst,$save=NULL,$width=200,$height=200) {
// 首先判断待处理的图片存不存在
$dinfo = self::imageInfo($dst);
if($dinfo == false) {
return false;
}
// 计算缩放比例
$calc = min($width/$dinfo['width'], $height/$dinfo['height']);
// 创建原始图的画布
$dfunc = 'imagecreatefrom' . $dinfo['ext'];
$dim = $dfunc($dst);
// 创建缩略画布
$tim = imagecreatetruecolor($width,$height);
// 创建白色填充缩略画布
$white = imagecolorallocate($tim,255,255,255);
// 填充缩略画布
imagefill($tim,0,0,$white);
// 复制并缩略
$dwidth = (int)$dinfo['width']*$calc;
$dheight = (int)$dinfo['height']*$calc;
$paddingx = (int)($width - $dwidth) / 2;
$paddingy = (int)($height - $dheight) / 2;
imagecopyresampled($tim,$dim,$paddingx,$paddingy,0,0,$dwidth,$dheight,$dinfo['width'],$dinfo['height']);
// 保存图片
if(!$save) {
$save = $dst;
unlink($dst);
}
$createfunc = 'image' . $dinfo['ext'];
$createfunc($tim,$save);
imagedestroy($dim);
imagedestroy($tim);
return true;
}
}
// print_r(ImageTool::imageInfo('./home.jpg'));
/*
echo ImageTool::water('./home.jpg','./smallfeng.png','home1.jpg',0)?'OK':'FAIL';
echo ImageTool::water('./home.jpg','./smallfeng.png','home2.jpg',1)?'OK':'FAIL';
echo ImageTool::water('./home.jpg','./smallfeng.png','home3.jpg',2)?'OK':'FAIL';
echo ImageTool::water('./home.jpg','./smallfeng.png','home4.jpg',3)?'OK':'FAIL';
*/
echo ImageTool::thumb('./feng.png','./feng1.png',200,200)?'OK':'FAIL';
echo ImageTool::thumb('./feng.png','./feng2.png',200,300)?'OK':'FAIL';
echo ImageTool::thumb('./feng.png','./feng3.png',300,200)?'OK':'FAIL';