服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - PHP教程 - PHP实现可添加水印与生成缩略图的图片处理工具类

PHP实现可添加水印与生成缩略图的图片处理工具类

2019-10-25 12:47woider PHP教程

这篇文章主要介绍了PHP实现可添加水印与生成缩略图的图片处理工具类,涉及php针对图片的显示、保存、压缩、水印等相关操作技巧,需要的朋友可以参考下

本文实例讲述了PHP实现可添加水印与生成缩略图的图片处理工具类。分享给大家供大家参考,具体如下:

ImageTool.class.php

  1. <?php 
  2. class ImageTool 
  3.   private $imagePath;//图片路径 
  4.   private $outputDir;//输出文件夹 
  5.   private $memoryImg;//内存图像 
  6.   public function __construct($imagePath, $outputDir = null
  7.   { 
  8.     $this->imagePath = $imagePath; 
  9.     $this->outputDir = $outputDir; 
  10.     $this->memoryImg = null
  11.   } 
  12.   /** 
  13.    * 显示内存中的图片 
  14.    * @param $image 
  15.    */ 
  16.   public function showImage() 
  17.   { 
  18.     if ($this->memoryImg != null) { 
  19.       $info = getimagesize($this->imagePath); 
  20.       $type = image_type_to_extension($info[2], false); 
  21.       header('Content-type:' . $info['mime']); 
  22.       $funs = "image{$type}"
  23.       $funs($this->memoryImg); 
  24.       imagedestroy($this->memoryImg); 
  25.       $this->memoryImg = null
  26.     } 
  27.   } 
  28.   /**将图片以文件形式保存 
  29.    * @param $image 
  30.    */ 
  31.   private function saveImage($image) 
  32.   { 
  33.     $info = getimagesize($this->imagePath); 
  34.     $type = image_type_to_extension($info[2], false); 
  35.     $funs = "image{$type}"
  36.     if (empty($this->outputDir)) { 
  37.       $funs($image, md5($this->imagePath) . '.' . $type); 
  38.     } else { 
  39.       $funs($image, $this->outputDir . md5($this->imagePath) . '.' . $type); 
  40.     } 
  41.   } 
  42.   /** 
  43.    * 压缩图片 
  44.    * @param $width 压缩后宽度 
  45.    * @param $height 压缩后高度 
  46.    * @param bool $output 是否输出文件 
  47.    * @return resource 
  48.    */ 
  49.   public function compressImage($width, $height, $output = false
  50.   { 
  51.     $image = null
  52.     $info = getimagesize($this->imagePath); 
  53.     $type = image_type_to_extension($info[2], false); 
  54.     $fun = "imagecreatefrom{$type}"
  55.     $image = $fun($this->imagePath); 
  56.     $thumbnail = imagecreatetruecolor($width, $height); 
  57.     imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $width, $height, $info[0], $info[1]); 
  58.     imagedestroy($image); 
  59.     if ($output) { 
  60.       $this->saveImage($thumbnail); 
  61.     } 
  62.     $this->memoryImg = $thumbnail; 
  63.     return $this
  64.   } 
  65.   /** 
  66.    * 为图像添加文字标记 
  67.    * 
  68.    * @param $content 文本内容 
  69.    * @param $size 字体大小 
  70.    * @param $font 字体样式 
  71.    * @param bool $output 是否输出文件 
  72.    * @return $this 
  73.    */ 
  74.   public function addTextmark($content, $size, $font, $output = false
  75.   { 
  76.     $info = getimagesize($this->imagePath); 
  77.     $type = image_type_to_extension($info[2], false); 
  78.     $fun = "imagecreatefrom{$type}"
  79.     $image = $fun($this->imagePath); 
  80.     $color = imagecolorallocatealpha($image, 0, 0, 0, 80); 
  81.     $posX = imagesx($image) - strlen($content) * $size / 2; 
  82.     $posY = imagesy($image) - $size / 1.5; 
  83.     imagettftext($image, $size, 0, $posX, $posY, $color, $font, $content); 
  84.     if ($output) { 
  85.       $this->saveImage($image); 
  86.     } 
  87.     $this->memoryImg = $image; 
  88.     return $this
  89.   } 
  90.   /** 
  91.    * 为图片添加水印 
  92.    * 
  93.    * @param $watermark 水印图片路径 
  94.    * @param $alpha 水印透明度(0-100) 
  95.    * @param bool $output 是否输出文件 
  96.    * @return $this 
  97.    */ 
  98.   public function addWatermark($watermark, $alpha, $output = false
  99.   { 
  100.     $image_info = getimagesize($this->imagePath); 
  101.     $image_type = image_type_to_extension($image_info[2], false); 
  102.     $image_fun = "imagecreatefrom{$image_type}"
  103.     $image = $image_fun($this->imagePath); 
  104.     $mark_info = getimagesize($watermark); 
  105.     $mark_type = image_type_to_extension($mark_info[2], false); 
  106.     $mark_fun = "imagecreatefrom{$mark_type}"
  107.     $mark = $mark_fun($watermark); 
  108.     $posX = imagesx($image) - imagesx($mark); 
  109.     $posY = imagesy($image) - imagesy($mark); 
  110.     imagecopymerge($image, $mark, $posX, $posY, 0, 0, $mark_info[0], $mark_info[1], $alpha); 
  111.     if ($output) { 
  112.       $this->saveImage($image); 
  113.     } 
  114.     $this->memoryImg = $image; 
  115.     return $this
  116.   } 

ImageTool使用

首先导入ImageTool工具:

  1. require_once 'ImageTool.class.php'

然后实例化ImageTool对象:

  1. $imageTool = new ImageTool('img/oppman.jpeg''out/');//图片路径、输出文件夹 

一、生成压缩图片

  1. $imageTool->compressImage(350, 250, true);//压缩宽度、压缩高度、是否保存 
  2. $imageTool->showImage(); 

 

PHP实现可添加水印与生成缩略图的图片处理工具类

二、添加文字水印

  1. $imageTool->addTextmark('一拳超人', 50, 'res/micro.ttf'true);//内容、尺寸、字体、是否保存 
  2. $imageTool->showImage(); 

 

PHP实现可添加水印与生成缩略图的图片处理工具类

三、添加图片水印

  1. $imageTool->addWatermark('res/logo.jpeg', 100, true);//水印路径、透明度、是否保存 
  2. $imageTool->showImage(); 

 

PHP实现可添加水印与生成缩略图的图片处理工具类

仅当做临时图像输出:

  1. $imageTool->addTextmark('快捷输出', 50, 'res/micro.ttf')->showImage(); 

希望本文所述对大家PHP程序设计有所帮助。

延伸 · 阅读

精彩推荐