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. 简介. 图像 ...
随机推荐
- python 写入csv文件
import csv fieldnames = ['Column1', 'Column2', 'Column3', 'Column4'] rows = [{'Column1': '0', 'Col ...
- postgres 正则表达式
PostgreSQL正则表达式 基础: Operator Description Example ~ Matches regular expression, case sensitive 'thoma ...
- javascript function new this
1. 首先,我们这里把function直接调用时将这个function当做方法来看待,而new function是将function当做类来看待 2. 当把function作为类来使用时,functi ...
- 转 Warning:MongoDB Replica Sets配置注意事项
我们知道,MongoDB不提供单机的数据安全性,取而代之的是提供了Replica Sets的高可用方案.官方文档中提到的案例是三个节点组成的Replica Sets,这样在其中任何一个节点宕机后都会自 ...
- 【模板下载】innosetup 制作.net安装包的模板
NetworkComms网络通信框架序言 这个模板是在博客园和CodeProject上的代码修改而成的,感谢原作者 模板是2个 innosetup 制作.net 2.0 安装包的模板 innosetu ...
- linux查找文件命令find
http://blog.csdn.net/ydfok/article/details/1486451 find 路径 -name'文件名' 如:find / -name '*dhcp*'
- 使用AlarmManager设置闹钟----之一
import java.util.Calendar; import android.os.Bundle;import android.app.Activity;import android.app.A ...
- plsql快速选中一行的快捷键
实际工作中,经常用到pl/sql,在sql window中,经常性的用到选中一行然后按F8执行这条sql语句.用鼠标选中一行不是特别方便.用快捷键就快多了. 1.使用home键(不是windows键奥 ...
- 知名杀毒软件Mcafee(麦咖啡)个人版 资源汇总兼科普(来自卡饭)
虽然早已不是用咖啡了,但我也实时关注的咖啡的一举一动,潜水看帖日久,发现小白众多,好多有价值的帖子淹没于帖海当中,甚是惋惜. 我有如下建议 1.咖啡区管理层,能否吧一些优秀的资源教程 ...
- LA 4119 - Always an integer
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...