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

 更新时间:2016年11月25日 16:57  点击:1990

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

 代码如下 复制代码

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;
 }
}

 

在php中给图片增加水印有imagecreatefromjpeg imagecreatefrompng imagecopymerge imagejpeg就成了,只要你设置原图与水印图片就成了,下面看实例。
*/

 代码如下 复制代码
header("content-type: image/jpeg");
$filename='temp/www.111cn.net/111cn.net.jpg';
$im=imagecreatefromjpeg($filename);
$s=imagecreatefrompng('mb.111cn.net/pic/water_template.png');
imagecopymerge($im,$s,0,200,0,0,365,27,20);
imagejpeg($im);

本文章提供一款php教程生成验证码详细教程,前面是讲关于生成验证核心代码,后面是一款生成与调用的方法。

 代码如下 复制代码

<?php
//header("content-type:image/png");
$num ='1234';
$imagewidth=60;
$imageheight=18;

$numimage = imagecreate($imagewidth,$imageheight);
imagecolorallocate($numimage,240,240,240);
for($i=0;$i<strlen($num);$i++){
$x = mt_rand(1,8)+$imagewidth*$i/4;
$y = mt_rand(1,$imageheight/4);
$color=imagecolorallocate($numimage,mt_rand(0,150),mt_rand(0,150),mt_rand(0,150));
imagestring($numimage,5,$x,$y,$num[$i],$color);
}

for($i=0;$i<200;$i++){
$randcolor=imagecolorallocate($numimage,rand(200,255),rand(200,255),rand(200,255));
imagesetpixel($numimage,rand()%70,rand()%20,$randcolor);
}
imagepng($numimage);
imagedestroy($numimage);
?>


来看一款生成验证码实例

 代码如下 复制代码

<?php

   //生成验证码图片

    session_start();

        header("content-type: image/png"); 

  srand((double)microtime()*1000000); 

  $roundnum=rand(1000,9999);

  //把随机数存入session以便以后用

   $_session["sessionround"]=$roundnum;

        $im = imagecreate(58,28);

        $red = imagecolorallocate($im, 255,0,0);

        $blue = imagecolorallocate($im, 0,255,0);

 //局域填充,相当于背景

        imagefill($im,68,30,$red);

   //将四位整数验证码绘入图片

        imagestring($im, 5, 10, 8, $roundnum, $blue);

        for($i=0;$i<50;$i++)   //加入干扰象素

        {

                imagesetpixel($im, rand()%70 , rand()%30 , $black);

        }

        imagepng($im);

        imagedestroy($im);

?>


html调用 方法

 代码如下 复制代码

<td>验证码</td><td width="20%">

     <input name="validt" type="text" size="10" maxlength="4"/></td><td colspan="3" width="80%">

     <img src="/www.111cn.net/validitpicture.php" name="validitpic" align="absmiddle"  /></td>

 

本文章提供一款php教程中文汉字验证码生成程序,如果在图片片生成汉字,需要font文件和imagettftext函数,用到的时候大家再网上搜索吧。你要产生随机数,那有mt_rand函数;你还要用到session保存这个随机数;如果需要转成utf-8,需要iconv函数。

 <?php
class simpleimage {
var $image;
var $image_type;
function load($filename) {
$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
if( $this->image_type == imagetype_jpeg ) {
$this->image = imagecreatefromjpeg($filename);
} elseif( $this->image_type == imagetype_gif ) {
$this->image = imagecreatefromgif($filename);
} elseif( $this->image_type == imagetype_png ) {
$this->image = imagecreatefrompng($filename);
}
}
function save($filename, $image_type=imagetype_jpeg, $compression=75, $permissions=null) {
if( $image_type == imagetype_jpeg ) {
imagejpeg($this->image,$filename,$compression);
} elseif( $image_type == imagetype_gif ) {
imagegif($this->image,$filename);
} elseif( $image_type == imagetype_png ) {
imagepng($this->image,$filename);
}
if( $permissions != null) {
chmod($filename,$permissions);
}
}
function output($image_type=imagetype_jpeg) {
if( $image_type == imagetype_jpeg ) {
imagejpeg($this->image);
} elseif( $image_type == imagetype_gif ) {
imagegif($this->image);
} elseif( $image_type == imagetype_png ) {
imagepng($this->image);
}
}
function getwidth() {
return imagesx($this->image);
}
function getheight() {
return imagesy($this->image);
}
function resizetoheight($height) {
$ratio = $height / $this->getheight();
$width = $this->getwidth() * $ratio;
$this->resize($width,$height);
}
function resizetowidth($width) {
$ratio = $width / $this->getwidth();
$height = $this->getheight() * $ratio;
$this->resize($width,$height);
}
function scale($scale) {
$width = $this->getwidth() * $scale/100;
$height = $this->getheight() * $scale/100;
$this->resize($width,$height);
}
function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getwidth(), $this->getheight());
$this->image = $new_image;
}
}

$newfile = upload_dir."/icons/".md5($_session['user']->email).".jpg";//上传文件保存的目录
$image = new simpleimage();
$image->load($_files['icons']['tmp_name']);//上传的临时文件名
$image->resizetowidth(80);设置宽度
$image->save($newfile);
?>

如果你想利用php 给图片增加背景平铺水印效果话,必须利用php的一个插件来实例,就是利用imagick,他可以给图片增加背景平铺水印效果哦,下面我们提供一款实例代码。

imagemagic官方去除图片背景的命令行模式:

 代码如下 复制代码
convert -size 140x80 xc:none -fill grey           -gravity northwest -draw "text 10,10 'copyright'"           -gravity southeast -draw "text 5,15 'copyright'"           miff:- |    composite -tile - logo.jpg  wmark_text_tiled.jpg


imagick代码:

 代码如下 复制代码
<?php
$image = new imagick('logo.jpg');
$im = new imagick();
$im->newimage( 140, 80, new imagickpixel( "none" ) );
$draw = new imagickdraw();
$draw->setfillcolor(new imagickpixel( "grey" ));
$draw->setgravity(imagick::gravity_northwest);
$draw->annotation(10,10 ,'copyright');
$draw->setgravity(imagick::gravity_southeast);
$draw->annotation(5,15 ,'copyright');
$im->drawimage( $draw);
$image = $image->textureimage($im);
$image->compositeimage($image,imagick::composite_copy,0,0);
header( "content-type: image/{$image->getimageformat()}" );
$image->writeimage('wmark_text_tiled.jpg');
$image->clear();
$image->destroy();
?>

如果你的机型还没装php_imagick就下载吧,下载地址如下

http://pecl.php.net/package/imagick

[!--infotagslink--]

相关文章

  • c#生成高清缩略图的二个示例分享

    这篇文章主要介绍了c#生成高清缩略图的二个示例,需要的朋友可以参考下...2020-06-25
  • PHP批量生成图片缩略图(1/5)

    这款批量生成缩略图代码可以生成指定大小的小图哦,并且支持文件批量上传。 这款教程会用到php文件 view.php config.php funs.php index.php 功能: -------...2016-11-25
  • C#实现为一张大尺寸图片创建缩略图的方法

    这篇文章主要介绍了C#实现为一张大尺寸图片创建缩略图的方法,涉及C#创建缩略图的相关图片操作技巧,需要的朋友可以参考下...2020-06-25
  • php 上传文件并生成缩略图代码

    if( isset($_FILES['upImg']) ) { if( $userGroup[$loginArr['group']]['upload'] == 0 ) { echo '{"error":"您所在的用户组无权上传图片!"}'; } else...2016-11-25
  • CentOS下编译安装nginx及配置缩略图插件的方法教程

    这篇文章主要给大家介绍了在CentOS系统下编译安装nginx及配置缩略图插件的方法教程,文中给出了详细的安装步骤,对大家具有一定的参考价值,有需要的朋友们下面来一起看看吧。...2017-07-06
  • c#多图片上传并生成缩略图的实例代码

    今天写了一个上传多张图片并生成缩略图的小程序。当然因为是菜鸟,所以写的一般。但还是学到了不少东西。现在上代码。...2021-09-22
  • Nginx配合php实现生成实时缩略图功能

    这篇文章主要介绍了Nginx配合php实现生成实时缩略图功能,这在一些特殊场合可能会要用到,需要的朋友可以参考下...2016-01-27
  • js通过canvas生成图片缩略图

    对于生成缩略图一般做法是通过后端语言php等来生成,但是为了给服务器减压,我们或许可以从前端来着手,先生成好不同尺寸的缩略图,传给后端,而后端只需要将前端传过来的图片进行存储就好了...2020-10-03
  • php 图片上传代码(具有生成缩略图与增加水印功能)

    这款图片上传源代码是一款可以上传图片并且还具有给上传的图片生成缩略图与增加水印功能哦,可以说是一款完美的图片上传类哦。 代码如下 复制代码 ...2016-11-25
  • C# jpg缩略图函数代码

    生成jpg缩略图字节,本人的小软件中需要用到的功能,所以自己做了一个函数,和大家分享 为什么要生成字节而不是文件,这是为了方便后续处理啦^_^...2020-06-25
  • php支持生成缩略图文件上传代码

    <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1...2016-11-25
  • C#获取视频某一帧的缩略图的方法

    这篇文章主要介绍了C#获取视频某一帧的缩略图的方法,涉及执行CMD命令及针对视频文件操作的技巧,具有一定的实用价值,需要的朋友可以参考下...2020-06-25
  • python批量提取图片信息并保存的实现

    这篇文章主要介绍了python批量提取图片信息并保存的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-06
  • php利用Imagick把pdf生成png缩略图

    缩略图如果是图片我们直接使用php gD库就可实现了,本文章要介绍的是Imagick把pdf生成png缩略图方法,这里我们要利用一个插件了,下面我来给大家演示一个实例。 php_im...2016-11-25
  • ASP.NET创建动态缩略图的方法

    这篇文章主要介绍了ASP.NET创建动态缩略图的方法,实例分析了asp.net动态操作图片的相关技巧,需要的朋友可以参考下...2021-09-22
  • php图片上传类同时可生成缩略图与加水印

    这款图片上传代码可以把上传的图片增加水印,生成小图片,同时还可以生成文字水印。 代码如下 复制代码 class upimages { var $annexf...2016-11-25
  • php文件上传程序(支持缩略图)(1/2)

    代码如下 复制代码 <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional....2016-11-25
  • c#生成缩略图不失真的方法实例分享

    使用.net的方法GetThumbnailImage生成的缩略图失真严重,这里推荐一种不失真生成缩略图的方法...2021-09-22
  • 等比例生成缩略图的php程序

    等比例生成缩略图的php程序 这个程序很实现,但只是用来进来等比例生成缩略图哦,你要把文件上传到服务器,然后再由此函数来操作,有需要的朋友参考一下。 等比例生成缩...2016-11-25
  • php遍历读取文件夹/目录图片信息

    今天帮助一个客户做一上企业网站,发现企业网站做好之后它准备了几百张图片让我上传,这个对于我来讲非常的不想做了,但后来发现可以直接使用程序读取目录然后保存到mysql...2016-11-25