用到两个image系统函数

imagecopymerge — 拷贝并合并图像的一部分

imagecopyresampled — 重采样拷贝部分图像并调整大小

/*
如何知道图片的大小和类型
无法确认调用函数:Imagecreatefrompng/jpeg……
可以独处图片的宽和高 相当于宽高是已知的
一个重要的函数getimagesize()
*/ /*
想操作图片
先把图片的大小,类型信息得到 水印:就是把指定的水印复制到目标上,并加透明效果 缩略图:就是把大图片复制到小尺寸画面上 */ class ImageTool{ //imageinfo 分析图片的信息
//return array()
public static function imageInfo($image){
//判断图片是否存在
if(!file_exists($image)){
return false; }
$info = getimagesize($image);
if($info == false){
return false;
} //此时info分析出来是一个数组
$img['width'] = $info[0];
$img['height'] = $info[1];
//分析图片的后缀
$img['ext'] = substr($info['mime'],strpos($info['mime'],'/')+1); return $img;
} /*
加水印
string $dst 待操作图片
$water 水印小图
$save 不填,则默认替换原始图
$alpha 透明度
$pos 水印图放的位置1234,顺时针
*/
public static function water($dst,$water,$save=NULL,$pos=2,$alpha=50){
//先保证两个图片存在
if(!file_exists($dst) || !file_exists($water)){
return false;
} //1.保证水印不能比待操作图片还大
$dinfo = self::imageInfo($dst);
$winfo = self::imageInfo($water); //判断
if($winfo['height'] > $dinfo['height'] || $winfo['width'] > $dinfo['width']){
return false;
} //两张图,读到画布上,但是图片可能是png,可能是jpeg,用什么函数来读
$dfunc = 'imagecreatefrom' .$dinfo['ext'];
$wfunc = 'imagecreatefrom' .$winfo['ext']; if(!function_exists($dfunc) || !function_exists($wfunc)){
return false;
} //动态加载函数来创建画布
$dim = $dfunc($dst); //创建待操作的画布
$wim = $wfunc($water); //创建水印画布 //根据水印的位置,计算粘贴的坐标
switch($pos){
case 0; //左上角
$posx = 0;
$posy = 0;
break; case 1; //右上角
$posx = $dinfo['width'] - $winfo['width'];
$posy = 0;
break; case 2; //左下角
$posx = 0;
$posy = $dinfo['height'] - $winfo['height'];
break; default: //默认右下角
$posx = $dinfo['width'] - $winfo['width'];
$posy = $dinfo['height'] - $winfo['height']; } //加水印 在什么位置?
// imagecopymerge ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h , int $pct ) imagecopymerge($dim,$wim,$posx,$posy,0,0,$winfo['width'],$winfo['height'],$alpha); if(!$save){ $save=$dst;
unlink($dst); //删除原图
} //保存图片
$createfunc = 'image' . $dinfo['ext'];
$createfunc($dim,$save); //销毁图片
imagedestroy($dim);
imagedestroy($wim); return true;
} /*
thumb 生成缩略图
等比例缩放,两边留白
*/
public static function thumb($dst,$save=NULL,$width=200,$height=200){
//首先判断待处理的图片是否存在
$dinfo = self::imageInfo($dst);
if($dinfo == false){
return false;
} //计算缩放比例
$calc = min($width/$dinfo['width'],$height/$dinfo['height']); //创建原始图的画布
$dfunc = 'imagecreatefrom'.$dinfo['ext'];
$dim = $dfunc($dst); //创建缩略图画布
$tim = imagecreatetruecolor($width,$height); //创建白色填充缩略图画布
$white = imagecolorallocate($tim,255,255,255); //填充缩略画布
imagefill($tim,0,0,$white); //复制并缩略
$dwidth = (int)$dinfo['width']*$calc;
$dheight = (int)$dinfo['height']*$calc; $paddingx = ($width - $dwidth) / 2;
$paddingy = ($height - $dheight) /2; imagecopyresampled($tim,$dim,$paddingx,$paddingy,0,0,$dwidth,$dheight,$dinfo['width'],$dinfo['height']); //保存图片
if(!$save){
$save = $dst;
unlink($dst);
}
$createfun = 'image'.$dinfo['ext'];
$createfun($tim,$save); imagedestroy($dim);
imagedestroy($tim); return true; }

调用传参:

//调用传参  加水印
echo ImageTool::water('原图片地址','加水印图片地址','生成图片')?'ok':false;
//调用 缩略图
echo ImageTool::thumb('原图地址','生成图片地址',200,200)?'ok':false;

php之图片处理类缩略图加水印的更多相关文章

  1. [原创]超强C#图片上传,加水印,自动生成缩略图源代码

    <%@ Page Language=“C#“ AutoEventWireup=“true“ %> <%@ Import Namespace=“System“ %> <%@ ...

  2. ECSHOP商品描述和文章里不加水印,只在商品图片和商品相册加水印

    fckeditor\editor\filemanager\connectors\php //判断并给符合条件图片加上水印 if ($**tension == 'jpg' || $**tension = ...

  3. PHP的图片处理类(缩放、加图片水印和剪裁)

    <!--test.php文件内容--> <?php //包含这个类image.class.php include "image.class.php"; $img ...

  4. PHP生成缩略图、加水印

    <?php class ThumbWaterImages{ /** * 生成缩略图/加水印 * classname ThumbWaterImages * datetime:2015-1-15 * ...

  5. 帝国cms更换Ueditor编辑器上传图片加水印

    Ueditor安装包,里面有个/php/文件夹,找到Uploader.class.php,这是通用上传类文件找到private function upFile(),这是上传文件的主处理方法,找到122 ...

  6. Android中的缩略图加载-不浪费一点多余的内存

    1. Why,为什么要加载缩略图? 有的时候不需要展示原图,只需展示图片的缩略图,可以节省内存.比如:网易新闻中的图片浏览,左边展示的小狮子图片就是一个缩略图,点击这个图片,才会展示原图.   2. ...

  7. PHPThumb处理图片,生成缩略图,图片尺寸调整,图片截取,图片加水印,图片旋转

    [强烈推荐]下载地址(github.com/masterexploder/PHPThumb). 注意这个类库有一个重名的叫phpThumb,只是大小写的差别,所以查找文档的时候千万注意. 在网站建设过 ...

  8. 黄聪:C#图片处理封装类(裁剪、缩放、清晰度、加水印、生成缩略图)有示例(转)

    C#图片处理示例(裁剪,缩放,清晰度,水印) 吴剑 2011-02-20 原创文章,转载必需注明出处:http://www.cnblogs.com/wu-jian/ 前言 需求源自项目中的一些应用,比 ...

  9. 使用 ImageEnView 给图片加水印,及建缩略图

    摘要: 使用 ImageEnView 给图片加水印,及建缩略图 {Power by hzqghost@21cn.com}unit CutWater; interface uses  Math,imag ...

随机推荐

  1. cocos2d的安装

    安装cocos2d其实就是在Xcode中安装几个模板,然后在Xcode里面就可以直接使用这些模板了. 其实说是模板,也就是封装了许许多多引擎的文件,相对于原生的程序,也许使用引擎模板更加方便.   下 ...

  2. 排序Tip

    排序算法   所有排序算法汇总:http://en.wikipedia.org/wiki/Sort_algorithm counting sort 资料 :http://www.cs.miami.ed ...

  3. MySQL表设计基础

    MySQL表设计关于blog数据库中建立所有表的sql语句<一.>sql语句中 约束概念constraint concept1.1 实体完整性entity integrity(主键--唯一 ...

  4. openStack云平台虚拟桌面galera mysql 3节点集群实例实战

  5. 安装rabbitmq集群

    一.安装 erlang.rabbitmq 在10.0.0.45.10.0.0.57.10.0.0.58三个节点上安装,然后开启 RabbitMQ 监控插件 以下在root用户操作 1./etc/hos ...

  6. shuffle() 函数(转)

    定义和用法 shuffle() 函数把数组中的元素按随机顺序重新排列. 若成功,则返回 TRUE,否则返回 FALSE. 注释:本函数为数组中的单元赋予新的键名.这将删除原有的键名而不仅是重新排序. ...

  7. spring mvc DispatcherServlet详解之一---处理请求深入解析(续)

    上文中,我们知道分发过程有以下步骤: 分发过程如下: 1. 判断是否设置了multipart resolver,设置的话转换为multipart request,没有的话则继续下面的步骤. 2. 根据 ...

  8. 用GitHub Pages免费空间搭建Blog

    前言   其实之前就知道可以用GitHub Pages搭建静态博客,不过之前一直忙着爬手册撸代码==,昨天终于把前端各种手册里的入门教程撸的差不多了(CSS布局撸的我要吐了好嘛),于是把代码什么的放一 ...

  9. CSS3 颜色值HSL表示方式&简单实例

    HSL色彩模式:就是色调(Hue).饱和度(Saturation).亮度(Lightness)三个颜色通道的改变以及它们相互之间的叠加来获得各种颜色,色调(Hue)色调最大值360,饱和度和亮度有百分 ...

  10. android 定时请求(两种实现方式)

    方式一: Handler + Runnable (借鉴网址:http://stackoverflow.com/questions/6207362/how-to-run-an-async-task-fo ...