用到两个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. 为xampp 安装pear db (database) 模块

    pear channel-update pear.php.netpear install db

  2. iptables vpn

  3. 去除 Visual Studio 中臃肿的 ipch 和 sdf 文件

    使用VS2010建立C++解决方案时,会生成SolutionName.sdf和一个叫做ipch的文件夹,这两个文件再加上*.pch等文件使得工程变得非常的庞大,一个简单的程序都会占用几十M的硬盘容量, ...

  4. (组合数学3.1.1.1)POJ 1146 ID Codes(字典序法)

    /* * POJ_1146.cpp * * Created on: 2013年10月8日 * Author: Administrator */ #include <iostream> #i ...

  5. 外显子分析:cutadapt,去除序列adapter详细解析

    外显子测序时带有adapt接头,因此我们需要去除adapt接头,cutadapt的作用是去除adapt接头,一般用到如下命令: cutadapt -a AACCGGTT -o output.fastq ...

  6. C语言学习_include<>与include""的区别

    经常会遇到两种include引用头文件的情况,其实区别很简单,如下: 一.#include< > #include< > 引用的是编译器的类库路径里面的头文件. 假如你编译器定 ...

  7. python_将一组数据展示成直方图(以list为例)

    直接上代码: from matplotlib import pyplot as plt # 参数依次为list,抬头,X轴标签,Y轴标签,XY轴的范围 def draw_hist(myList,Tit ...

  8. c# 字符串转化成声音 分类: C# 2014-09-24 12:20 316人阅读 评论(0) 收藏

    说明: (1)支持Window 7系统,但是xp系统智能朗读英文和数字: (2)添加引用 Interop.SpeechLib.dll; (3)使用时调用StringToVoice(str)即可. us ...

  9. Doctor NiGONiGO’s multi-core CPU(最小费用最大流模板)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=693 题意:有一个 k 核的处理器和 n 个工作,全部的工作都须要在一个核上处理一个单位的 ...

  10. UITableView的简单应用介绍

    创建一个tableView视图,然后把这个视图界面添加到主界面上. _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, [ ...