【php学习】图片操作

时间:2023-03-10 02:47:24
【php学习】图片操作

  前两天要对一张图片进行处理,其实很简单,就是在图片上加上字符串,一个图片而已,但是自己如同得了短暂性失忆似的,图片操作的函数一个都想不起来。所以就抽空整理了一下图片操作函数。

1. 创建画布

  从文件中创建一个新图像

  • imagecreatefromgif($filename)
  • imagecreatefromjpeg($filename)
  • imagecreatefrompng($filename)

  上面几个函数区别在于图片格式,知道了图片的格式就能选对函数。

     $type_arr = array(1=>'gif', 2=>'jpeg', 3=>'png');
//获取图片信息
list($width, $height, $type) = getimagesize($filename); //创建一个画布
$createFun = 'imagecreatefrom' . $type_arr[$type];
$im = $createFun($f1);

2. 图片处理

  图片处理的函数就是参数多,具体说明还是看文档的比较好!

  • imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col )
  • 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 )
  • 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 )

3. 保存图片并销毁画布

     //保存图片
$saveFun = 'image' . $type_arr[$type];
$saveFun($dst, $f2); //销毁图片
imagedestroy($im); imagedestroy($dst);

第一步和第三步几乎是固定的,拿来用就行了。

下面是自己写的图片处理函数

 /**
* 生成缩略图
* @param $f1 源图片
* @param $w 缩略图宽度
* @param $h 缩略图高度
* @param string $f2 缩略图
*/
function imgThumb($f1, $w, $h, $f2=''){
$type_arr = array(1=>'gif', 2=>'jpeg', 3=>'png');
//获取图片信息
list($width, $height, $type) = getimagesize($f1); //1. 创建画布
$createFun = 'imagecreatefrom' . $type_arr[$type];
$src = $createFun($f1);
$dst = imagecreatetruecolor($w, $h); //创建空白画布 //2. 复制图片
imagecopyresampled($dst, $src, 0, 0, 0, 0, $w, $h, $width, $height); //3. 保存图片并销毁画布
if(empty($f2)) $f2 = $f1;
$saveFun = 'image' . $type_arr[$type];
$saveFun($dst, $f2); imagedestroy($src);
imagedestroy($dst);
}
/**
* 给图片添加水印
* @param $f1 源图片
* @param $f2 水印图片
* @param int $coord 坐标,用数字表示,1左上角2右上角3左下角4右下角5上下居中6左右居中7全居中
* @param string $f3 目标图片
*/
function imgWater($f1, $f2, $coord=1, $f3=''){
$type_arr = array(1=>'gif', 2=>'jpeg', 3=>'png');
//获取图片信息
list($w1, $h1, $t1) = getimagesize($f1);
list($w2, $h2, $t2) = getimagesize($f2); //1. 创建画布
$createFun = 'imagecreatefrom' . $type_arr[$t1];
$im = $createFun($f1);
$createFun = 'imagecreatefrom' . $type_arr[$t2];
$waterIm = $createFun($f2); //2. 图片复制到另一张图片上
$px = 0; $py=0;
switch($coord){
case 1 :break;
case 2 :
$px = $w1-$w2;
break;
case 3 :
$py = $h1-$h2;
break;
case 4:
$px = $w1-$w2; $py=$h1-$h2;
break;
case 5:
$py=($h1-$h2)/2;
break;
case 6:
$px = ($w1-$w2)/2;
break;
case 7:
$px = ($w1-$w2)/2; $py=($h1-$h2)/2;
break;
}
imagecopy($im, $waterIm, $px, $py, 0, 0, $w2, $h2); //3. 保存图片并销毁画布
if(empty($f3)) $f3 = $f1;
$saveFun = 'image' . $type_arr[$t1];
$saveFun($im, $f3); imagedestroy($im);
imagedestroy($waterIm);
} /**
* 给图片添加文字
* @param $f 源图片
* @param $text 文字
* @param string $fc 文字颜色
* @param int $px 文字x坐标
* @param int $py 文字y坐标
* @param int $fs 文字字体,1,2,3,4,5表示内置字体
*/
function imgText($f, $text, $fc='#F00', $px=0, $py=0, $fs=5){
$type_arr = array(1=>'gif', 2=>'jpeg', 3=>'png');
//获取图片信息
list($width, $height, $type) = getimagesize($f); //1. 创建画布
$createFun = 'imagecreatefrom' . $type_arr[$type];
$im = $createFun($f); //2. 图片操作
//获取颜色
list($r, $g, $b) = rgbtodec($fc);
$color = imagecolorallocate($im, $r, $g, $b); //计算位置(默认居中)
if(empty($px) || empty($py)){
$px = ($width-imagefontwidth($fs) * strlen($text))/2;
$py = ($height-imagefontheight($fs))/2;
}
//写入字符
imagestring($im, $fs, $px, $py, $text, $color); //3. 保存图片并销毁画布
$saveFun = 'image' . $type_arr[$type];
$saveFun($im, $f); imagedestroy($im);
}
//rgb值转换十进制
function rgbtodec($str){
$str = str_replace('#', '', $str);
if(strlen($str)>4){
$r = substr($str, 0, 2);
$g = substr($str, 2, 2);
$b = substr($str, 4, 2);
}else{
$r = substr($str, 0, 1); $r .= $r;
$g = substr($str, 1, 1); $g .= $g;
$b = substr($str, 2, 1); $b .= $b;
}
return array(hexdec($r), hexdec($g), hexdec($b));
}