php图片处理类(生成缩略图,增加水印,获取图片信息)

本文章提供这款图片处理类,他可以做的事情有把图片生成缩略图,可能给图片增加水印以及获取图片信息,算是比较实用代码又简洁的函数*/

 代码如下 复制代码

class image
{
 public $info=array();

 function __construct()
 {
  !extension_loaded('gd') && exit("www.111cn.net提示:服务器环境不支持gd库");
  return true;
 }

 function image()
 {
  $this->__construct();
 }
 
 function thumb($image,$thumb_width=300,$thumb_height=225)
 {
  $info=$this->info($image);
  $scale=min(1,min($thumb_width/$info['width'],$thumb_height/$info['height'])); //按比例缩放
  $thumb_width=intval($info['width']*$scale);
  $thumb_height=intval($info['height']*$scale);
  $createfunc='imagecreatefrom'.($info['type']=='jpg'?'jpeg':$info['type']);
  $im=$createfunc($image);
  $thumb_im=$info['type']!='gif' && function_exists('imagecreatetruecolor')?imagecreatetruecolor($thumb_width,$thumb_height):imagecreate($thumb_width,$thumb_height);
  imagecopyresampled($thumb_im,$im,0,0,0,0,$thumb_width,$thumb_height,$info['width'],$info['height']);
  if($info['type']=='gif' || $info['type']=='png')
  {
   $bgcolor=imagecolorallocate($thumb_im,0,255,0);
   imagecolortransparent($thumb_im,$bgcolor);
  }
  $imagefunc='image'.($info['type']=='jpg'?'jpeg':$info['type']);
  $thumbname='thumb_'.$info['name'].'.'.$info['type'];
  $imagefunc($thumb_im,$info['path'].$thumbname);
  imagedestroy($im);
  imagedestroy($thumb_im);
  return $info['path'].$thumbname;  
 }

 function watermark($image,$pos=9,$watermarkimg='images/watermark.gif',$pct=65,$text='',$w_font=5,$w_color='#ff0000')
 {
  $imageinfo=$this->info($image);
  $source_w=$imageinfo['width'];
  $source_h=$imageinfo['height'];
  $imagecreatefunc='imagecreatefrom'.($imageinfo['type']=='jpg'?'jpeg':$imageinfo['type']);
  $im=$imagecreatefunc($image);
  if(!empty($watermarkimg) && file_exists($watermarkimg)) //添加图片水印
  {
   $iswaterimage=true;
   $watermarkinfo=$this->info($watermarkimg);
   $width=$watermarkinfo['width'];
   $height=$watermarkinfo['height'];
   $watermarkcreatefunc='imagecreatefrom'.($watermarkinfo['type']=='jpg'?'jpeg':$watermarkinfo['type']);
   $watermark_im=$watermarkcreatefunc($watermarkimg);
  }
  else //添加文字水印
  {
   $iswaterimage=false;
   if(!empty($w_color) && strlen($w_color)==7)
   {
    $r=hexdec(substr($w_color,1,2));
    $g=hexdec(substr($w_color,3,2));
    $b=hexdec(substr($w_color,5,2));
   }
   $temp = imagettfbbox(ceil($w_font*2.5), 0, 'fonts/alger.ttf', $text);
   $width = $temp[2] - $temp[6];
   $height = $temp[3] - $temp[7];
   unset($temp);
  }
  switch($pos)
  {
   case 0:
    $wx = mt_rand(0,($source_w - $width));
    $wy = mt_rand(0,($source_h - $height));
    break;
   case 1:
    $wx = 5;
    $wy = 5;
    break;
   case 2:
    $wx = ($source_w - $width) / 2;
    $wy = 5;
    break;
   case 3:
    $wx = $source_w - $width-5;
    $wy = 5;
    break;
   case 4:
    $wx = 5;
    $wy = ($source_h - $height) / 2;
    break;
   case 5:
    $wx = ($source_w - $width) / 2;
    $wy = ($source_h - $height) / 2;
    break;
   case 6:
    $wx = $source_w - $width-5;
    $wy = ($source_h - $height) / 2;
    break;
   case 7:
    $wx = 5;
    $wy = $source_h - $height-5;
    break;
   case 8:
    $wx = ($source_w - $width) / 2;
    $wy = $source_h - $height-5;
    break;
   default:
    $wx = $source_w - $width-5;
    $wy = $source_h - $height-5;
    break;
  }
  if($iswaterimage)
  {
   if($imageinfo['type'] == 'png') {
    imagecopy($im, $watermark_im, $wx, $wy, 0, 0, $width, $height);
   } else {
    imagecopymerge($im, $watermark_im, $wx, $wy, 0, 0, $width, $height, $pct);
   }
  }
  else
  {
   imagestring($im,$w_font,$wx,$wy,$text,imagecolorallocate($im,$r,$g,$b));
  }
  $imagefunc='image'.($imageinfo['type']=='jpg'?'jpeg':$imageinfo['type']);
  $imagefunc($im,$image);
  imagedestroy($im);
  return true;
 }

 function info($image)
 {
  $info=array();
  $info['size']=filesize($image);
  $imageinfo=getimagesize($image);
  $info['width']=$imageinfo[0];
  $info['height']=$imageinfo[1];
  $info['width_height']=$imageinfo[3];
  $info['mime']=$imageinfo['mime'];
  unset($imageinfo);
  $imageinfo=pathinfo($image);
  $info['path']=$imageinfo['dirname'].'/';
  $info['type']=strtolower($imageinfo['extension']); //图片类型,不含'.'
  $info['name']=$imageinfo['filename'];
  unset($imageinfo,$name);
  $this->info=$info;
  return $info;
 }
}

 

时间: 2024-08-02 14:47:01

php图片处理类(生成缩略图,增加水印,获取图片信息)的相关文章

php图片处理类(生成缩略图,图片尺寸调整,图片截取,图片加水印,图片旋转 )

使用演示:  代码如下 复制代码 //加载类库文件 require_once 'path/to/ThumbLib.inc.php'; //实例化类库,传入你要处理的图片的地址可以是网络地址,也可以是本地地址 $thumb = PhpThumbFactory::create('http://www.111cn.net/'); //把图片等比缩小到最大宽度 100px或者最高100px,当只输入一个参数的时候,是限制最宽的尺寸. $thumb->resize(100, 100); //把图片等比缩小

支持png透明图片的php生成缩略图类分享

 这篇文章主要介绍了支持png透明图片的php生成缩略图类分享,本文代码基于GD2图形库,实现支持png透明图片生成缩略图,需要的朋友可以参考下     注:此功能依赖GD2图形库 最近要用php生成缩略图,在网上找了一下,发现了这篇文章:PHP生成图片缩略图 试用了一下后,发现有这样几个问题: 1.png图片生成的缩略图是jpg格式的 2.png图片生成的缩略图没有了透明(半透明)效果(填充了黑色背景) 3.代码语法比较老 因此,在这个版本的基础上简单修改优化了一下. PHP生成缩略图类 ?

Thinkphp调用Image类生成缩略图的方法

 这篇文章主要介绍了Thinkphp调用Image类生成缩略图的方法,实例分析了Thinkphp调用Image类生成缩略图的使用原理与相关技巧,需要的朋友可以参考下     本文实例讲述了Thinkphp调用Image类生成缩略图的方法.分享给大家供大家参考.具体分析如下: Thinkphp的Image类 在ThinkPHP/Extend/Library/ORG/Util/Image.class.php中. 调用方法如下: ? 1 2 3 4 5 6 7 import("ORG.Util.Ima

Thinkphp调用Image类生成缩略图的方法_php实例

本文实例讲述了Thinkphp调用Image类生成缩略图的方法.分享给大家供大家参考.具体分析如下: Thinkphp的Image类 在ThinkPHP/Extend/Library/ORG/Util/Image.class.php中. 调用方法如下: import("ORG.Util.Image"); $Img = new Image();//实例化图片类对象 $image_path = './图片路径'; //若当前php文件在Thinkphp的中APP_PATH路径中 //'./

按照比例改变图片大小(非生成缩略图)

  <? /**      按照比例改变图片大小(非生成缩略图)      @param string $img 图片路径      @param int $max_w 最大缩放宽      @param int $max_h 最大缩放高  */  function chImageSize ($img,$max_w,$max_h)  {      $size = @getimagesize($img);          $w = $size[0];          $h     =    $

android-安卓图片上传生成缩略图,裁剪后背景为黑色。

问题描述 安卓图片上传生成缩略图,裁剪后背景为黑色. 用户在上传图片后图片可能会自定义,不一定为最大,如果为黑色的话,会影响用户体验. 请问我要如何做成透明的. 解决方案 http://zhidao.baidu.com/link?url=vqPoNNcVjXD2Lsb57JMZSuJd3gH8rfpJpr9iItIPZ7_PQ__8-rSho89hBSKpKCj--1e9x5S3oQpcc2zHXk1H-QaXFhA4BtI-X6GJdJxGeOK 解决方案二: 裁剪保存的时候注意压缩质量,最好

Go语言图片处理和生成缩略图的方法_Golang

本文实例讲述了Go语言图片处理和生成缩略图的方法.分享给大家供大家参考.具体实现方法如下: 复制代码 代码如下: package main import (     "fmt"     "os"     "image"     "image/color"     "image/draw"     "image/jpeg" ) func main() {     f1, err := os

支持png透明图片的php生成缩略图类分享_php技巧

注:此功能依赖GD2图形库 最近要用php生成缩略图,在网上找了一下,发现了这篇文章:PHP生成图片缩略图 试用了一下后,发现有这样几个问题: 1.png图片生成的缩略图是jpg格式的 2.png图片生成的缩略图没有了透明(半透明)效果(填充了黑色背景) 3.代码语法比较老 因此,在这个版本的基础上简单修改优化了一下. PHP生成缩略图类 <?php /* * desc: Resize Image(png, jpg, gif) * author: 十年后的卢哥哥 * date: 2014.11.

C#图片压缩、生成缩略图、添加水印模块

  C#图片处理工具类,可以压缩JPG图像,图片自动生成缩略图,为图片添加水印效果等,返回高清缩略图,得到最佳的图片比例缩放尺寸,并获取图片类型等,类代码如下: view sourceprint?001using System; 002using System.Collections.Generic; 003using System.Linq; 004using System.Text; 005using System.IO; 006using System.Drawing; 007using