PHP学习创建水印,缩略图
今天网上学习了一段PHP创建缩略图还有打水印的代码,如下:
其中将图片的路径作为参数传给函数,打水印的过程就是首先获取图片和logo的参数信息,然后将logo图片拷贝到原图的某个位置,然后保存,水印打入完毕。
创建缩略图就是将缩放比例传入函数,然后调整元图片的宽度和高度,进行保存。
感觉这段代码当中比较巧妙地就是用到了图片的路径保存还有就是这个大量运用到PHP提供的图片处理函数,这点是非常重要的,没见过的函数要多查查手册,大体就能理解功能了 。
图片处理类:
<?php
class Image_process{
public $source;//原图
public $source_width;//宽
public $source_height;//高
public $source_type_id;//原图的类型
public $orign_name;//文件名
public $orign_dirname;//路径名 public function __construct($source){ //传入图片路径作为参数
$this->typeList = array(1=>'gif',2=>'jpg',3=>'png');
$ginfo = getimagesize($source); //获取图片的参数
$this->source_width = $ginfo[0];
$this->source_height = $ginfo[1];
$this->source_type_id= $ginfo[2];
$this->orign_url = $source;
$this->orign_name = basename($source);
$this->orign_dirname = dirname($source);
} public function judgeType($type,$source){ //判断并处理,返回PHP可识别编码
if($type==1){
return ImageCreateFromGIF($source);//gif
}else if($type==2){
return ImageCreateFromJPEG($source);//jpg
}else if($type==3){
return ImageCreateFromPNG($source);//png
}else{
return false;
}
} public function watermarkImage($logo){ //生成水印图
$linfo = getimagesize($logo);
$logo_width = $linfo[0];
$logo_height = $linfo[1];
$logo_type_id = $linfo[2];
$sourceHandle = $this->judgeType($this->source_type_id,$this->orign_url);
$logoHandle = $this->judgeType($logo_type_id,$logo); if( !$sourceHandle || ! $logoHandle ){
return false;
}
$x = $this->source_width - $logo_width;
$y = $this->source_height- $logo_height; ImageCopy($sourceHandle,$logoHandle,$x,$y,0,0,$logo_width,$logo_width) or die("fail to combine");
$newPic = $this->orign_dirname .'\water_'. time().'.'. $this->typeList[$this->source_type_id];
//将小logo作为水印拷贝到原图片的某个位置
if( $this->saveImage($sourceHandle,$newPic)){ //保存将水印写入
imagedestroy($sourceHandle);
imagedestroy($logoHandle);
}
} // fix 宽度
// height = true 固顶高度
// width = true 固顶宽度
public function fixSizeImage($width,$height){
if( $width > $this->source_width) $this->source_width;
if( $height > $this->source_height ) $this->source_height;
if( $width === false){
$width = floor($this->source_width / ($this->source_height / $height));
}
if( $height === false){
$height = floor($this->source_height / ($this->source_width / $width));
}
$this->tinyImage($width,$height);
} //比例缩放
// $scale 缩放比例
public function scaleImage($scale){
$width = floor($this->source_width * $scale); //按照scale比例将原图进行缩放
$height = floor($this->source_height * $scale);
$this->tinyImage($width,$height);
} //创建略缩图
private function tinyImage($width,$height){
$tinyImage = imagecreatetruecolor($width, $height );
$handle = $this->judgeType($this->source_type_id,$this->orign_url);
if(function_exists('imagecopyresampled')){
imagecopyresampled($tinyImage,$handle,0,0,0,0,$width,$height,$this->source_width,$this->source_height);
}else{
imagecopyresized($tinyImage,$handle,0,0,0,0,$width,$height,$this->source_width,$this->source_height);
} $newPic = time().'_'.$width.'_'.$height.'.'. $this->typeList[$this->source_type_id];
$newPic = $this->orign_dirname .'\thumb_'. $newPic;
if( $this->saveImage($tinyImage,$newPic)){
imagedestroy($tinyImage);
imagedestroy($handle);
}
} //保存图片
private function saveImage($image,$url){
if(ImageJpeg($image,$url)){
return true;
}
}
}
?>
调用以上类代码:
<?php
include('image_process.class.php');
$m = array(
'D:\localhost\1.jpg',
'D:\localhost\2.jpg',
'D:\localhost\3.jpg',
'D:\localhost\4.jpg'
);
//$img = 'D:\localhost\1.jpg';
$logo = 'D:\localhost\logo.jpg';
foreach( $m as $item){
$s = new Image_process( $item );
$s->watermarkImage($logo);//生成水印
$s->scaleImage(0.8);//生成缩略图
$s->fixSizeImage(200,false);
sleep(1);
echo $m." is done"."<br/>";
}
?>
以上参考:http://www.oschina.net/code/snippet_163553_37508#55208
PHP学习创建水印,缩略图的更多相关文章
- Spring MVC 学习 -- 创建过程
Spring MVC 学习 -- 创建过程 Spring MVC我们使用的时候会在web.xml中配置 <servlet> <servlet-name>SpringMVC< ...
- pandas学习(创建多层索引、数据重塑与轴向旋转)
pandas学习(创建多层索引.数据重塑与轴向旋转) 目录 创建多层索引 数据重塑与轴向旋转 创建多层索引 隐式构造 Series 最常见的方法是给DataFrame构造函数的index参数传递两个或 ...
- java核心知识点学习----创建线程的第三种方式Callable和Future CompletionService
前面已经指出通过实现Runnable时,Thread类的作用就是将run()方法包装成线程执行体,那么是否可以直接把任意方法都包装成线程执行体呢?Java目前不行,但其模仿者C#中是可以的. Call ...
- 使用AVFoundation仅仅生成缩略图,不进行播放视频(本地和网络文件都可以创建视频缩略图)
使用MPMoviePlayerController来生成缩略图足够简单,但是如果仅仅是是为了生成缩略图而不进行视频播放的话,此刻使用 MPMoviePlayerController就有点大材小用了.其 ...
- PythonOCC 3D图形库学习—创建立方体模型
Open CASCADE(简称OCC)平台是是一个开源的C++类库,OCC主要用于开发二维和三维几何建模应用程序,包括通用的或专业的计算机辅助设计CAD系统.制造或分析领域的应用程序.仿真应用程序或图 ...
- Nodejs 菜鸟教程学习-创建第一个应用
注:为了解学习,都是参照http://www.runoob.com/nodejs/nodejs-tutorial.html书写,做下笔记. 对于Nodejs开发来说,在开发一个应用时,我们不仅仅是实现 ...
- lucene&solr学习——创建和查询索引(代码篇)
1. Lucene的下载 Lucene是开发全文检索功能的工具包,从官网下载Lucene4.10.3并解压. 官网:http://lucene.apache.org/ 版本:lucene7.7.0 ( ...
- Git学习--创建版本库
什么是版本库呢?版本库又名仓库,英文名repository,你可以简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,每个文件的修改.删除,Git都能跟踪,以便任何时刻都可以追踪历史,或 ...
- UCOSII学习 - 创建任务
本人刚刚学习UCOSII,平台为正点原子的STM32F103战舰开发板,写这篇博客主要是为了学习UCOSII,也方便自己能够一点一点的进步,话不多说直入正题吧. 第一步:在STM32上移植好UCOSI ...
随机推荐
- Yii2 自定义独立验证器
新建一个文件: ?php /** * author : forecho <caizhenghai@gmail.com> * createTime : 2015/7/1 14:54 * de ...
- Windows读写文件的猫腻
这里主要涉及对于回车换行的讨论. 回车:\r 换行:\n Windows读写文件分为普通文件读写和二进制文件读写. 如果以二进制的方式读写文件(如rb, wb),将会完全的把文件内容读出来,不做任何处 ...
- RocketMq入坑指南
报错信息Caused by: org.apache.rocketmq.remoting.exception.RemotingConnectException: connect to <172.1 ...
- php自带函数去除html标记
strip_tags 去掉 HTML 及 PHP 的标记. 语法: string strip_tags(string str); 传回值: 字串 函式种类: 资料处理 内容说明 本函式可去掉字串中包含 ...
- 跨平台移动开发 手机上使用Iscroll.Js的Banner
二话不说,直接上图,看效果 需要使用Iscroll <script src="content/scripts/iscroll.js"></script> 示 ...
- 在shell,R,python中用变量和常量创建文件名
很多时候我们希望文件名的格式是:变量+常量的. 1.shell:变量"常量" [wangjq@mgmt multi_pcr]$ a="var" [wangjq@ ...
- 关于读取本地text文件,自动被添加空格的问题
最近做一个小程序,读取本地指定路径下的text文件,逐行获取text文本然后再进行处理,结果遇到了一个奇葩问题,先插个图片给各位看官 坑:本地text文件中数据为1123/10(数据反复检查无空格,换 ...
- 一个gpio 不受控制的bug
前几天调试一个flash灯的驱动程序,这可ic 有两个控制pin, 一个叫en1 一个叫en2, 根据spec的说明,不同的组合将产生不同的输出电流.但我发现,那个en1 这个pin 死活是拉不高的, ...
- JAVA 使用qq邮箱发送邮件
引入一个架包: gradle( "com.sun.mail:javax.mail:1.5.6", ) 代码如下: private static final String QQ_EM ...
- mysqldump 的常用操作
以下是 mysqldump 的一些使用参数 备份数据库#mysqldump 数据库名 >数据库备份名 #mysqldump -A -u用户名 -p密码 数据库名>数据库备份名 #mysql ...