PHP文字水印,缩略图,图片水印实现类与用法示例

  本文实例讲述了PHP开发的文字水印,缩略图,图片水印实现类与用法。分享给大家供大家参考,具体如下:

  1.实现类ImageToTest.class.php参考代码

class ImageToTest {
  /**
   * 图片的基本信息
   */
  private $info;
  private $image;
  public function __construct($src){
    $info = getimagesize($src);
    $this->info = array(
      'width'=> $info[0],
      'height'=> $info[1],
      'type'=> image_type_to_extension($info[2],false),
      'mime'=>$info['mime']
    );
    $fun = "imagecreatefrom{$this->info['type']}";
    $this->image = $fun($src);
  }
  /**
   * 操作图片 (压缩)
   */
  public function thumb($width,$height){
    $image_thumb = imagecreatetruecolor($width,$height);
    imagecopyresampled($image_thumb,$this->image,0,0,0,0,$width,$height,
      $this->info['width'],$this->info['height']);
    imagedestroy($this->image);
    $this->image = $image_thumb;
  }
  /**
   * 操作图片(添加文字水印)
   */
  public function fontMark($content,$font_url,$size,$color,$local,$angle){
    $col = imagecolorallocatealpha($this->image,$color[0],$color[1],$color[2],$color[3]);
    imagettftext($this->image,$size,$angle,$local['x'],$local['y'],$col,$font_url,$content);
  }
  /**
   * 操作图片(添加水印图片)
   */
  public function imageMark($source,$local,$alpha){
    //1.获取水印图片的基本信息
    $info2 = getimagesize($source);
    //2.通过水印的图片编号来获取水印的图片类型
    $type2 = image_type_to_extension($info2[2],false);
    //3.在内存中创建一个和我们的水印图像一致的图像类型
    $func2 = "imagecreatefrom{$type2}";
    //4.把水印图片复制到内存中
    $water = $func2($source);
    //5.合并图片
    imagecopymerge($this->image,$water,$local['x'],$local['y'],0,0,$info2[0],$info2[1],$alpha);
    //6.销毁水印图片
    imagedestroy($water);
  }
  /**
   * 在浏览器中输出图片
   */
  public function show(){
    header("Content-type:".$this->info['mime']);
    $funs = "image{$this->info['type']}";
    $funs($this->image);
  }
  /**
   * 把图片保存到硬盘里
   */
  public function save($newName){
    $funs = "image{$this->info['type']}";
    $funs($this->image,'./outPut/'.$newName.'.'.$this->info['type']);
  }
  /**
   * 销毁图片 使用析构函数
   */
  public function __destruct()
  {
    imagedestroy($this->image);
  }
}

  2.测试参考代码

require_once('ImageToTest.class.php');
/*$src = './image/wbg.jpg';
$image = new ImageToTest($src);
$image->thumb(700,550);
$image->show();*/
/*$src2 = './image/wbg.jpg';
$content = 'SGC';
$font_url = './image/YGYcuhei.ttf';
$size = 33;
$color = array(
  0=>2,
  1=>222,
  2=>222,
  3=>60
);
$local = array(
  'x'=>20,
  'y'=>100
);
$angle = 10;
$image2 = new ImageToTest($src2);
$image2->fontMark($content,$font_url,$size,$color,$local,$angle);
$image2->show();
$image2->save('hahahah');*/
$src3 = './image/wbg.jpg';
$source = './image/water.jpg';
$local = array(
  'x'=>20,
  'y'=>100
);
$font_url = './image/YGYcuhei.ttf';
$size = 38;
$color = array(
  0=>2,
  1=>222,
  2=>222,
  3=>60
);
$alpha = 60;
$angle = 50;
$image3 = new ImageToTest($src3);
$image3->imageMark($source,$local,$alpha);
$image3->thumb(700,550);
$image3->fontMark('Hello',$font_url,$size,$color,$local,$angle);
$image3->show();
$image3->save('WAWAWAWAWA');

本站原创内容,转载请注明来源:https://www.liutonghui.com/185.html

Mysql中utf8和utf8mb4区别
上一篇 2020-07-10
PHP生成任意尺寸并居中裁剪缩略图的函数
下一篇 2020-10-03

评论列表(0条)

  • 暂无评论

发表评论

captcha

相关推荐

  • PHP利用正则表达式提取内容中图片的函数

      用PHP开发产品中经常需要从某部分内容中提取出图片,比如在一个表单中,我们需要从内容中提取出图片的路径当作缩略图,这时就可以用正则表达式的规则匹配的方法来提取出图片路径,利用下面这个函数来完成。 <?php function getImgs($content,$order='ALL'){ $pattern="//"; preg_match_all($pattern,$content,$match); ......

    2015-01-07
    23820
  • jQuery实现文件列表和缩略图模式切换

      做一个简单的仿网盘前端页面,实现文件以列表模式和缩略图模式切换功能。 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>列表/缩略图模式切换</title> <script src="https://code.jquery.com/jquery-1......

    2015-03-09
    17250
  • Dedecms文章内容页和图片集内容页调用缩略图的方法

      织梦DedeCMS的缩略图一般显示在列表页,但有时我们需要在文章内容页缩略图的调用,图片集内容页缩略图的调用,使用方法如下:   文章内容页和图片集内容页,缩略图的调用。适合内页中调用。 <img src="{dede:field.picname runphp='var_dmp(@me)' /}"/>   文章频道页、列表页调用缩略图: {dede:list pagesize='20'} [field:array ru......

    2015-04-29
    28090
  • PHP框架的实现原理

      PHP框架的实现原理是通过采用MVC模式、路由机制、依赖注入、模板引擎和数据库操作等技术,构建一个结构清晰、易于维护和扩展的应用程序框架。下面是一个简单的PHP框架示例,演示了基本的实现原理:   简单的PHP框架文件结构 /simple_framework |-- app | |-- controllers | | |-- HomeController.php | |-- models | | ......

    2017-10-04
    15492
  • PHP封装数据库操作Model类示例

      数据库操作是产品开发中最常见的需求,封装一个PHP数据库操作Model类可以减少反复的写大量SQL语句,下面就为大家分享一个数据库操作Model类。 <?php //引入配置文件 include "./config.php"; class Model { public $link;//存储连接对象 public $tableName = "";//存储表名 public $field =......

    2019-06-12
    21024
  • PHP生成任意尺寸并居中裁剪缩略图的函数

      在项目中经常需要对图片进行各种处理,比如生成缩略图,并且生成的缩略图还要保持原有图片的宽高比例不变形,下面就分享一个非常好用的PHP生成缩略图函数,可以手动输入自定义尺寸并居中裁剪保持图片比例不变形,代码如下: <?php // 应用公共文件 /** * 居中裁剪图片 * @param string $source [原图路径] * @param int $width [设置宽度] * @param int $heig......

    2020-10-03
    19932
  • ThinkPHP6开发wangEditor图片上传接口的方法

      wangEditor是一款近几年非常流行的富文本编辑器,它界面简洁,功能非常强大,本文介绍WangEditor图片上传接口的开发,以Thinkphp6为例。   首先看一下wangEditor官方的接口返回要求:   然后开发Thinkphp6的上传功能: public function testup(){ $file = request()->file('file'); $savename = \think......

    2021-03-02
    19030
  • 解决 PHP 8.1 隐式类型转换导致缩略图生成错误

      这几天解决了一个困扰已久的问题,我的博客系统在文章上传图片生成缩略图时,在本地测试环境没有任何问题,但在服务器环境中总是生成缩略图失败,报错:"Implicit conversion from float"。排查了 PHP 和环境扩展组件库、目录权限、图片格式大小等都没有问题。在百思不得其解时,我注意到了一个平时不太注意的细节,我在本地测试环境用的是 PHP 8.0,服务器上用的是 PHP 8.1,经过反复测试问题果然出在这里。  ......

    2023-09-04
    13310