热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

PHP给图片加水印【php类】

PHP给图片加水印【php类】

加水印类

annexFolder = $annexFolder;
        $this->smallFolder = $smallFolder;
        $this->fOntType= $includeFolder."/04B_08__.TTF";
        }
function upLoad($inputName) {
      $imageName = time();//设定当前时间为图片名称
      if(@empty($_FILES[$inputName]["name"])) die("没有上传图片信息,请确认");
      $name = explode(".",$_FILES[$inputName]["name"]);//将上传前的文件以“.”分开取得文件类型
      $imgCount = count($name);//获得截取的数量
      $imgType = $name[$imgCount-1];//取得文件的类型
      if(strpos($this->upFileType,$imgType) === false) die(error("上传文件类型仅支持 ".$this->upFileType." 不支持 ".$imgType));
      $photo = $imageName.".".$imgType;//写入数据库的文件名
      $uploadFile = $this->annexFolder."/".$photo;//上传后的文件名称
      $upFileok = move_uploaded_file($_FILES[$inputName]["tmp_name"],$uploadFile);
      if($upFileok) {
              $imgSize = $_FILES[$inputName]["size"];
              $kSize = round($imgSize/1024);
              if($kSize > ($this->upFileMax*1024)) {
                    @unlink($uploadFile);
                    die(error("上传文件超过 ".$this->upFileMax."KB"));
                        }
                } else {
                        die(error("上传图片失败,请确认你的上传文件不超过 $upFileMax KB 或上传时间超时"));
                }
                return $photo;
        }
        function getInfo($photo) {
                $photo = $this->annexFolder."/".$photo;
                $imageInfo = getimagesize($photo);
                $imgInfo["width"] = $imageInfo[0];
                $imgInfo["height"] = $imageInfo[1];
                $imgInfo["type"] = $imageInfo[2];
                $imgInfo["name"] = basename($photo);
                return $imgInfo;
        }
        function smallImg($photo,$width=128,$height=128) {
                $imgInfo = $this->getInfo($photo);
                $photo = $this->annexFolder."/".$photo;//获得图片源
                $newName = substr($imgInfo["name"],0,strrpos($imgInfo["name"], "."))."_thumb.jpg";//新图片名称
                if($imgInfo["type"] == 1) {
                        $img = imagecreatefromgif($photo);
                } elseif($imgInfo["type"] == 2) {
                        $img = imagecreatefromjpeg($photo);
                } elseif($imgInfo["type"] == 3) {
                        $img = imagecreatefrompng($photo);
                } else {
                        $img = "";
                }
                if(empty($img)) return False;
                $width = ($width > $imgInfo["width"]) ? $imgInfo["width"] : $width;
                $height = ($height > $imgInfo["height"]) ? $imgInfo["height"] : $height;
                $srcW = $imgInfo["width"];
                $srcH = $imgInfo["height"];
                if ($srcW * $width > $srcH * $height) {
                        $height = round($srcH * $width / $srcW);
                } else {
                        $width = round($srcW * $height / $srcH);
                }
                if (function_exists("imagecreatetruecolor")) {
                        $newImg = imagecreatetruecolor($width, $height);
                        ImageCopyResampled($newImg, $img, 0, 0, 0, 0, $width, $height, $imgInfo["width"], $imgInfo["height"]);
                } else {
                        $newImg = imagecreate($width, $height);
                        ImageCopyResized($newImg, $img, 0, 0, 0, 0, $width, $height, $imgInfo["width"], $imgInfo["height"]);
                }
                if ($this->toFile) {
                        if (file_exists($this->annexFolder."/".$this->smallFolder."/".$newName)) @unlink($this->annexFolder."/".$this->smallFolder."/".$newName);
                        ImageJPEG($newImg,$this->annexFolder."/".$this->smallFolder."/".$newName);
                        return $this->annexFolder."/".$this->smallFolder."/".$newName;
                } else {
                        ImageJPEG($newImg);
                }
                ImageDestroy($newImg);
                ImageDestroy($img);
                return $newName;
        }
        function waterMark($photo,$text) {
                $imgInfo = $this->getInfo($photo);
                $photo = $this->annexFolder."/".$photo;
                $newName = substr($imgInfo["name"], 0, strrpos($imgInfo["name"], ".")) . "_mark.jpg";
                switch ($imgInfo["type"]) {
                        case 1:
                                $img = imagecreatefromgif($photo);
                        break;
                        case 2:
                                $img = imagecreatefromjpeg($photo);
                        break;
                        case 3:
                                $img = imagecreatefrompng($photo);
                        break;
                        default:
                                return False;
                }
                if (empty($img)) return False;
                $width = ($this->maxWidth > $imgInfo["width"]) ? $imgInfo["width"] : $this->maxWidth;
                $height = ($this->maxHeight > $imgInfo["height"]) ? $imgInfo["height"] : $this->maxHeight;
                $srcW = $imgInfo["width"];
                $srcH = $imgInfo["height"];
                if ($srcW * $width > $srcH * $height) {
                        $height = round($srcH * $width / $srcW);
                } else {
                        $width = round($srcW * $height / $srcH);
                }
                if (function_exists("imagecreatetruecolor")) {
                        $newImg = imagecreatetruecolor($width, $height);
                        ImageCopyResampled($newImg, $img, 0, 0, 0, 0, $width, $height, $imgInfo["width"], $imgInfo["height"]);
                } else {
                        $newImg = imagecreate($width, $height);
                        ImageCopyResized($newImg, $img, 0, 0, 0, 0, $width, $height, $imgInfo["width"], $imgInfo["height"]);
                }
                   
                $white = imageColorAllocate($newImg, 255, 255, 255);
                $black = imageColorAllocate($newImg, 0, 0, 0);
                $alpha = imageColorAllocateAlpha($newImg, 230, 230, 230, 40);
                ImageFilledRectangle($newImg, 0, $height-26, $width, $height, $alpha);
                ImageFilledRectangle($newImg, 13, $height-20, 15, $height-7, $black);
                ImageTTFText($newImg, 4.9, 0, 20, $height-14, $black, $this->fontType, $text[0]);
                ImageTTFText($newImg, 4.9, 0, 20, $height-6, $black, $this->fontType, $text[1]);
                if($this->toFile) {
                        if (file_exists($this->annexFolder."/".$this->markFolder."/".$newName)) @unlink($this->annexFolder."/".$this->markFolder."/".$newName);
                        ImageJPEG($newImg,$this->annexFolder."/".$this->markFolder."/".$newName);
                        return $this->annexFolder."/".$this->markFolder."/".$newName;
                } else {
                        ImageJPEG($newImg);
                }
                ImageDestroy($newImg);
                ImageDestroy($img);
                return $newName;
        }
}
?>
 

用法

 

include 'Upimages.class.php';
$max="upload";//文件上传路径
$mix="small";//缩略图路径(必须在upload下建立)
$mark="mark";//加水引的图片存放路径
$text = array("oktang","2012");//水印内容
$img= new UpImages($max,$mix,$max);//实例化类文件
$photo = $img->upLoad("file");//上传的文件域
$img->maxWidth = $img->maxHeight = 600;//设置高,和宽
$img->toFile = true;
$newSmallImg = $img->smallImg($photo); 
$newMark = $img->waterMark($photo,$text);
echo$newSmallImg;
echo$newMark;
echo"
 
";
echo"
 
";

 

 


推荐阅读
  • Matplotlib,带有已保 ... [详细]
  • 2018年人工智能大数据的爆发,学Java还是Python?
    本文介绍了2018年人工智能大数据的爆发以及学习Java和Python的相关知识。在人工智能和大数据时代,Java和Python这两门编程语言都很优秀且火爆。选择学习哪门语言要根据个人兴趣爱好来决定。Python是一门拥有简洁语法的高级编程语言,容易上手。其特色之一是强制使用空白符作为语句缩进,使得新手可以快速上手。目前,Python在人工智能领域有着广泛的应用。如果对Java、Python或大数据感兴趣,欢迎加入qq群458345782。 ... [详细]
  • GetWindowLong函数
    今天在看一个代码里头写了GetWindowLong(hwnd,0),我当时就有点费解,靠,上网搜索函数原型说明,死活找不到第 ... [详细]
  • 本文介绍了在Python3中如何使用选择文件对话框的格式打开和保存图片的方法。通过使用tkinter库中的filedialog模块的asksaveasfilename和askopenfilename函数,可以方便地选择要打开或保存的图片文件,并进行相关操作。具体的代码示例和操作步骤也被提供。 ... [详细]
  • 本文描述了作者第一次参加比赛的经历和感受。作者是小学六年级时参加比赛的唯一选手,感到有些紧张。在比赛期间,作者与学长学姐一起用餐,在比赛题目中遇到了一些困难,但最终成功解决。作者还尝试了一款游戏,在回程的路上感到晕车。最终,作者以110分的成绩取得了省一会的资格,并坚定了继续学习的决心。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 基于layUI的图片上传前预览功能的2种实现方式
    本文介绍了基于layUI的图片上传前预览功能的两种实现方式:一种是使用blob+FileReader,另一种是使用layUI自带的参数。通过选择文件后点击文件名,在页面中间弹窗内预览图片。其中,layUI自带的参数实现了图片预览功能。该功能依赖于layUI的上传模块,并使用了blob和FileReader来读取本地文件并获取图像的base64编码。点击文件名时会执行See()函数。摘要长度为169字。 ... [详细]
  • 搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的详细步骤
    本文详细介绍了搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的步骤,包括环境说明、相关软件下载的地址以及所需的插件下载地址。 ... [详细]
  • PHP图片截取方法及应用实例
    本文介绍了使用PHP动态切割JPEG图片的方法,并提供了应用实例,包括截取视频图、提取文章内容中的图片地址、裁切图片等问题。详细介绍了相关的PHP函数和参数的使用,以及图片切割的具体步骤。同时,还提供了一些注意事项和优化建议。通过本文的学习,读者可以掌握PHP图片截取的技巧,实现自己的需求。 ... [详细]
  • 关羽败走麦城时路过马超封地 马超为何没有出手救人
    对当年关羽败走麦城,恰好路过马超的封地,为啥马超不救他?很感兴趣的小伙伴们,趣历史小编带来详细的文章供大家参考。说到英雄好汉,便要提到一本名著了,没错,那就是《三国演义》。书中虽 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • PHP设置MySQL字符集的方法及使用mysqli_set_charset函数
    本文介绍了PHP设置MySQL字符集的方法,详细介绍了使用mysqli_set_charset函数来规定与数据库服务器进行数据传送时要使用的字符集。通过示例代码演示了如何设置默认客户端字符集。 ... [详细]
  • Java序列化对象传给PHP的方法及原理解析
    本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ... [详细]
  • 橱窗设计的表现手法及其应用
    本文介绍了橱窗设计的表现手法,包括直接展示、寓意与联想、夸张与幽默等。通过对商品的折、拉、叠、挂、堆等陈列技巧,橱窗设计能够充分展现商品的形态、质地、色彩、样式等特性。同时,寓意与联想可以通过象形形式或抽象几何道具来唤起消费者的联想与共鸣,创造出强烈的时代气息和视觉空间。合理的夸张和贴切的幽默能够明显夸大商品的美的因素,给人以新颖奇特的心理感受,引起人们的笑声和思考。通过这些表现手法,橱窗设计能够有效地传达商品的个性内涵,吸引消费者的注意力。 ... [详细]
author-avatar
samiensfe_663
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有