【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));
}
【php学习】图片处理三步走的更多相关文章
- Python学习笔记(一)三步走安装pip
pip是用来方便地管理Python的第三方包的,由于此前玩Python仅仅是浅尝辄止,用的是python(x,y),但是这里并不代表你想用什么包都能从里面找到的,所以我把python(x,y)卸了,然 ...
- git 三步走
git三步走: git add . (注:别忘记后面的.,此操作是把Test文件夹下面的文件都添加进来) git commit -m "提交信息" (注:“提交 ...
- Knative 实战:三步走!基于 Knative Serverless 技术实现一个短网址服务
短网址顾名思义就是使用比较短的网址代替很长的网址.维基百科上面的解释是这样的: 短网址又称网址缩短.缩短网址.URL 缩短等,指的是一种互联网上的技术与服务,此服务可以提供一个非常短小的 URL 以代 ...
- 三步走起 提升 iOS 审核通过率 下篇
根据2015年的数据统计情况,并结合<苹果应用商店审核指南>,互娱 iOS 预审组通过细分将预审工作划为3大模块:客户端资源检查.应用内容检查和提审资源检查. 在上一篇文章中,Bugly ...
- mongodb安装和配置三步走
最近在重新学习node,所以和同事一起搞了个模仿新浪微博的项目,项目刚开始,所以其他的东西就暂时先不提.这里介绍下mongodb的安装.直接搜索可以看到很多介绍,但是我第一次是失败了,不过看了好几个还 ...
- 用powershell+excel行列转置三步走
本文重点讲解第一步,手动在excel表中输入公式,或者用powershell自动输入公式. 第二步,用powershell向excel中写入数据,略. 第三步,用powershell从excel中读取 ...
- 三步走起 提升 iOS 审核通过率 上篇
<ignore_js_op> Bugly 技术干货系列内容主要涉及移动开发方向,是由 Bugly 邀请腾讯内部各位技术大咖,通过日常工作经验的总结以及感悟撰写而成,内容均属原创,转载请标明 ...
- 学习 WebService 第三步:一个简单的实例(RAD+WAS 8.5开发SOAP项目)
[开发环境] Web Service 服务器端开发工具:RAD(Eclipse内核) Web Service 服务器:IBM WebSphere v8.5 REST/SOAP:SOAP(JAX-WS/ ...
- Java学习笔记一:三步搭建Java开发环境
Java开发环境搭建 一:安装JDK: 1.下载地址:http://www.oracle.com/technetwork/java/javase/downloads 非常显眼的下载界面 2.点击下载后 ...
随机推荐
- 2019-3-8-win10-uwp-渲染原理-DirectComposition-渲染
title author date CreateTime categories win10 uwp 渲染原理 DirectComposition 渲染 lindexi 2019-03-08 09:18 ...
- Linux下yum安装Redis
检验是否有yum源: [root@localhost ~]# yum install redis 显示没有软件包Redis,安装epel仓库(提供一些RHEL/CentOS默认不提供的软件包). [r ...
- element-ui隐藏组件el-scrollbar
代码如下: <div class="main_wrapper"> <el-scrollbar wrapClass="scrollar_container ...
- Java 参数的值传递和引用传递
在Java中,方法的参数的传递分为值传递(基本数据)和引用传递(引用数据:对象.字符串),这是最容易接受的.如果你能知道有这两种情况存在,那么,在遇到调用方法时,你可以避免很多问题的产生.但是,仔细查 ...
- 基于@AspectJ注解配置切面与基于XML配置切面
1. Waiter目标类 package com.smart.aop.advice.pointcut; public class Waiter { public void greetTo(String ...
- P1072 城市轰炸
题目描述 一个大小为N*M的城市遭到了X次轰炸,每次都炸了一个每条边都与边界平行的矩形. 在轰炸后,有Y个关键点,指挥官想知道,它们有没有受到过轰炸,如果有,被炸了几次,最后一次是第几轮. 输入格式 ...
- Redis - 命令行工具
使用Redis内置的命令行工具 redis-cli一些便捷的命令: 1.执行单条命令 平时在访问 Redis 服务器,一般都会使用 redis-cli 进入交互模式,然后一问一答来读写服务器,这种情况 ...
- printk函数 打印设备编号
偶尔地, 当从一个驱动打印消息, 你会想打印与感兴趣的硬件相关联的设备号. 打印主次 编号不是特别难, 但是, 为一致性考虑, 内核提供了一些实用的宏定义( 在 <linux/kdev_t.h& ...
- 【codeforces 764A】Taymyr is calling you
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- UVA - 11475 Extend to Palindrome (后缀数组)
Your task is, given an integer N, to make a palidrome (word that reads the same when you reverse it) ...