今天网上学习了一段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学习创建水印,缩略图的更多相关文章

  1. Spring MVC 学习 -- 创建过程

    Spring MVC 学习 -- 创建过程 Spring MVC我们使用的时候会在web.xml中配置 <servlet> <servlet-name>SpringMVC< ...

  2. pandas学习(创建多层索引、数据重塑与轴向旋转)

    pandas学习(创建多层索引.数据重塑与轴向旋转) 目录 创建多层索引 数据重塑与轴向旋转 创建多层索引 隐式构造 Series 最常见的方法是给DataFrame构造函数的index参数传递两个或 ...

  3. java核心知识点学习----创建线程的第三种方式Callable和Future CompletionService

    前面已经指出通过实现Runnable时,Thread类的作用就是将run()方法包装成线程执行体,那么是否可以直接把任意方法都包装成线程执行体呢?Java目前不行,但其模仿者C#中是可以的. Call ...

  4. 使用AVFoundation仅仅生成缩略图,不进行播放视频(本地和网络文件都可以创建视频缩略图)

    使用MPMoviePlayerController来生成缩略图足够简单,但是如果仅仅是是为了生成缩略图而不进行视频播放的话,此刻使用 MPMoviePlayerController就有点大材小用了.其 ...

  5. PythonOCC 3D图形库学习—创建立方体模型

    Open CASCADE(简称OCC)平台是是一个开源的C++类库,OCC主要用于开发二维和三维几何建模应用程序,包括通用的或专业的计算机辅助设计CAD系统.制造或分析领域的应用程序.仿真应用程序或图 ...

  6. Nodejs 菜鸟教程学习-创建第一个应用

    注:为了解学习,都是参照http://www.runoob.com/nodejs/nodejs-tutorial.html书写,做下笔记. 对于Nodejs开发来说,在开发一个应用时,我们不仅仅是实现 ...

  7. lucene&solr学习——创建和查询索引(代码篇)

    1. Lucene的下载 Lucene是开发全文检索功能的工具包,从官网下载Lucene4.10.3并解压. 官网:http://lucene.apache.org/ 版本:lucene7.7.0 ( ...

  8. Git学习--创建版本库

    什么是版本库呢?版本库又名仓库,英文名repository,你可以简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,每个文件的修改.删除,Git都能跟踪,以便任何时刻都可以追踪历史,或 ...

  9. UCOSII学习 - 创建任务

    本人刚刚学习UCOSII,平台为正点原子的STM32F103战舰开发板,写这篇博客主要是为了学习UCOSII,也方便自己能够一点一点的进步,话不多说直入正题吧. 第一步:在STM32上移植好UCOSI ...

随机推荐

  1. Ubuntu 下 java 版本的切换

    切换的方法很简单,使用下面的两个命令即可: update-alternatives --config java update-alternatives --config javac eg: root@ ...

  2. pyhton3 sys模块

    Python常用模块之sys sys模块提供了一系列有关Python运行环境的变量和函数. 1 ). sys.stdin 标准输入流.2 ).sys.stdout 标准输出流.3 ). sys.std ...

  3. Python自然语言处理-系列一

    一:python基础,自然语言概念 from nltk.book import * 1,text1.concordance("monstrous")      用语索引 2,tex ...

  4. 【HackerRank】 Sherlock and The Beast

    Sherlock and The Beast Sherlock Holmes is getting paranoid about Professor Moriarty, his archenemy. ...

  5. hadoop实战项目:查找相同字母组成的字谜

    前面我们学习了MapReduce编程思想和编程示例,那么本节课程同学们一起操练操练,动手完成下面的项目. 项目需求 一本英文书籍包含成千上万个单词或者短语,现在我们需要在大量的单词中,找出相同字母组成 ...

  6. django大全

    数据库配置: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME':'dbname', 'USER': 'ro ...

  7. SwfUpload文件上传

    SWFUpload是一个flash和js相结合而成的文件上传插件,其功能非常强大.以前在项目中用过几次,但它的配置参数太多了,用过后就忘记怎么用了,到以后要用时又得到官网上看它的文档,真是太烦了.所以 ...

  8. XXL-Job分布式任务调度

    分布式情况下定时任务会出现哪些问题? 分布式集群的情况下,怎么保证定时任务不被重复执行 分布式定时任务解决方案 ①使用zookeeper实现分布式锁 缺点(需要创建临时节点.和事件通知不易于扩展) ② ...

  9. Mysql -- You can't specify target table 'address' for update in FROM clause

    做地址管理时,需要先根据要设为默认的地址的用户将用户的其他地址都设置为非默认 需要select出用户id然后update 原语句 update address set isdeafult = 0 wh ...

  10. 操作文件和目录【TLCL】

    cp – Copy files and directories mv – Move/rename files and directories mkdir – Create directories rm ...