这里写的是完成每个功能的函数,可以复制单个函数直接使用,这里的每个函数都是另外一篇PHP常用类------图片处理类Image当中的方法进行细化,可以参考一下

废话不多说,直接付代码吧!

添加水印(文字和图片)

<?php
/**
* [创建图片文字水印]
* @param [string] $imagename [需要添加水印的值]
* @param [string] $string [图片上添加的文字]
* @param [string] $locate [水印位置,center,left_buttom,right_buttom三选一]
* @return [null] [description]
*/
function create_words_watermark($imagename,$string,$locate){
list($width,$height,$type)=getimagesize($imagename);
$types=array(1 => "GIF",2 => "JPEG",3 => "PNG",
4 => "SWF",5 => "PSD",6 => "BMP",
7 => "TIFF",8 => "TIFF",9 => "JPC",
10 => "JP2",11 => "JPX",12 => "JB2",
13 => "SWC",14 => "IFF",15 => "WBMP",16 => "XBM");
$type=strtolower($types[$type]);
$create="imagecreatefrom".$type;
$img=$create($imagename); $string_color=imagecolorallocate($img,200, 200, 200);
$fontsize=4; // 图片的宽和高也可用下面两个函数获得
// $width=imagesx($img);
// $height=imagesy($img); switch($locate){
case 'center':
$x=($width-imagefontwidth($fontsize)*strlen($string))/2;
$y=($height-imagefontheight($fontsize))/2;
break;
case 'left_buttom':
$x=5;
$y=($height-imagefontheight($fontsize)-3);
break;
case 'right_buttom':
$x=($width-imagefontwidth($fontsize)*strlen($string)-3);
$y=($height-imagefontheight($fontsize)-3);
break;
default:
die("未指定水印位置!");
break;
} imagestring($img,$fontsize,$x,$y,$string,$string_color);
imagestring($img,$fontsize,$x+1,$y+1,$string,$string_color); $save="image".$type;
//保存
//$save($img,"new_".$imagename); //显示
header("content-type:image/".$type);
$save($img);
imagedestroy($img);
} // create_words_watermark("test.png","hello world","right_buttom"); /**
* [create_pic_watermark 添加图片水印]
* @param [string] $dest_image [需要添加图片水印的图片名]
* @param [string] $watermark [水印图片名]
* @param [string] $locate [水印位置,center,left_buttom,right_buttom三选一]
* @return [type] [description]
*/
function create_pic_watermark($dest_image,$watermark,$locate){
list($dwidth,$dheight,$dtype)=getimagesize($dest_image);
list($wwidth,$wheight,$wtype)=getimagesize($watermark); $types=array(1 => "GIF",2 => "JPEG",3 => "PNG",
4 => "SWF",5 => "PSD",6 => "BMP",
7 => "TIFF",8 => "TIFF",9 => "JPC",
10 => "JP2",11 => "JPX",12 => "JB2",
13 => "SWC",14 => "IFF",15 => "WBMP",16 => "XBM"); $dtype=strtolower($types[$dtype]);//原图类型
$wtype=strtolower($types[$wtype]);//水印图片类型 $created="imagecreatefrom".$dtype;
$createw="imagecreatefrom".$wtype; $imgd=$created($dest_image);
$imgw=$createw($watermark); switch($locate){
case 'center':
$x=($dwidth-$wwidth)/2;
$y=($dheight-$wheight)/2;
break;
case 'left_buttom':
$x=1;
$y=($dheight-$wheight-2);
break;
case 'right_buttom':
$x=($dwidth-$wwidth-1);
$y=($dheight-$wheight-2);
break;
default:
die("未指定水印位置!");
break;
} imagecopy($imgd,$imgw,$x,$y,0,0, $wwidth,$wheight); $save="image".$dtype; //显示
header("content-type:image/".$dtype);
$save($imgd); imagedestroy($imgw);
imagedestroy($imgd);
} create_pic_watermark("ganlixin.jpg","test.png","left_buttom");
?>

剪切图片

<?php
/**
* [cut_image] 从原图中剪切一部分
* @param [string] $old_imagename [需要剪切的图片名]
* @param [int] $start_width [从原图片宽为的$start_width开始剪切]
* @param [int] $start_height [从原图片高为的$start_height开始剪切]
* @param [int] $new_width [从原图片中剪切$new_width的宽度]
* @param [int] $new_height [从原图片中剪切$new_height的高度]
* @return [null] [null]
*/
function cut_image($old_imagename,$start_width,$start_height,$new_width,$new_height){
list($old_width,$old_height,$type)=getimagesize($old_imagename);
$types=array(1 => "GIF",2 => "JPEG",3 => "PNG",
4 => "SWF",5 => "PSD",6 => "BMP",
7 => "TIFF",8 => "TIFF",9 => "JPC",
10 => "JP2",11 => "JPX",12 => "JB2",
13 => "SWC",14 => "IFF",15 => "WBMP",16 => "XBM");
$type=strtolower($types[$type]);
$create="imagecreatefrom".$type; $old_img=$create($old_imagename);
$new_img=imagecreatetruecolor($new_width, $new_height); imagecopyresampled($new_img,$old_img,0,0,$start_width,$start_height,$new_width,$new_height,$new_width,$new_height);
$save="image".$type; //保存
//$save($new_img,"new_".$old_imagename); //显示
header("content-type:image/".$type);
$save($new_img); //销毁
imagedestroy($old_img);
imagedestroy($new_img);
} cut_image("ganlixin.jpg",0,0,200,200);
?>

  

翻转图片

<?php
/**
* [rotate_image 图片旋转]
* @param [string] $imagename [要进行旋转的图片名]
* @param [string] $angle [旋转的角度,逆时针为正]
* @return [null] [description]
*/
function rotate_image($imagename,$angle){
list($width,$height,$type)=getimagesize($imagename);
$types=array(1 => "GIF",2 => "JPEG",3 => "PNG",
4 => "SWF",5 => "PSD",6 => "BMP",
7 => "TIFF",8 => "TIFF",9 => "JPC",
10 => "JP2",11 => "JPX",12 => "JB2",
13 => "SWC",14 => "IFF",15 => "WBMP",16 => "XBM");
$type=$types[$type]; $create="imagecreatefrom".$type;
$img=$create($imagename);
$new_img=imagerotate($img,$angle,0);
$save="image".$type; //显示
header("content-type:image/".$type);
$save($new_img); // 保存
// $save($new_img,"new_".$imagename); imagedestroy($img);
imagedestroy($new_img);
} rotate_image("ganlixin.jpg",-90); ?>

  

翻转图片

<?php
/**
* [overturn_image 翻转图片]
* @param [string] $imagename [要反转的图片名]
* @param [char] $method [按x轴或y轴翻转,只有x,y选项]
* @return [type] [description]
*/
function overturn_image($imagename,$method){
$method=strtolower($method);
list($width,$height,$type)=getimagesize($imagename);
$types=array(1 => "GIF",2 => "JPEG",3 => "PNG",
4 => "SWF",5 => "PSD",6 => "BMP",
7 => "TIFF",8 => "TIFF",9 => "JPC",
10 => "JP2",11 => "JPX",12 => "JB2",
13 => "SWC",14 => "IFF",15 => "WBMP",16 => "XBM");
$type=$types[$type]; $create="imagecreatefrom".$type;
$img=$create($imagename);//源图片
$new_img=imagecreatetruecolor($width,$height);//翻转之后的图片 if($method=='y'){
for($i=0;$i<$width;$i++){
imagecopy($new_img,$img,$width-$i-1,0,$i,0,1,$height);
}
} else if($method=='x'){
for($i=0;$i<$height;$i++){
imagecopy($new_img,$img,0,$height-$i-1,0,$i,$width,1);
}
}
$save="image".$type; //显示
header("content-type:image/".$type);
$save($new_img); // 保存
// $save($new_img,"new_".$imagename); imagedestroy($img);
imagedestroy($new_img);
}
overturn_image("ganlixin.jpg","y");
?>

  

PHP利用GD库处理图片方法实现的更多相关文章

  1. Windows环境下php开启GD库的方法

    一.GD库是什么? GD库是php处理图形的扩展库,GD库提供了一系列用来处理图片的API,使用GD库可以处理图片,或者生成图片,也可以给图片加水印.在网站上GD库通常用来生成缩略图,或者用来对图片加 ...

  2. php学习笔记:利用gd库生成图片,并实现随机验证码

    说明:一些基本的代码我都进行了注释,这里实现的验证码位数.需要用的字符串都可以再设置.有我的注释,大家应该很容易能看得懂. 基本思路: 1.用mt_rand()随机生成数字确定需要获取的字符串,对字符 ...

  3. GD库imagecopyresampled()方法详解~

    整理了一下GD库这个缩放,拉伸复制的方法 因为这个函数参数太多了~ imagecopyresampled()   /* //拷贝部分图像并调整大小 bool imagecopyresampled ( ...

  4. PHP利用GD库绘图和生成验证码图片

    首先得确定php.ini设置有没有打开GD扩展功能,測试例如以下 print_r(gd_info()); 假设有打印出内容例如以下,则说明GD功能有打开: Array ( [GD Version] = ...

  5. PHP学习笔记:利用gd库给图片打图片水印

    <?php $dst_path = '1.jpg';//目标图片 $src_path = 'logo1.png';//水印图片 //创建图片的实例 $dst = imagecreatefroms ...

  6. thinkphp 利用GD库在图片上写文字

    <?php /** * Created by PhpStorm. * User: Administrator */ namespace Home\Event; use \Think\Image; ...

  7. PHP利用GD库画曲线

    效果: PHP代码 <?php Header('Content-type: image/png;Charset:utf-8'); //声明图片 $im = imagecreate(400,200 ...

  8. php 利用Gd库添加文字水印乱码的问题及解决方案

    最近一个项目进行了服务器迁移,部署后发现 ,其中一个为图片添加水印文字的功能出现了乱码问题,确认功能代码不存在问题,同时项目代码都是使用UTF-8编码,不存在编码问题,也检查排除了字体文件出现问题的可 ...

  9. php 简单的学习GD库绘制图片并传回给前端实现方式

    1.基本的GD库绘制图片汇总 2.后台实现小案例 <?php // $img = imagecreatetruecolor(200,40); // var_dump($img); // 利用GD ...

随机推荐

  1. BSOJ 2414 -- 【JSOI2011】分特产

    Description JYY 带队参加了若干场ACM/ICPC 比赛,带回了许多土特产,要分给实验室的同学们. JYY 想知道,把这些特产分给N 个同学,一共有多少种不同的分法?当然,JYY 不希望 ...

  2. Tensorflow基本概念

    [本文摘自网络,仅供学习使用] 官网上对TensorFlow的介绍是,一个使用数据流图(data flow graphs)技术来进行数值计算的开源软件库.数据流图中的节点,代表数值运算:节点节点之间的 ...

  3. MySQL-proxy代理导致PHP PDO::ATTR_EMULATE_PREPARES的预处理出错,MySQL报General error: 1243错误

    背景: 用的ThinkPHP5的框架.(相比之前的3.2版本,版本5都用了PDO处理数据库) 症状: 报错信息: SQLSTATE[HY000]: General error: 1243 Unknow ...

  4. OPTIMIZER_INDEX_COST_ADJ 与OPTIMIZER_INDEX_CACHING 参数说明

    [部分转载]http://www.xifenfei.com/2012/06/optimizer_index_caching和optimizer_index_cost_adj参数说明.html 1. O ...

  5. Zookeeper安装及运行

    zookeeper的安装分为三种模式:单机模式.集群模式和伪集群模式. 单机模式 首先,从Apache官网下载一个Zookeeper稳定版本,本次教程采用的是zookeeper-3.4.9版本. ht ...

  6. oracle(sql)基础篇系列(五)——PLSQL、游标、存储过程、触发器

    PL/SQL PL/SQL 简介 每一种数据库都有这样的一种语言,PL/SQL 是在Oracle里面的一种编程语言,在Oracle内部使用的编程语言.我们知道SQL语言是没有分支和循环的,而PL语言是 ...

  7. mongodb数据库中插入数据

    mongodb数据库中插入数据 一:connection 访问集合: 在mongodb数据库中,数据是存储在许多数据集合中,可以使用数据库对象的collection方法访问一个集合.该方法使用如下: ...

  8. git排错

    解决: 将远程仓库中除.git以外的所有文件删除,然后执行   git config --bool core.bare true  然后客户端重新push即可解决问题 还要注意远程仓库权限方面...

  9. ESP32 environment ubuntu

    ubuntu官方用的是16.04版本的,阿里源下载地址:http://mirrors.aliyun.com/ubuntu-releases/16.04/     用官方版本的好处就是省的搞一堆各种错误 ...

  10. 【Codeforces 1120C】Compress String

    Codeforces 1120 C 题意:给一个串\(S\),将这个串分成\(t_1..t_m\),如果\(t_i\)在\(t_1..t_{i-1}\)中作为子串出现过,那么这个的代价是\(b\),否 ...