php图像处理函数大全(推荐收藏)


Posted in PHP onJuly 11, 2013

一、创建图片资源
imagecreatetruecolor(width,height);
imagecreatefromgif(图片名称);
imagecreatefrompng(图片名称);
imagecreatefromjpeg(图片名称);画出各种图像 imagegif(图片资源,保存路径);
imagepng()
imagejpeg();

二、获取图片属性
imagesx(res//宽度
imagesy(res//高度
getimagesize(文件路径)
返回一个具有四个单元的数组。索引 0 包含图像宽度的像素值,索引 1 包含图像高度的像素值。索引 2 是图像类型的标记:1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,7 = TIFF(intel byte order),8 = TIFF(motorola byte order),9 = JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM。这些标记与 PHP 4.3.0 新加的 IMAGETYPE 常量对应。索引 3 是文本字符串,内容为“height="yyy" width="xxx"”,可直接用于 IMG 标记。
销毁图像资源
imagedestroy(图片资源);

三、透明处理
PNG、jpeg透明色都正常,只有gif不正常
imagecolortransparent(resource image [,int color])//将某个颜色设置成透明色
imagecolorstotal()
imagecolorforindex();

四、图片的裁剪
imagecopyresized()
imagecopyresampled();

五、加水印(文字、图片)
字符串编码转换string iconv ( string $in_charset , string $out_charset , string $str )

六、图片旋转
imagerotate();//制定角度的图片翻转

七、图片的翻转
沿X轴 沿Y轴翻转

八、锐化
imagecolorsforindex()
imagecolorat()
在图片上画图形 $img=imagecreatefromgif("./images/map.gif");

<?PHP
 /**
 * 图片锐化处理
 */
 $red= imagecolorallocate($img, 255, 0, 0); imageline($img, 0, 0, 100, 100, $red);
 imageellipse($img, 200, 100, 100, 100, $red);
 imagegif($img, "./images/map2.gif");
 imagedestroy($img);
 图片普通缩放
 代码如下:
 $filename="./images/hee.jpg";
 $per=0.3;
 list($width, $height)=getimagesize($filename);
 $n_w=$width*$per;
 $n_h=$width*$per;
 $new=imagecreatetruecolor($n_w, $n_h);
 $img=imagecreatefromjpeg($filename);
 //拷贝部分图像并调整
 imagecopyresized($new, $img,0, 0,0, 0,$n_w, $n_h, $width, $height);
 //图像输出新图片、另存为
 imagejpeg($new, "./images/hee2.jpg");
 imagedestroy($new);
 imagedestroy($img);
 图片等比例缩放、没处理透明色
 代码如下:
 function thumn($background, $width, $height, $newfile) {
 list($s_w, $s_h)=getimagesize($background);//获取原图片高度、宽度
 if ($width && ($s_w < $s_h)) {
 $width = ($height / $s_h) * $s_w;
 } else {
 $height = ($width / $s_w) * $s_h;
 }
 $new=imagecreatetruecolor($width, $height);
 $img=imagecreatefromjpeg($background);
 imagecopyresampled($new, $img, 0, 0, 0, 0, $width, $height, $s_w, $s_h);
 imagejpeg($new, $newfile);
 imagedestroy($new);
 imagedestroy($img);
 }
 thumn("images/hee.jpg", 200, 200, "./images/hee3.jpg");
 gif透明色处理
 代码如下:
 function thumn($background, $width, $height, $newfile) {
 list($s_w, $s_h)=getimagesize($background);
 if ($width && ($s_w < $s_h)) {
 $width = ($height / $s_h) * $s_w;
 } else {
 $height = ($width / $s_w) * $s_h;
 }
 $new=imagecreatetruecolor($width, $height);
 $img=imagecreatefromgif($background);
 $otsc=imagecolortransparent($img);
 if($otsc >=0 && $otst < imagecolorstotal($img)){//判断索引色
 $tran=imagecolorsforindex($img, $otsc);//索引颜色值
 $newt=imagecolorallocate($new, $tran["red"], $tran["green"], $tran["blue"]);
 imagefill($new, 0, 0, $newt);
 imagecolortransparent($new, $newt);
 }
 imagecopyresized($new, $img, 0, 0, 0, 0, $width, $height, $s_w, $s_h);
 imagegif($new, $newfile);
 imagedestroy($new);
 imagedestroy($img);
 }
 thumn("images/map.gif", 200, 200, "./images/map3.gif");

图片裁剪
<?php
/**
* 图片裁剪处理
* edit by 3water.com
*/
function cut($background, $cut_x, $cut_y, $cut_width, $cut_height, $location){
$back=imagecreatefromjpeg($background);
$new=imagecreatetruecolor($cut_width, $cut_height);
imagecopyresampled($new, $back, 0, 0, $cut_x, $cut_y, $cut_width, $cut_height,$cut_width,$cut_height);
imagejpeg($new, $location);
imagedestroy($new);
imagedestroy($back);
}
cut("./images/hee.jpg", 440, 140, 117, 112, "./images/hee5.jpg");
?>

图片加水印 文字水印
<?PHP
 /**
 * 
 * 图片添加文字水印
 */ function mark_text($background, $text, $x, $y){
 $back=imagecreatefromjpeg($background);
 $color=imagecolorallocate($back, 0, 255, 0);
 imagettftext($back, 20, 0, $x, $y, $color, "simkai.ttf", $text);
 imagejpeg($back, "./images/hee7.jpg");
 imagedestroy($back);
 }
 mark_text("./images/hee.jpg", "细说PHP", 150, 250);
 //图片水印
 function mark_pic($background, $waterpic, $x, $y){
 $back=imagecreatefromjpeg($background);
 $water=imagecreatefromgif($waterpic);
 $w_w=imagesx($water);
 $w_h=imagesy($water);
 imagecopy($back, $water, $x, $y, 0, 0, $w_w, $w_h);
 imagejpeg($back,"./images/hee8.jpg");
 imagedestroy($back);
 imagedestroy($water);
 }
 mark_pic("./images/hee.jpg", "./images/gaolf.gif", 50, 200);

图片旋转
<?PHP
 /**
 * 图片旋转
 */
 $back=imagecreatefromjpeg("./images/hee.jpg"); $new=imagerotate($back, 45, 0);
 imagejpeg($new, "./images/hee9.jpg");
 ?>

图片水平翻转垂直翻转
<?php
 /**
 * 图片水平翻转 垂直翻转
 */
 function turn_y($background, $newfile){
 $back=imagecreatefromjpeg($background);
 $width=imagesx($back);
 $height=imagesy($back);
 $new=imagecreatetruecolor($width, $height);
 for($x=0; $x < $width; $x++){
 imagecopy($new, $back, $width-$x-1, 0, $x, 0, 1, $height);
 }
 imagejpeg($new, $newfile);
 imagedestroy($back);
 imagedestroy($new);
 }
 function turn_x($background, $newfile){
 $back=imagecreatefromjpeg($background);
 $width=imagesx($back);
 $height=imagesy($back);
 $new=imagecreatetruecolor($width, $height);
 for($y=0; $y < $height; $y++){
 imagecopy($new, $back,0, $height-$y-1, 0, $y, $width, 1);
 }
 imagejpeg($new, $newfile);
 imagedestroy($back);
 imagedestroy($new);
 }
 turn_y("./images/hee.jpg", "./images/hee11.jpg");
 turn_x("./images/hee.jpg", "./images/hee12.jpg");
 ?>
PHP 相关文章推荐
数据库的日期格式转换
Oct 09 PHP
php mssql 日期出现中文字符的解决方法
Mar 10 PHP
编译php 5.2.14+fpm+memcached(具体操作详解)
Jun 18 PHP
php后门URL的防范
Nov 12 PHP
ThinkPHP验证码使用简明教程
Mar 05 PHP
PHP文件锁定写入实例解析
Jul 14 PHP
PHP高级编程实例:编写守护进程
Sep 02 PHP
php简单实现MVC
Feb 05 PHP
浅谈本地WAMP环境的搭建
May 13 PHP
PHP使用array_merge重新排列数组下标的方法
Jul 22 PHP
laravel自定义分页效果
Jul 23 PHP
在PHP中实现使用Guzzle执行POST和GET请求
Oct 15 PHP
PHP 获取远程文件大小的3种解决方法
Jul 11 #PHP
深入Nginx + PHP 缓存详解
Jul 11 #PHP
基于PHP中的常用函数回顾
Jul 11 #PHP
PHP 获取文件权限函数介绍
Jul 11 #PHP
浅析php学习的路线图
Jul 10 #PHP
php对图像的各种处理函数代码小结
Jul 08 #PHP
php/js获取客户端mac地址的实现代码
Jul 08 #PHP
You might like
PHP调用三种数据库的方法(3)
2006/10/09 PHP
解析zend studio中直接导入svn中的项目的方法步骤
2013/06/21 PHP
PHP 中 DOMDocument保存xml时中文出现乱码问题的解决方案
2016/09/19 PHP
php组合排序简单实现方法
2016/10/15 PHP
PHP验证终端类型是否为手机的简单实例
2017/02/07 PHP
PHP PDOStatement::fetchObject讲解
2019/02/01 PHP
Laravel 6.2 中添加了可调用容器对象的方法
2019/10/22 PHP
php在linux环境中如何使用redis详解
2020/12/15 PHP
二级域名转向类
2006/11/09 Javascript
ExtJS 简介 让你知道extjs是什么
2008/12/29 Javascript
用jquery实现的模拟QQ邮箱里的收件人选取及其他效果(一)
2011/01/06 Javascript
JS基础之undefined与null的区别分析
2011/08/08 Javascript
javascript学习笔记(八) js内置对象
2012/06/19 Javascript
javascript制作坦克大战全纪录(1)
2014/11/27 Javascript
jquery获取当前日期的方法
2015/01/14 Javascript
纯js实现无限空间大小的本地存储
2015/06/18 Javascript
js表单处理中单选、多选、选择框值的获取及表单的序列化
2016/03/08 Javascript
JavaScript实现复制内容到粘贴板代码
2016/03/31 Javascript
BootStrap智能表单实战系列(八)表单配置json详解
2016/06/13 Javascript
AngularJS入门教程之表格实例详解
2016/07/27 Javascript
Angular的事件和表单详解
2016/12/26 Javascript
详解Vue中添加过渡效果
2017/03/20 Javascript
bootstrap常用组件之头部导航实现代码
2017/04/20 Javascript
Angular 2父子组件数据传递之@Input和@Output详解(下)
2017/07/05 Javascript
js字符限制(字符截取) 一个中文汉字算两个字符
2017/09/12 Javascript
小程序实现授权登陆的解决方案
2018/12/02 Javascript
antd design table更改某行数据的样式操作
2020/10/31 Javascript
[44:39]2014 DOTA2国际邀请赛中国区预选赛 NE VS CNB
2014/05/21 DOTA
[54:05]DOTA2-DPC中国联赛定级赛 SAG vs iG BO3第一场 1月9日
2021/03/11 DOTA
python生成excel的实例代码
2017/11/08 Python
Numpy 改变数组维度的几种方法小结
2018/08/02 Python
python与c语言的语法有哪些不一样的
2020/09/13 Python
ASP.NET中的身份验证有那些
2012/07/13 面试题
大学生应聘导游自荐信
2014/06/02 职场文书
小学入学感言
2015/08/01 职场文书
仓库管理制度范本
2015/08/04 职场文书