php之图片处理类缩略图加水印
用到两个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之图片处理类缩略图加水印的更多相关文章
- [原创]超强C#图片上传,加水印,自动生成缩略图源代码
<%@ Page Language=“C#“ AutoEventWireup=“true“ %> <%@ Import Namespace=“System“ %> <%@ ...
- ECSHOP商品描述和文章里不加水印,只在商品图片和商品相册加水印
fckeditor\editor\filemanager\connectors\php //判断并给符合条件图片加上水印 if ($**tension == 'jpg' || $**tension = ...
- PHP的图片处理类(缩放、加图片水印和剪裁)
<!--test.php文件内容--> <?php //包含这个类image.class.php include "image.class.php"; $img ...
- PHP生成缩略图、加水印
<?php class ThumbWaterImages{ /** * 生成缩略图/加水印 * classname ThumbWaterImages * datetime:2015-1-15 * ...
- 帝国cms更换Ueditor编辑器上传图片加水印
Ueditor安装包,里面有个/php/文件夹,找到Uploader.class.php,这是通用上传类文件找到private function upFile(),这是上传文件的主处理方法,找到122 ...
- Android中的缩略图加载-不浪费一点多余的内存
1. Why,为什么要加载缩略图? 有的时候不需要展示原图,只需展示图片的缩略图,可以节省内存.比如:网易新闻中的图片浏览,左边展示的小狮子图片就是一个缩略图,点击这个图片,才会展示原图. 2. ...
- PHPThumb处理图片,生成缩略图,图片尺寸调整,图片截取,图片加水印,图片旋转
[强烈推荐]下载地址(github.com/masterexploder/PHPThumb). 注意这个类库有一个重名的叫phpThumb,只是大小写的差别,所以查找文档的时候千万注意. 在网站建设过 ...
- 黄聪:C#图片处理封装类(裁剪、缩放、清晰度、加水印、生成缩略图)有示例(转)
C#图片处理示例(裁剪,缩放,清晰度,水印) 吴剑 2011-02-20 原创文章,转载必需注明出处:http://www.cnblogs.com/wu-jian/ 前言 需求源自项目中的一些应用,比 ...
- 使用 ImageEnView 给图片加水印,及建缩略图
摘要: 使用 ImageEnView 给图片加水印,及建缩略图 {Power by hzqghost@21cn.com}unit CutWater; interface uses Math,imag ...
随机推荐
- Centos6快速yum lamp
yum install httpd httpd-devel mysql mysql-server mysql-devel php php-mysql php-common php-gd php-mb ...
- ELK Packetbeat 部署指南(15th)
原文链接:http://www.ttlsa.com/elk/elk-packetbeat-deployment-guide/ Packetbeat 是一个实时网络数据包分析工具,与elasticsea ...
- RabbitMQ 概念
RabbitMQ快速概念入门 转(http://blog.csdn.net/qq_16414307/article/details/50585630) 本文适有一定消息队列基础的,但没有接触过Ra ...
- 关于用jQuery知识来实现优酷首页轮播图!
▓▓▓▓▓▓ 大致介绍 看到了一个轮播图的思路,就想的自己动手实践一下,总体来说用jQuery实现起来简单多了 如果对代码中使用的方法有疑问,可以参考我的jQuery学习之路(持续更新),里面有讲解: ...
- UVa11925 Generating Premutations
留坑(p.254) #include<cstdio> #include<cstring> #include<cstdlib> #include<algorit ...
- PopupWindow源码分析
PopupWindow是我们经常使用的一个控件,严格来说这个PopuWindow就用来在指定位置显示一个View. 经过分析源码,PopupWindow里面没有Window对象,只是把View设置到屏 ...
- 使用redis来实现分布式锁
在实际的工作中,有部分的特定场景需要使用到分布式锁来进行跨服务器资源的统一调配.之前在一家医疗互联网公司,因为黄牛抢号等原因,造成同一个患者同一时段在同一个医生处,挂到了两个及以上的号,我对之前我司实 ...
- 阿里云 centos 部署javaweb 应用
今天在阿里云上部署了个javaweb应用,在此记录下步骤,以供下次使用. 服务器版本: 1.root登陆服务器 2.服务器安装FTP服务,或者直接使用winscp上传文件(简单),本文介绍安装FTP服 ...
- SQL server数据库中的DateTime类型出现的问题
我们知道这个SQL server数据库中的DateTime类型是数据库应用开发中经经常使用到的一种数据类型.而C#语言中也有DateTime类型,尽管二者都是用来描写叙述时间的,可是它们的默认值是不同 ...
- docker 镜像中包含数据库环境和运行环境
需求: 一个镜像中要包含数据库环境和运行环境 Apache 环境 + mariadb 已经在拉取了Apache的运行环境 - 拉取代码 git https://github.com/timhaak/d ...