php 加水印、缩略图的实现代码(1/2)

 更新时间:2016年11月25日 16:57  点击:1456
在上传图片为了推扩网站我们可能会在图片上增加水印,或对图片进行缩小,下面提供的代码就有这个功能。
 代码如下 复制代码
<?php教程
/************************************
//函数: watermark($bigimg, $smallimg, $coord = 1)
//作用: 添加水印
//参数:
$bigimg 必选。大图片--要加上水印的图片
$smallimg 必选。小图片
$coord 可选。水印在大图中的位置,
1 左上角; 2 右上角; 3 右下角; 4 左下角; 5 中间
//示例: watermark('datu.png', 'xiaotu.png', 3); //给datu.png打上水印,水印位置在右下角
*************************************/
function watermark($bigimg, $smallimg, $coord = 1){
//载入两张图片,并转成php识别的编码格式,
//等同于 imagecreate 函数,只不过这里创建的不是一个空图片。
$bi = getimagesize($bigimg);
switch($bi[2]){
case 1:
$im1 = imagecreatefromgif($bigimg);break;
case 2;
$im1 = imagecreatefromjpeg($bigimg);break;
case 3;
$im1 = imagecreatefrompng($bigimg);break;
}
$si = getimagesize($smallimg);
switch($si[2]){
case 1:
$im2 = imagecreatefromgif($smallimg);break;
case 2;
$im2 = imagecreatefromjpeg($smallimg);break;
case 3;
$im2 = imagecreatefrompng($smallimg);break;
}
// 创建水印--原理:复制小图到大图上。这里注意坐标值的计算
switch($coord){
case 1:
imagecopy ( $im1, $im2, 0, 0, 0, 0, $si[0], $si[1] ); break;
case 2:
imagecopy ( $im1, $im2, $bi[0]-$si[0], 0, 0, 0, $si[0], $si[1] ); break;
case 3:
imagecopy ( $im1, $im2, $bi[0]-$si[0], $bi[1]-$si[1], 0, 0, $si[0], $si[1] ); break;
case 4:
imagecopy ( $im1, $im2, 0, $bi[1]-$si[1], 0, 0, $si[0], $si[1] ); break;
case 5:
imagecopy ( $im1, $im2, ($bi[0]-$si[0])/2, ($bi[1]-$si[1])/2, 0, 0, $si[0], $si[1] ); break;
}
//根据后缀名生成不同格式的图片文件
switch($bi[2]){
case 1:
imagegif($im1);break;
case 2;
imagejpeg($im1);break;
case 3;
imagepng($im1);break;
}
imagedestroy($im1);
}
/************************************************
//函数: thumbnail($srcimg, $multiple)
//作用: 生成一张缩略图
//参数:
// $srcimg 必选。源图片文件名
// $multiple 可选。缩略倍数,默认为2倍,即缩小为原来的1/2
//注意: 只支持gif、jpg、png的格式图片。
//示例: thumbnail('我的图片.jpg', 5);
*************************************************/
function thumbnail($srcimg, $multiple = 2){
//载入图片并保存其信息到数组
$srcimg_arr = getimagesize($srcimg);
//计算缩略倍数
$thumb_width = $srcimg_arr[0] / $multiple;
$thumb_height = $srcimg_arr[1] / $multiple;
//判断:要建立什么格式的图片(转成php识别的编码)
switch($srcimg_arr[2]){
case 1:
$im = imagecreatefromgif($srcimg);break;
case 2;
$im = imagecreatefromjpeg($srcimg);break;
case 3;
$im = imagecreatefrompng($srcimg);break;
}

 

int exif_imagetype ( string $filename )
1 imagetype_gif
2 imagetype_jpeg
3 imagetype_png
4 imagetype_swf
5 imagetype_ps教程d
6 imagetype_bmp
7 imagetype_tiff_ii (intel byte order)
8 imagetype_tiff_mm (motorola byte order) 
9 imagetype_jpc
10 imagetype_jp2
11 imagetype_jpx
12 imagetype_jb2
13 imagetype_swc
14 imagetype_iff
15 imagetype_wbmp
16 imagetype_xbm

*/
$img="image.gif";          //定义文件
if(exif_imagetype($img)!=imagetype_gif)     //判断文件类型
{
  echo "指定的图片不是gif图片";       //输出结果
}
else
{
  echo "指定的图片是gif图片";
}

/*
array exif_read_data ( string $filename [, string $sections = null [, bool $arrays = false [, bool $thumbnail = false ]]] )

*/

echo "test1.jpg:<br/>n";         //输出文件名
$exif=exif_read_data('tests/test1.jpg','ifd0');     //读取文件1信息
             //根据结果判断
echo $exif===false ? "no header data found.<br/>n":"image contains headers<br/>n";
$exif=exif_read_data('tests/test2.jpg',0,true);     //读取文件2信息
echo "test2.jpg:<br/>n";         //输出文件名
foreach($exif as $key=>$section)       //循环读取信息
{
  foreach($section as $name=>$val)
  {
    echo "$key.$name:$val<br/>n";
  }
}
/*
test1.jpg:
no header data found.
test2.jpg:
file.filename: test2.jpg
file.filedatetime: 1017666176
file.filesize: 1240
file.filetype: 2
file.sectionsfound: any_tag, ifd0, thumbnail, comment
computed.html: width="1" height="1"
computed.height: 1
computed.width: 1
computed.iscolor: 1
computed.byteordermotorola: 1
computed.usercomment: exif test image.
computed.usercommentencoding: ascii
computed.copyright: photo (c) m.boerger, edited by m.boerger.
computed.copyright.photographer: photo (c) m.boerger
computed.copyright.editor: edited by m.boerger.
ifd0.copyright: photo (c) m.boerger
ifd0.usercomment: ascii
thumbnail.jpeginterchangeformat: 134
thumbnail.jpeginterchangeformatlength: 523
comment.0: comment #1.
comment.1: comment #2.
comment.2: comment #3end
thumbnail.jpeginterchangeformat: 134
thumbnail.thumbnail.height: 1
thumbnail.thumbnail.height: 1


//*/

$index="1";          //定义索引
$string=exif_tagname($index);       //获得图像索引的头信息
echo $string;          //输出结果

//

if(array_key_exists('file',$_request))
{
  $image=exif_thumbnail($_request['file'],$width,$height,$type);  //取得文件的缩略图
}
else              //如果数组不存在相应的键值
{
  $image=false;           //返回错误
}
if($image!==false)           //如果返回true
{
  header("content-type:".image_type_to_mime_type($type));   //发送头文件
  echo $image;           //输出缩略图
  exit;             //结束php教程代码
}
else              //如果返回错误
{
  echo "no thumbnail available";        //输出信息
}

这里有两款验证码生成程序,第一款比较好,第二款也不错,同时第二款生成程序用一个完完的实例来说明验证码调用与生成

session_start();
class authnum {
 //图片对象、宽度、高度、验证码长度
 private $im;
 private $im_width;
 private $im_height;
 private $len;
 //随机字符串、y轴坐标值、随机颜色
 private $randnum;
 private $y;
 private $randcolor;
 //背景色的红绿蓝,默认是浅灰色
 public $red=238;
 public $green=238;
 public $blue=238;
 /**
  * 可选设置:验证码类型、干扰点、干扰线、y轴随机
  * 设为 false 表示不启用
 **/
 //默认是大小写数字混合型,1 2 3 分别表示 小写、大写、数字型
 public $ext_num_type='';
 public $ext_pixel = false; //干扰点
 public $ext_line  = false; //干扰线
 public $ext_rand_y= true;  //y轴随机
 function __construct ($len=4,$im_width='',$im_height=25) {
  // 验证码长度、图片宽度、高度是实例化类时必需的数据
  $this->len      = $len; $im_width = $len * 15;
  $this->im_width = $im_width;
  $this->im_height= $im_height;
  $this->im = imagecreate($im_width,$im_height);
 }
 // 设置图片背景颜色,默认是浅灰色背景
 function set_bgcolor () {
  imagecolorallocate($this->im,$this->red,$this->green,$this->blue);
 }
 // 获得任意位数的随机码
 function get_randnum () {
  $an1 = 'abcdefghijklmnopqrstuvwxyz';
  $an2 = 'abcdefghijklmnopqrstuvwxyz';
  $an3 = '0123456789';
  if ($this->ext_num_type == '')  $str = $an1.$an2.$an3;
  if ($this->ext_num_type == 1) $str = $an1;
  if ($this->ext_num_type == 2) $str = $an2;
  if ($this->ext_num_type == 3) $str = $an3;
  for ($i = 0; $i < $this->len; $i++) {
   $start = rand(1,strlen($str) - 1);
   $randnum .= substr($str,$start,1);
  }
  $this->randnum = $randnum;
  $_session[an] = $this->randnum;
 }
 // 获得验证码图片y轴
 function get_y () {
  if ($this->ext_rand_y) $this->y = rand(5, $this->im_height/5);
  else $this->y = $this->im_height / 4 ;
 }
 // 获得随机色
 function get_randcolor () {
  $this->randcolor = imagecolorallocate($this->im,rand(0,100),rand(0,150),rand(0,200));
 }
 // 添加干扰点
 function set_ext_pixel () {
  if ($this->ext_pixel) {
   for($i = 0; $i < 100; $i++){
       $this->get_randcolor();
       imagesetpixel($this->im, rand()%100, rand()%100, $this->randcolor);
   }
  }
 }
 // 添加干扰线
 function set_ext_line () {
  if ($this->ext_line) {
   for($j = 0; $j < 2; $j++){
    $rand_x = rand(2, $this->im_width);
    $rand_y = rand(2, $this->im_height);
    $rand_x2 = rand(2, $this->im_width);
    $rand_y2 = rand(2, $this->im_height);
    $this->get_randcolor();
    imageline($this->im, $rand_x, $rand_y, $rand_x2, $rand_y2, $this->randcolor);
   }
  }
 }
 /**创建验证码图像:
  * 建立画布(__construct函数)
  * 设置画布背景($this->set_bgcolor();)
  * 获取随机字符串($this->get_randnum ();)
  * 文字写到图片上(imagestring函数)
  * 添加干扰点/线($this->set_ext_line(); $this->set_ext_pixel();)
  * 输出图片
 **/
 function create () {
  $this->set_bgcolor();
  $this->get_randnum ();
  for($i = 0; $i < $this->len; $i++){
   $font = rand(4,6);
      $x    = $i/$this->len * $this->im_width + rand(1, $this->len);
   $this->get_y();
   $this->get_randcolor();
      imagestring($this->im, $font, $x, $this->y, substr($this->randnum, $i ,1), $this->randcolor);
  }
      $this->set_ext_line();
      $this->set_ext_pixel();
   header("content-type:image/png");
   imagepng($this->im);
   imagedestroy($this->im);     //释放图像资源
 }

}//end class
/**使用验证码类的方法:
 * $an = new authnum(验证码长度,图片宽度,图片高度);
 * 实例化时不带参数则默认是四位的60*25尺寸的常规验证码图片
 * 表单页面检测验证码的方法,对比 $_session[an] 是否等于 $_post[验证码文本框id]
 * 可选配置:
 *  1.验证码类型:$an->ext_num_type=1;  值为1是小写类型,2是大写类型,3是数字类型
 *  2.干扰点:$an->ext_pixel = false;   值为false表示不添加干扰点
 *  3.干扰线:$an->ext_line = false;    值为false表示不添加干扰线
 *  4.y轴随机:$an->ext_rand_y = false; 值为false表示不支持图片y轴随机
 *  5.图片背景:改变 $red $green $blue 三个成员变量的值即可
**/
$an = new authnum();
$an->ext_num_type='';
$an->ext_pixel = true; //干扰点
$an->ext_line  = false; //干扰线
$an->ext_rand_y= true; //y轴随机
$an->green = 238;
$an->create();
?>

好下面来看一款验证码调用实例

例子demo:

以下为引用的内容:
<!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/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>hi.baidu.com/ji_haiyang</title>
</head>

<body>
<form action ="demo1.php教程" method="post" name="form1">
<input name ="test" type="text" value="1234" />
<div onclick='this.innerhtml="<img src=vcode.php?r="+math.random()+"></img>"'>
<img src="vcode.php"></img>
</div>
<input name ="test2" type="text" value="1" />
<input name ="submit" type="submit" value="submit" />
</form>

</body>
</html>


 


例子demo1:

<?php

session_start();
$test = $_post['test'];
$test = trim($test);
$submit = $_post['submit'];
if($test==$_session['vcode']){
echo 'true,输入验证码正确';
} else {
echo 'false,输入验证码错误';
}

?>
 

调用文件vcode.php: 以下为引用的内容:
<?php
/**
* 默认验证码session为vcode.即:$_session['vcode'];
* 注意在给变量符值时不要把变量的名子和session冲突
* 注:在验证时不分大小写
*/
include("inc_vcode_class.php");
$v = new vcode;
//$config['width'] = 50;   //验证码宽
//$config['height'] = 20;   //验证码高
//$config['vcode'] = "vcode"; //检查验证码时用的session
//$config['type'] = "default"; //验证码展示的类型default:大写字母,string:小写字母,int:数字 php程序员站
//$config['length'] = 4;   //验证码长度
//$config['interfere']= 10;   //干扰线强度,范围为1-30,0或空为不起用干扰线
//$v->init($config); //配置
$v->create();
?>

验证码类inc_vcode_class.php: www~phperz~com 以下为引用的内容:
<?php
/**
* 验证码类
* 注:需要gd库支持
*/
session_start();
class vcode{
private $config;
private $im;
private $str;

function __construct(){
$this->config['width'] = 50;
$this->config['height'] = 20;
$this->config['vcode'] = "vcode";
$this->config['type'] = "default";
$this->config['length'] = 4;
$this->config['interfere'] = 10;

$this->str['default'] = "abcdefghijklmnopqrstuvwxyz";
$this->str['string'] = "abcdefghijklmnopqrstuvwxyz";
$this->str['int']  = "0123456789";
}

//配置
public function init($config=array()){
if (!empty($config) && is_array($config)){
foreach($config as $key=>$value){
$this->config[$key] = $value;
}
}
}

//输出验证码
public function create(){
if (!function_exists("imagecreate")){
return false;
}
$this->create_do();
}

//创建
private function create_do(){
$this->im = imagecreate($this->config['width'],$this->config['height']); php程序员之家
//设置背景为白色
imagecolorallocate($this->im, 255, 255, 255);

//为此背景加个边框
$bordercolor= imagecolorallocate($this->im,37,37,37);
imagerectangle($this->im,0,0,$this->config['width']-1,$this->config['height']-1,$bordercolor);

//生成验证码
$this->create_str();
$vcode = $_session[$this->config['vcode']];

//输入文字
$fontcolor = imagecolorallocate($this->im,46,46,46);
for($i=0;$i<$this->config['length'];$i++){
imagestring($this->im,5,$i*10+6,rand(2,5),$vcode[$i],$fontcolor);
}

//加入干扰线
$interfere = $this->config['interfere'];
$interfere = $interfere>30?"30":$interfere; php程序员站
if (!empty($interfere) && $interfere>1){
for($i=1;$i<$interfere;$i++){
$linecolor = imagecolorallocate($this->im,rand(0,255),rand(0,255),rand(0,255));
$x = rand(1,$this->config['width']);
$y = rand(1,$this->config['height']);
$x2 = rand($x-10,$x+10);
$y2 = rand($y-10,$y+10);
imageline($this->im,$x,$y,$x2,$y2,$linecolor);
}
}

header("pragma:no-cachern");
header("cache-control:no-cachern");
header("expires:0rn");
header("content-type:image/jpegrn");
imagejpeg($this->im);
imagedestroy($this->im);
exit;
}

//得到验证码
private function create_str(){
if ($this->config['type']=="int"){
for($i=1;$i<=$this->config['length'];$i++){
$vcode .= rand(0,9);
}
$_session[$this->config['vcode']] = $vcode;
return true;
}
$len = strlen($this->str[$this->config['type']]);
if (!$len){
$this->config['type'] = "default";
$this->create_str();
}
for($i=1;$i<=$this->config['length'];$i++){
$offset  = rand(0,$len-1);
$vcode .= substr($this->str[$this->config['type']],$offset,1);
}
$_session[$this->config['vcode']] = $vcode;
return true;
}
}

此程序实现php画柱状图它的设计思想是:首先以用imagecreate()来生成一个空白图形;其程序实现如下: //需要两种不同的颜色,正数一个 负数一个 ,一个数据数组,$path为保存图片的地址

<?php教程
//此程序实现php画柱状图,开发股票网站的朋友经常会碰到在生成k线图片,现在我们来看看一款利用php根据数组数据生成k线图片。
//它的设计思想是:首先以用imagecreate()来生成一个空白图形;其程序实现如下:
//需要两种不同的颜色,正数一个 负数一个 ,一个数据数组,$path为保存图片的地址

$data = array ("-1174.58","-1865.41","1961.24","-1174.58","-1865.41","1961.24","-1174.58","-1865.41","1961.24","-1174.58","-1865.41","1961.24","-1174.58","-1865.41","1961.24","-1174.58","-1865.41","1961.24","-1174.58","-1865.41","1961.24","-1174.58","-1865.41","1961.24","-1174.58","-1865.41","1961.24","-1174.58","-1865.41","1961.24");
$colors = array (array (255,0,0 ),array (0,255,0 ) );
$path = "gg15_600050.gif";
$title = "中国联通(600050)30日资金进出";
$date = array ("2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00");
$hai = array(7203,6037,1931,9605,7405,8855,228,7771,8671,8791,1290,426,3265,4298,4809,3350,8204,8682,5523,7569,5853,593,3124,9062,3329,3236,2252,8756,2796,5164,2599,2630,1470,9779,8805,1512,1517,5635,760,1380,152,6035,2905,3163,9443,9432,5031,2838,6802,9330,1878,1333,4297,4180,3037,4527,9043,7287,3248,3448,3405,7204,2203,3974,4399,7333,4541,7033,4292,6128,9955,8603,297,6992,3282,9612,4483,8379,4016,6038,4163,3789,6896,8710,1502,1579,1712,3705,3619,5150,3007,1853,2776,3056,569,9031,6092,8600,8941,6366,5829,433,8011,9637,9106,1761,2422,9873,8186,1136,1763,3050,285,1962,775,9900,7371,2691,9051,6596,6858,6726,1527,3970,910,2621,7598,7899,6546,3678,2202,9411,6129,8352,4830,4484,6776,243,323,3946,751,7508,8173,3776,5071,3847,3318,4901,2102,7516,5251,3299,8859,2114,8711,9800,7707,5898,3489,580,5763,2470,4126,6269,5191,182,412,3043,3208,2438,6889,1732,1979,7692,3445,8214,8620,9446,4624,3615,5496,6152,213,3715,9623,1215,4757,9443,3366,2988,8043,6874,3742,4974,4812,6839,3016,5625,4656,5315,8303,8346,2315,3230,1969,1688,7381,6463,1030,8856,4691,6042,7652,8502,3839,7844,9149,4029,9465,2897,7688,3791,1458,6981,484,1290,4261,1222,9503,4881,7663,1399,2260,8066,6405,8442,722,9297,177,8426,2103,2078,3521,2280,8570,4663,6227,5784,7438,654,3036,9965,872,8306,3537,899,5888,1319,2472,9218,4853,7849,9433,1196,4880,1597,9258,7465,8138,817,1637,4201,1939,8914,1114,2232,8251,4538,7378,7438,4489,993,5269,944,2718,6633,4378,8431,155,5061,9347,1125,8865,4629,9965,4706,6263,8742,971,301);
draw(600,300,$data,$colors,$path,$title,$date);
function draw($w,$h,$data,$colors,$path,$title,$date)
{
 //创建新空白图形
 //箭头50像素,内容400*300
 $width = $w;
 $height = $h;
 //取得左下角的坐标点,左边50像素和最下边50像素用于写标尺
 $l_b_x = 50;
 $l_b_y = $height/2;
 
 $chartfont = 2; //图表字体
 $font = 'c:/windows/fonts/simsun.ttc';//支持汉字的字体
 $chartfontheight = imagefontheight($chartfont);//图表字体的大小
 
 //取得最大的数据
 $max = abs(max($data));
 $max = $max>abs(min($data))?$max:abs(min($data));
 $image = imagecreate($width,$height);
 
 //填充背景为白色
 $color_white = imagecolorallocate($image, 0xff, 0xff, 0xff);//白色
 $color_diwen = imagecolorallocate($image,0xd8,0xd8,0xd8);//暗灰色
 imagefill($image,0,0,$color_white);
 //写入logo
 imagettftext($image,20,0,$l_b_x+20,45,$color_diwen,$font,'华南财经网');
 //画坐标
 //坐标颜色为黑色,先画坐标,再画箭头
 $black_color = imagecolorallocate($image,0x00,0x00,0x00);
 imageline($image,$l_b_x,$l_b_y,$l_b_x,10,$black_color);
 imageline($image,$l_b_x,$l_b_y,$width-10,$l_b_y,$black_color);
 //箭头
 imageline($image,$l_b_x,10,$l_b_x-3,14,$black_color);
 imageline($image,$l_b_x,10,$l_b_x+3,14,$black_color);
 imageline($image,$width-10,$l_b_y,$width-13,$l_b_y-3,$black_color);
 imageline($image,$width-10,$l_b_y,$width-13,$l_b_y+3,$black_color);
 //画底纹,灰色;画20条底纹;虚线
 $pit = floor(($l_b_y-50)/10);//每一段的高度
 $pit_x = floor(($width-$l_b_x-30)/count($data));//每一段的宽度
 //计算单位
 $x_max = $max;
 $flag_danwei = 0;
 while($x_max> 1000 )
 {
  $x_max /= 100;
  $flag_danwei++;
 }
 switch($flag_danwei)
 {
 // case 0:$danwei = '(元)';break;
  case 0:$danwei = '(万)';break;
  case 1:$danwei = '(百万)';break;
  case 2:$danwei = '(亿)';break;
  case 3:$danwei = '(百亿)';break;
 }
 for($i=1;$i<11;$i++)
 {
  imageline($image,$l_b_x+1,$l_b_y-$i*$pit,$width-15,$l_b_y-$i*$pit,$color_diwen);
  //从坐标的值
  $v_v = number_format(($max/(10*pow(100,$flag_danwei))*$i),2);
  $v_v = substr($v_v,0,5);
  imagestring($image,
     $chartfont,
     $l_b_x-40,
     $l_b_y-$i*$pit-10,
     $v_v,
     $black_color);
 }
 //原点坐标
 imagestring($image,
    $chartfont,
    $l_b_x-20,
    $l_b_y-10,
    0,
    $black_color);
 //写入汉字,单位 和标题
 imagettftext($image, 10,0,$l_b_x-45,20,$black_color,$font,$danwei);
 //imagettftext($image, 11,0,$width/2-70,$l_b_y+20,$black_color, $font,$title);

 //画柱体,长方形,并着色
 $color_big = imagecolorallocate($image,$colors[0][0],$colors[0][1],$colors[0][2]);
 $color_small = imagecolorallocate($image,$colors[1][0],$colors[1][1],$colors[1][2]);
 for($j=0;$j<count($data);$j++)
 {
  $d_h = floor(abs($data[$j]/$max*10*$pit));//柱体高度
  $current_date = substr($date[$j],5,5);
 // $current_date .= substr($date[$j],8,2);
  imagerectangle($image,
     $l_b_x+$j*$pit_x+1,
     $l_b_y,
     $l_b_x+($j+1)*$pit_x-2,
     $l_b_y-$d_h,
     $black_color);
  //为矩形着色
  $current_color = $data[$j]>0?$color_big:$color_small;
  imagefilltoborder($image,
    $l_b_x+$j*$pit_x+1 + floor($pit_x/2),
    $l_b_y-floor($d_h/2),
    $black_color,
    $current_color);
  //写入日期
  /*imagestring($image,
    $chartfont,
    $l_b_x+$j*$pit_x,
    $l_b_y+2,
    $current_date,
    $black_color);*/
  imagettftext($image,8,90,$l_b_x+$j*$pit_x+10,$l_b_y-$d_h,$black_color,$font,$current_date);
 } 
 if($path !="")
  imagegif($image,$path);
 else imagegif($image);
 imagedestroy($image);
 }//画图函数结束
//输出生成的图片
/*

header("content-type: image/gif");
$my_colors[] = array(0xff,0x00,0x00);
$my_colors[] = array(0x00,0xff,0x00);
draw(600,400,array(1066565650,-40345340,50343433440,1834340,50343230,-8023454230,600,7232323300,-836),$my_colors,0,'教科书大阿斯顿即可');*/
//draw(600,400,array(1066,-4040,5040,1840,5030,-8230,600,7232,-836),$my_colors,0,'教科书大阿斯顿即可');
?>

本站原创教程,转载注明来源于http://www.111cn.net/phper/php.html 否则必究

$ico_pic 是你要给图片加水印的水印图片,其它的参数都有详细的说明,如果你下大找这类代码可以下载保存成php文件再利用后面说的调用方法来调用本生成水印图片类代码。

 class smallpic{

  private $src_pic;//原图
  private $ico_pic = "003.png";//水印图
  private $ico_text = "水印";//水印文字
  private $small_width;//缩略图宽度
  private $small_height;//缩略图高度
  private $is_ico_pic = true;//是否加图片水印
  private $is_text = true;//是否加文字水印
  private $src_x = 20;//水印在原图的x坐标
  private $src_y = 20;//水印在原图的y坐标
  private $ut = "utf-8";//文字编码
  private $font_color = "#990000";//文字水印颜色
  private $samll_pic_name = "smallpic";//小图的名称
  private $big_pic_name = "bigpic";//大图的名称


  function __construct($src_pic,$small_width,$small_height){
   $this->checkfile($src_pic);
   $this->checkfile($this->ico_pic);
  $this->src_pic = $src_pic;
  $this->small_width = $small_width;
  $this->small_height = $small_height;
  }

 private function __get($property_name){
  return $this->$property_name;
 }

 private function __set($property_name,$value){
  return $this->$property_name = $value;
 }


 /**
  * 取得图片的一些基本信息,类型为array
  */
  function getimageinfo($image){
  return @getimagesize($image);
  }

 /**
  * 把图片加载到php中
  * $image 传进来的图片
  */
  function getimage($image){
  $image_info = $this->getimageinfo($image);
  switch($image_info[2]){
   case 1:
    $img = @imagecreatefromgif($image);
    break;
   case 2:
    $img = @imagecreatefromjpeg($image);
    break;
   case 3:
    $img = @imagecreatefrompng($image);
    break;
  }
  return $img;
  }

 function createimageforsuffix($big_pic,$new_pic){
  $image_info = $this->getimageinfo($this->src_pic);
  switch($image_info[2]){
   case 1:
    //输出大图
    @imagegif($big_pic,$this->big_pic_name.".gif");
    //输出小图
    @imagegif($new_pic,$this->samll_pic_name.".gif");
    break;
   case 2:
    //输出大图
    @imagejpeg($big_pic,$this->big_pic_name.".jpg");
    //输出小图
    @imagejpeg($new_pic,$this->samll_pic_name.".jpg");
    break;
   case 3:
    //输出大图
    @imagepng($big_pic,$this->big_pic_name.".png");
    //输出小图
    @imagepng($new_pic,$this->samll_pic_name.".png");
    break;
  }
 }

 function checkfile($file){
  if(!file_exists($file)){
   die("图片:".$file."不存在!");
  }
 }

 function createsmallimage(){
  $big_pic = $this->getimage($this->src_pic);
  $big_pic_info = $this->getimageinfo($this->src_pic);
  $new_pic = $this->getimage($this->ico_pic);
  $new_pic_info = $this->getimageinfo($this->ico_pic);
  $rgb = $this->convcolor();

  //判断是按宽比例缩放还是按高比例缩放
  if($big_pic_info[0] > $big_pic_info[1]){
   $ratio = $this->small_width/(int)$big_pic_info[0];
   $small_pic_width = $this->small_width;
   $small_pic_height = (int)($big_pic_info[1]*$ratio);
  }else{
   $ratio = $this->small_height/(int)$big_pic_info[1];
   $small_pic_height = $this->small_height;
   $small_pic_width = (int)($big_pic_info[0]*$ratio);
  }

  //echo $small_pic_width = (int)($big_pic_info[0]*$ratio);
  //echo $small_pic_height = (int)($big_pic_info[1]*$ratio);

  //是否打图片水印
  if ($this->is_ico_pic){
   //打图片水印
   @imagecopy($big_pic,$new_pic,$this->src_x,$this->src_y,0,0,$new_pic_info[0],$new_pic_info[1]);
  }
  //是否打文字水印
  if ($this->is_text){
   //设置文字颜色
   $text_color = @imagecolorallocate($big_pic,$rgb[0],$rgb[1],$rgb[2]);
   //转换文字编码
   $text = @iconv($this->ut,"utf-8",$this->ico_text);
   //打文字水印
   @imagettftext($big_pic,12,0,$this->src_x,$this->src_y,$text_color,"simkai_0.ttf",$text);
  }
  //新建一个新图片的画板
  $new_pic = @imagecreatetruecolor($small_pic_width,$small_pic_height);
  //生成缩略图
  @imagecopyresized($new_pic,$big_pic,0,0,0,0,$small_pic_width,$small_pic_height,$big_pic_info[0],$big_pic_info[1]);
  //输出图
  $this->createimageforsuffix($big_pic,$new_pic);
 }

 /**
  * 类内部的功能函数把#000000转换成255,255,255
  */
 private function convcolor(){
  $rgb = array();
  $color = preg_replace("/#/","",$this->font_color);
  $c = hexdec($color);
  $r = ($c >> 16) & 0xff;
  $g = ($c >> 8) & 0xff;
  $b = $c & 0xff;
  $rgb[0] = $r;
  $rgb[1] = $g;
  $rgb[2] = $b;
  return $rgb;
 }
 }

//调用方法
 

$pic = new smallpic("002.jpg",600,300);
 $pic->is_text = true;
 $pic->is_ico_pic = true;
 $pic->ico_pic = "./images/004.png";
 $pic->ico_text = "新年快乐!";
 //$pic->src_x = 80;
 $pic->src_y = 80;
 $pic->ut = "utf-8";
 $pic->font_color = "#0521f8";
 $pic->samll_pic_name = "hslsamll";
 $pic->big_pic_name = "hslbig";
 $pic->createsmallimage();

?>

[!--infotagslink--]

相关文章

  • 短视频(douyin)去水印工具的实现代码

    这篇文章主要介绍了市面上短视频(douyin)"去水印"的工具原来是这样实现的,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-03-30
  • c#生成高清缩略图的二个示例分享

    这篇文章主要介绍了c#生成高清缩略图的二个示例,需要的朋友可以参考下...2020-06-25
  • php图片添加文字水印实现代码

    这篇文章主要为大家详细介绍了php图片添加文字水印实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2016-03-17
  • PHP批量生成图片缩略图(1/5)

    这款批量生成缩略图代码可以生成指定大小的小图哦,并且支持文件批量上传。 这款教程会用到php文件 view.php config.php funs.php index.php 功能: -------...2016-11-25
  • 红米K30至尊纪念版相机拍照怎么去水印?

    红米K30至尊纪念版相机拍照怎么去水印?红米K30至尊纪念版拍照的时候总是自动添加水印,想要关闭水印,该怎么操作呢?下面我们就来看看详细的教程,需要的朋友可以参考下...2020-12-08
  • C#实现为一张大尺寸图片创建缩略图的方法

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

    if( isset($_FILES['upImg']) ) { if( $userGroup[$loginArr['group']]['upload'] == 0 ) { echo '{"error":"您所在的用户组无权上传图片!"}'; } else...2016-11-25
  • python实现图片加文字水印OPenCV和PIL库

    本文来为大家介绍一下,使用python中的库实现给图片添加文字水印,openCV可以给图片添加水印,如果要添加汉字水印那就要使用PIL库...2021-09-26
  • 如何利用Python和OpenCV对图像进行加水印详解

    Python使用opencv是因为觉得它足够强大,很多图像处理这块都是用的它,最近就用opencv添加个水印,这篇文章主要给大家介绍了关于如何利用Python和OpenCV对图像进行加水印的相关资料,需要的朋友可以参考下...2021-10-21
  • ASP.NET实现图片自动添加水印

    这篇文章主要介绍了ASP.NET实现图片自动添加水印,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-22
  • vue实现上传图片添加水印

    这篇文章主要为大家详细介绍了vue实现上传图片添加水印,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-13
  • CentOS下编译安装nginx及配置缩略图插件的方法教程

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

    用PHP给上传图片加水印的程序是通过判定文件类型建立图形,然后把其复制到原建立的图形上,填充并建立rectangle,以备写入imagestring()或是原已经定好的图像程序当中判定水...2016-11-25
  • c#多图片上传并生成缩略图的实例代码

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

    这篇文章主要为大家详细介绍了C#给图片添加水印的实现代码,不仅可以为图片加文字水印,还可以判断是否是图片文件,感兴趣的小伙伴们可以参考一下...2020-06-25
  • C#监控文件夹并自动给图片文件打水印的方法

    这篇文章主要介绍了C#监控文件夹并自动给图片文件打水印的方法,涉及C#针对文件夹及图片操作的相关技巧,非常具有实用价值,需要的朋友可以参考下...2020-06-25
  • Nginx配合php实现生成实时缩略图功能

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

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

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

    这篇文章主要为大家详细介绍了vue轻松实现水印效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-03