PHP图像处理之在原图像处理
处理原有的图像
图片处理,缩放,裁剪,翻转,旋转,透明,锐化等图片操作
一.创建图片资源
imagecreatetruecolor(width,height);
gif jpg png
imagecreatefromgif(图片名称);
imagecreatefrompng(图片名称);
imagecreatefromjpeg(图片名称);
画出各种图形(圆形、矩形、线段、文字)
imagegif(图片位置);
imagepng(图片位置);
imagejpeg(图片位置);
imagedestroy(图片资源);
<?php //利用原有的图片创建图片资源 $img=imagecreatefromgif("./images/map.gif"); //设置颜色 $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); ?>
二.获取图片的属性
imagesx(res); 取得图片的宽度
imagesy(res); 取得图片的高度
getimagesize(图片名称); 返回数组,0==width,1==height,2==type
<?php $img=imagecreatefromgif("./images/map.gif"); $red=imagecolorallocate($img, 255, 0, 0); //输出图片的高度和宽度 echo 'width:'.imagesx($img)."<br>"; echo 'height:'.imagesy($img)."<br>"; echo '<pre>'; //取得图像大小 print_r(getimagesize("./images/map.gif")); echo '</pre>'; imagedestroy($img); ?>
三.透明处理
png jpeg 透明都正常,只有gif不正常
imagecolortransparent(); 将某个颜色定义为透明色
imagecolorstotal(); 取得一幅图像的调色板中颜色的数目
imagecolorsforindex(); 取得某索引的颜色
<?php function thumn($background,$newWidth,$newHeight,$newFile){ //取出图片的宽高 list($width,$height)=getimagesize($background); //求的而缩放的宽高 if ($newWidth && ($width < $height)){ $newWidth=($newHeight/$height)*$width; } else { $newHeight=($newWidth/$width)*$height; } //创建缩放后图片的画布 $new=imagecreatetruecolor($newWidth,$newHeight); $img=imagecreatefromgif($background); //imagecolortransparent(); 将某个颜色定义为透明色 $otsc=imagecolortransparent($img); //imagecolorstotal(); 取得一幅图像的调色板中颜色的数目 if($otsc>=0 && $otsc<imagecolorstotal($img)){ //imagecolorsforindex(); 取得某索引的颜色 $tran=imagecolorsforindex($img, $otsc); //新的颜色 $newt=imagecolorallocate($new, $tran["red"], $tran["green"], $tran["blue"]); //新的颜色设为缩放图片的颜色 imagefill($new, 0, 0, $newt); //将缩放图片的颜色设为背景透明 imagecolortransparent($new,newt); } //缩放原图片 imagecopyresampled($new, $img,0,0,0,0, $newWidth, $newHeight, $width, $height); //保存缩放后的图片 imagegif($new,$newFile); //释放资源 imagedestroy($new); imagedestroy($img); } thumn("images/map.gif",200,200,"./images/map3.gif"); ?>
四.图片的裁剪
imagecopyresized(); 拷贝部分图像并调整大小
imagecopyresampled(); 重采样拷贝部分图像并调整大小
<?php 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"); ?>
五、加水印(文字, 图片)
imagettftext();
imagecopy();
<?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) ?>
六、图片旋转
imagerotate -- 用给定角度旋转图像
<?php $back=imagecreatefromjpeg("./images/hee.jpg"); $new=imagerotate($back, 45, 0); imagejpeg($new, "./images/hee9.jpg"); ?>
七、图片翻转
沿Y轴
沿X轴
<?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"); ?>
八、锐化
imagecolorsforindex()
imagecolorat()
<?php function sharp($background, $degree, $save){ $back=imagecreatefromjpeg($background); $b_x=imagesx($back); $b_y=imagesy($back); $dst=imagecreatefromjpeg($background); for($i=0; $i<$b_x; $i++){ for($j=0; $j<$b_y; $j++){ $b_clr1=imagecolorsforindex($back, imagecolorat($back, $i-1, $j-1)); $b_clr2=imagecolorsforindex($back, imagecolorat($back, $i, $j)); $r=intval($b_clr2["red"]+$degree*($b_clr2["red"]-$b_clr1["red"])); $g=intval($b_clr2["green"]+$degree*($b_clr2["green"]-$b_clr1["green"])); $b=intval($b_clr2["blue"]+$degree*($b_clr2["blue"]-$b_clr1["blue"])); $r=min(255, max($r, 0)); $g=min(255, max($g, 0)); $b=min(255, max($b, 0)); if(($d_clr=imagecolorexact($dst, $r, $g, $b))==-1){ $d_clr=Imagecolorallocate($dst, $r, $g, $b); } imagesetpixel($dst, $i, $j, $d_clr); } } imagejpeg($dst, $save); imagedestroy($back); imagedestroy($dst); } sharp("./images/hee.jpg", 20, "./images/hee13.jpg"); ?>
PHP图像处理之在原图像处理的更多相关文章
- Atitit 图像处理 常用8大滤镜效果 Jhlabs 图像处理类库 java常用图像处理类库
Atitit 图像处理 常用8大滤镜效果 Jhlabs 图像处理类库 java常用图像处理类库1.1. 5种常用的Photoshop滤镜,分别针对照片的曝光.风格色调.黑白照片处理.锐利度.降噪这五大 ...
- 【图像处理】OpenCV+Python图像处理入门教程(四)几何变换
这篇随笔介绍使用OpenCV进行图像处理的第四章 几何变换. 4 几何变换 图像的几何变换是指将一幅图像映射到另一幅图像内.有缩放.翻转.仿射变换.透视.重映射等操作. 4.1 缩放 使用cv2. ...
- 【图像处理】OpenCV+Python图像处理入门教程(五)阈值处理
这篇随笔介绍使用OpenCV进行图像处理的第五章 阈值处理. 5 阈值处理 阈值是指像素到达某临界值.阈值处理表示像素到达某临界值后,对该像素点进行操作和处理. 例如:设定一幅图像素阈值为200,则 ...
- 【图像处理】OpenCV+Python图像处理入门教程(六)图像平滑处理
相信很多小伙伴都听过"滤波器"这个词,在通信领域,滤波器能够去除噪声信号等频率成分,然而在我们OpenCV中,"滤波"并不是对频率进行筛选去除,而是实现了图像的 ...
- 【图像处理】OpenCV+Python图像处理入门教程(七)图像形态学操作
图像形态学主要从图像内提取分量信息,该分量信息通常对表达图像的特征具有重要意义.例如,在车牌号码识别中,能够使用形态学计算其重要特征信息,在进行识别时,只需对这些特征信息运算即可.图像形态学在目标视觉 ...
- 第二篇:杂项之图像处理pillow
杂项之图像处理pillow 杂项之图像处理pillow 本节内容 参考文献 生成验证码源码 一些小例子 1. 参考文献 http://pillow-cn.readthedocs.io/zh_CN/ ...
- Qt Quick 图像处理实例之美图秀秀(附源代码下载)
在<Qt Quick 之 QML 与 C++ 混合编程具体解释>一文中我们解说了 QML 与 C++ 混合编程的方方面面的内容,这次我们通过一个图像处理应用.再来看一下 QML 与 C++ ...
- Atitit 图像处理和计算机视觉的分类 三部分 图像处理 图像分析 计算机视觉
Atitit 图像处理和计算机视觉的分类 三部分 图像处理 图像分析 计算机视觉 1.1. 按照当前流行的分类方法,可以分为以下三部分:三部分 图像处理 图像分析 计算机视觉1 1.2. 图像处理需要 ...
- Python 之 使用 PIL 库做图像处理
http://www.cnblogs.com/way_testlife/archive/2011/04/17/2019013.html Python 之 使用 PIL 库做图像处理 1. 简介. 图像 ...
随机推荐
- WCF中Service Configuration Editor的使用方法
1.在App.config文件上右击,选择Edit WCF Configuration.... 或者打开Program Files\Microsoft Visual Studio 8\Common7\ ...
- jq 中each的用法
jQuery的each方法的几种常用的用法 each()方法能使DOM循环结构简洁,可遍历一维数组.多维数组.DOM, JSON 等等. var arr = [ "one", &q ...
- 5. Longest Palindromic Substring -- 最长回文字串
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- 130. Surrounded Regions -- 被某字符包围的区域
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- XML中的非法字符转化成实体
问题 如果XML有非法字符比如 "·",或者HTML标签<br/>.XML在解析的过程中就会出错.就无法正常解析,或者把xml反射成实体. 有些字符,像(<)这类 ...
- Spring使用RowMapper将数据中的每一行封装成用户定义的类
1.dao public interface MapperSelecteAllEmpDao { public List<Emp> all(); } 2.实现类 public class M ...
- bzoj 2434: [Noi2011]阿狸的打字机
#include<cstdio> #include<iostream> #include<cstring> #define M 100008 using names ...
- encodeURI
encodeURI("http://www.cnblogs.com/season-huang/some other thing"); //整个URL进行编码"http:/ ...
- Linux 常用命令杂记
移动光标:h:向左移动j:向下移动k:向上移动l:向上移动 与window 光标移动键功能一致. 常用命令行:shift + ^ 行首shift + $ 行尾ctrl + v 可视模式 , 选择一个范 ...
- Android VersionedGestureDetector手势事件
今天研究了一下PhotoView,发现里面的自定义的手势事件可以支持所有的SDK版本,该事件可以实现拖拽.滑动.缩放功能.下面直接上代码: public abstract class Versione ...