thinkphp给图片打水印不清晰
项目中打印条形码的函数,从thinkphp自带的water函数修改而来的。
贴上代码:
/**
* water2
* 改写thinkphp的water函数更强健的函数,增加了写入位置参数
去掉了alpha设定参数,以修正打出的水印不清晰的bug
* @param string $source 原图文件名
* @param string $water 水印图片文件名
* @param int $waterPos 要打水印的位置
* @param string $savename 要保存的图片名,如果留空则用source
* @return void
*/
function water2($source,$water,$waterPos=1,$savename=null)
{
import('ORG.Util.Image');
//检查文件是否存在
if(!file_exists($source)||!file_exists($water))
return false;
//图片信息
$sInfo=Image::getImageInfo($source);
$wInfo=Image::getImageInfo($water);
//如果图片小于水印图片,不生成图片
if($sInfo["width"]<$wInfo["width"] || $sInfo['height']<$wInfo['height'])
return false;
//建立图像
$sCreateFun="imagecreatefrom".$sInfo['type'];
$sImage=$sCreateFun($source);
$wCreateFun="imagecreatefrom".$wInfo['type'];
$wImage=$wCreateFun($water);
//设定图像的混色模式
imagealphablending($wImage, true);
switch($waterPos){
case 0://随机
$posX = rand(0,($sInfo["width"] - $wInfo["width"]));
$posY = rand(0,($sInfo["height"] - $wInfo["height"]));
break;
case 1://1为顶端居左
$posX = 8;
$posY = 8;
break;
case 2://2为顶端居中
$posX = ($sInfo["width"] - $wInfo["width"])/2;
$posY = 0;
break;
case 3://3为顶端居右
$posX = $sInfo["width"] - $wInfo["width"];
$posY = 0;
break;
case 4://4为中部居左
$posX = 0;
$posY = ($sInfo["height"] - $wInfo["height"])/2;
break;
case 5://5为中部居中
$posX = ($sInfo["width"] - $wInfo["width"])/2;
$posY = ($sInfo["height"] - $wInfo["height"])/2;
break;
case 6://6为中部居右
$posX = $sInfo["width"] - $wInfo["width"];
$posY = ($sInfo["height"] - $wInfo["height"])/2;
break;
case 7://7为底端居左
$posX = 0;
$posY = $sInfo["height"] - $wInfo["height"];
break;
case 8://8为底端居中
$posX = ($sInfo["width"] - $wInfo["width"])/2;
$posY = $sInfo["height"]- $wInfo["height"];
break;
case 9://9为底端居右
$posX = $sInfo["width"] - $wInfo["width"]-8;
$posY = $sInfo["height"] - $wInfo["height"]-8;
break;
default://随机
$posX = rand(0,($sInfo["width"] - $wInfo["width"]));
$posY = rand(0,($sInfo["height"] - $wInfo["height"]));
break;
}
//生成混合图像
//imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo['width'],$wInfo['height'],$alpha);
imagecopy($sImage, $wImage,$posX, $posY, 0, 0,$wInfo["width"],$wInfo["height"]);
//输出图像
$ImageFun='Image'.$sInfo['type'];
//如果没有给出保存文件名,默认为原图像名
if(!$savename){
$savename=$source;
@unlink($source);
}
//保存图像
$ImageFun($sImage,$savename);
imagedestroy($sImage);
}
thinkphp中Image.class.php的getImageInfo方法:
/**
+----------------------------------------------------------
* 取得图像信息
*
+----------------------------------------------------------
* @static
* @access public
+----------------------------------------------------------
* @param string $image 图像文件名
+----------------------------------------------------------
* @return mixed
+----------------------------------------------------------
*/
static function getImageInfo($img) {
$imageInfo = getimagesize($img);
if( $imageInfo!== false) {
if(function_exists(image_type_to_extension)){
$imageType = strtolower(substr(image_type_to_extension($imageInfo[2]),1));
}else{
$imageType = strtolower(substr($img,strrpos($img,'.')+1));
}
$imageSize = filesize($img);
$info = array(
"width"=>$imageInfo[0],
"height"=>$imageInfo[1],
"type"=>$imageType,
"size"=>$imageSize,
"mime"=>$imageInfo['mime']
);
return $info;
}else {
return false;
}
}
thinkphp中的water函数:
/**
+----------------------------------------------------------
* 为图片添加水印
+----------------------------------------------------------
* @static public
+----------------------------------------------------------
* @param string $source 原文件名
* @param string $water 水印图片
* @param string $$savename 添加水印后的图片名
* @param string $alpha 水印的透明度
+----------------------------------------------------------
* @return string
+----------------------------------------------------------
* @throws ThinkExecption
+----------------------------------------------------------
*/
static public function water($source,$water,$savename=null,$alpha=80)
{
//检查文件是否存在
if(!file_exists($source)||!file_exists($water))
return false;
//图片信息
$sInfo=self::getImageInfo($source);
$wInfo=self::getImageInfo($water);
//如果图片小于水印图片,不生成图片
if($sInfo["width"]<$wInfo["width"] || $sInfo['height']<$wInfo['height'])
return false;
//建立图像
$sCreateFun="imagecreatefrom".$sInfo['type'];
$sImage=$sCreateFun($source);
$wCreateFun="imagecreatefrom".$wInfo['type'];
$wImage=$wCreateFun($water);
//设定图像的混色模式
imagealphablending($wImage, true);
//图像位置,默认为右下角右对齐
$posY=$sInfo["height"]-$wInfo["height"];
$posX=$sInfo["width"]-$wInfo["width"];
//生成混合图像
imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo['width'],$wInfo['height'],$alpha);
//输出图像
$ImageFun='Image'.$sInfo['type'];
//如果没有给出保存文件名,默认为原图像名
if(!$savename){
$savename=$source;
@unlink($source);
}
//保存图像
$ImageFun($sImage,$savename);
imagedestroy($sImage);
}
thinkphp给图片打水印不清晰的更多相关文章
- thinkphp 3.2.3整合ueditor 1.4,给上传的图片加水印
今天分享一下thinkphp 3.2.3整合ueditor 1.4,给上传的图片加水印.博主是新手,在这里卡住了很久(>_<) thinkphp 3.2.3整合ueditor 1.4 下载 ...
- 火车头dede采集接口,图片加水印,远程图片本地化,远程无后缀的无图片本地化
<?php /* [LocoySpider] (C)2005-2010 Lewell Inc. 火车采集器 DedeCMS 5.7 UTF8 文章发布接口 Update content: 图片加 ...
- PHPThumb处理图片,生成缩略图,图片尺寸调整,图片截取,图片加水印,图片旋转
[强烈推荐]下载地址(github.com/masterexploder/PHPThumb). 注意这个类库有一个重名的叫phpThumb,只是大小写的差别,所以查找文档的时候千万注意. 在网站建设过 ...
- JAVA给图片加上水印
import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java ...
- php 分享两种给图片加水印的方法
本文章向码农们介绍 php 给图片加水印的两种方法,感兴趣的码农可以参考一下本文章的源代码. 方法一:PHP最简单的加水印方法 <?php // http://www.manongjc.com ...
- Java图片处理(二)图片加水印
图片加水印,是通过图片重叠绘制实现的.实现代码如下: public static void press(String pressImg, String pressText, String target ...
- Nginx Image Module图片缩略图 水印处理模块
Nginx Image Module图片缩略图 水印处理模块 下载Tengine tar -zxvf tengine-1.4.5.tar.gz cd tengine-1.4.5 下载Nginx tar ...
- javacpp-opencv图像处理之2:实时视频添加图片水印,实现不同大小图片叠加,图像透明度控制,文字和图片双水印
欢迎大家积极开心的加入讨论群 群号:371249677 (点击这里进群) javaCV图像处理系列: javaCV图像处理之1:实时视频添加文字水印并截取视频图像保存成图片,实现文字水印的字体.位置. ...
- 四:Java使用google的thumbnailator工具对图片压缩水印等做处理
Thumbnailator是一个非常好的图片开源工具 使用方法: 在pom中加入以下jar包 <!-- 图片缩略图 图片压缩 水印 start--> <dependency>& ...
随机推荐
- MyEclipse x.x各版本终极优化配置指南
先说优化:随着myeclipse版本不断更新,其功能不断强大,更加智能及人性化,为开发人员提供了很多便利.提高了开发速度,但是也牺牲了性能,让很多机器配置稍差的开发人员头疼不已.其实我们平时常用的功能 ...
- C# 基础知识 protected 关键字
using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace Console ...
- logback使用
须要的jar包: slf4j-api-1.7.7.jar logback-classic-1.1.2.jar logback-core-1.1.2.jar logback.xml配置文件,放在proj ...
- [Angular 2] ROUTING IN ANGULAR 2 REVISITED
Let's say we have a list of contacts, click each contact, we can render a new route to get the detai ...
- 动作-CCActionInterval之CCAnimation&CCAnimate
动画简单实例 v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#def ...
- 通过ftp模拟网盘
package com.xiaomo.ftp.down_upload; import java.io.IOException; import java.util.ArrayList; import j ...
- Using Sessions and Session Persistence---reference
Using Sessions and Session Persistence The following sections describe how to set up and use session ...
- 在HTML中怎么去掉a标签(超链接)的下划线?
<style type="text/css">a:link,a:visited{ text-decoration:none; /*超链接无下划线*/}a:hover{ ...
- SQLServer获取最后插入的ID值SCOPE_IDENTITY、IDENT_CURRENT 和 @@IDENTITY的比较
IDENT_CURRENT 返回为任何会话和任何作用域中的特定表最后生成的标识值.IDENT_CURRENT 不受作用域和会话的限制,而受限于指定的表. @@IDENTITY 返回为当前会话的所有作用 ...
- SQL SERVER HTTP请求
--开启Sql Server 通讯配置-- sp_configure ; GO RECONFIGURE WITH OVERRIDE; GO sp_configure ; GO RECONFIGURE ...