今天网上学习了一段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. MapX小试

    需MapX 控件 string layerName = "12Q3_new"; string tabFile = string.Format(@"E:\map\地图\现在 ...

  2. marquee标记

    页面的自动滚动效果,可由javascript来实现, 但是有一个html标签 - <marquee></marquee>可以实现多种滚动效果,无需js控制. 使用marquee ...

  3. 留言处插入xss不弹框

    对于新手来说,往往会在留言地方插入<script>alert(1)</script>来检测是否有存储xss,事实是基本上不会弹框的,为啥? 通过查看源码,可知道<> ...

  4. loadrunder之脚本篇——脚本基础知识和常用操作

    1)编码工具设置 自动补全输入Tools->General Options->Environment->Auto complete word 显示功能语法Tools->Genr ...

  5. Zuul

    一.zuul是什么 zuul 是netflix开源的一个API Gateway 服务器, 本质上是一个web servlet应用. Zuul 在云平台上提供动态路由,监控,弹性,安全等边缘服务的框架. ...

  6. Socke---转

      Socket,又称为套接字,Socket是计算机网络通信的基本的技术之一.如今大多数基于网络的软件,如浏览器,即时通讯工具甚至是P2P下载都是基于Socket实现的.本文会介绍一下基于TCP/IP ...

  7. 基于R语言的数据分析和挖掘方法总结——描述性统计

    1.1 方法简介 描述性统计包含多种基本描述统计量,让用户对于数据结构可以有一个初步的认识.在此所提供之统计量包含: 基本信息:样本数.总和 集中趋势:均值.中位数.众数 离散趋势:方差(标准差).变 ...

  8. 【leetcode刷题笔记】Wildcard Matching

    Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...

  9. 【leetcode刷题笔记】Insert Interval

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  10. PHP操作MongoDB数据库的示例

    http://www.jquerycn.cn/a_8137 本节内容:PHP操作MongoDB数据库的简单示例. Mongodb的常用操作参看手册,php官方的http://us2.php.net/m ...