一款超级简单php图片水印代码

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

在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);

1.用imagecreatetruecolor和imagecopyresampled函数分别取代imagecreate和imagecopyresized
2.给imagejpeg的第三个参数带上100(例:imagejpeg($ni,$tofile,100))

imagecreatetruecolor -- 新建一个真彩色图像
说明
resource imagecreatetruecolor ( int x_size, int y_size )
imagecreatetruecolor() 返回一个图像标识符,代表了一幅大小为 x_size 和 y_size 的黑色图像

*/

header ("content-type: image/png");
$im = @imagecreatetruecolor (50, 100)
     or die ("cannot initialize new gd image stream");
$text_color = imagecolorallocate ($im, 233, 14, 91);
imagestring ($im, 1, 5, 5,  "a simple text string", $text_color);
imagepng ($im);
imagedestroy ($im);

/*


如果使用普通的imagecreate()函数将造成图片质量失真的情况,从网上搜了一下解决办法,方法是用imagecreateruecolor()函数替换imagecreate()函数。
*/

function createpreview($img,$name,$path,$maxwidth,$maxheight,$quality){//图片,保存名称,保存路径,最大宽,最大高,质量
 $widthratio=0;
 $heightratio=0;
 $width=imagesx($img);
 $height=imagesy($img);
 //开始计算缩小比例
 if($width>$maxwidth||$height>$maxheight){
  if($width>$maxwidth){
   $widthratio=$maxwidth/$width;
  }
  if($height>$maxheight){
   $heightratio=$maxheight/$height;
  }
  if($widthratio>0&&$heightratio>0){
   if($widthratio<$heightratio){
    $ratio=$widthratio;
   }else{
    $ratio=$heightratio;
   }
  }elseif($widthratio>0){
   $ratio=$widthratio;
  }elseif($heightratio>0){
   $ratio=$heightratio;
  }
  //根据得出的比例,重新计算缩略图的宽和高
  $newwidth=$ratio*$width;
  $newheight=$ratio*$height;
  $newimg=imagecreatetruecolor($newwidth,$newheight); // 创建目标图
  imagecopyresized($newimg,$img,0,0,0,0,$newwidth,$newheight,$width,$height);
  imagejpeg($newimg,$path."s_".$name,$quality);
  imagedestroy($newimg);
 }else{
  imagejpeg($img,$path."s_".$name,$quality);
 }
}
/*

imagecopyresamples() ,其像素插值算法得到的图像边缘比较平滑.质量较好(但该函数的速度比 imagecopyresized() 慢).

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

 代码如下 复制代码

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教程生成验证码详细教程,前面是讲关于生成验证核心代码,后面是一款生成与调用的方法。

 代码如下 复制代码

<?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);
?>

[!--infotagslink--]

相关文章

  • 使用PHP+JavaScript将HTML页面转换为图片的实例分享

    这篇文章主要介绍了使用PHP+JavaScript将HTML元素转换为图片的实例分享,文后结果的截图只能体现出替换的字体,也不能说将静态页面转为图片可以加快加载,只是这种做法比较interesting XD需要的朋友可以参考下...2016-04-19
  • C#从数据库读取图片并保存的两种方法

    这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
  • Photoshop古装美女图片转为工笔画效果制作教程

    今天小编在这里就来给各位Photoshop的这一款软件的使用者们来说说把古装美女图片转为细腻的工笔画效果的制作教程,各位想知道方法的使用者们,那么下面就快来跟着小编一...2016-09-14
  • Python 图片转数组,二进制互转操作

    这篇文章主要介绍了Python 图片转数组,二进制互转操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-09
  • php抓取网站图片并保存的实现方法

    php如何实现抓取网页图片,相较于手动的粘贴复制,使用小程序要方便快捷多了,喜欢编程的人总会喜欢制作一些简单有用的小软件,最近就参考了网上一个php抓取图片代码,封装了一个php远程抓取图片的类,测试了一下,效果还不错分享...2015-10-30
  • jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮

    jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮...2013-10-13
  • 利用JS实现点击按钮后图片自动切换的简单方法

    下面小编就为大家带来一篇利用JS实现点击按钮后图片自动切换的简单方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-10-25
  • Photoshop枪战电影海报图片制作教程

    Photoshop的这一款软件小编相信很多的人都已经是使用过了吧,那么今天小编在这里就给大家带来了用Photoshop软件制作枪战电影海报的教程,想知道制作步骤的玩家们,那么下面...2016-09-14
  • js实现上传图片及时预览

    这篇文章主要为大家详细介绍了js实现上传图片及时预览的相关资料,具有一定的参考价值,感兴趣的朋友可以参考一下...2016-05-09
  • python opencv通过4坐标剪裁图片

    图片剪裁是常用的方法,那么如何通过4坐标剪裁图片,本文就详细的来介绍一下,感兴趣的小伙伴们可以参考一下...2021-06-04
  • 使用PHP下载CSS文件中的图片的代码

    共享一段使用PHP下载CSS文件中的图片的代码 复制代码 代码如下: <?php //note 设置PHP超时时间 set_time_limit(0); //note 取得样式文件内容 $styleFileContent = file_get_contents('images/style.css'); //not...2013-10-04
  • PHP swfupload图片上传的实例代码

    PHP代码如下:复制代码 代码如下:if (isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) { $upload_file = $_FILES['Filedata']; $fil...2013-10-04
  • 微信小程序如何获取图片宽度与高度

    这篇文章主要给大家介绍了关于微信小程序如何获取图片宽度与高度的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-10
  • C#中图片旋转和翻转(RotateFlipType)用法分析

    这篇文章主要介绍了C#中图片旋转和翻转(RotateFlipType)用法,实例分析了C#图片旋转及翻转Image.RotateFlip方法属性的常用设置技巧,需要的朋友可以参考下...2020-06-25
  • ps怎么制作图片阴影效果

    ps软件是现在很多人比较喜欢的,有着非常不错的使用效果,这次文章就给大家介绍下ps怎么制作图片阴影效果,还不知道制作方法的赶紧来看看。 ps图片阴影效果怎么做方法/...2017-07-06
  • OpenCV如何去除图片中的阴影的实现

    这篇文章主要介绍了OpenCV如何去除图片中的阴影的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-29
  • C#将图片和字节流互相转换并显示到页面上

    本文主要介绍用C#实现图片转换成字节流,字节流转换成图片,并根据图片路径返回图片的字节流,有需要的朋友可以参考下...2020-06-25
  • JavaScript 如何禁止用户保存图片

    这篇文章主要介绍了JavaScript 如何禁止用户保存图片,帮助大家完成需求,更好的理解和使用JavaScript,感兴趣的朋友可以了解下...2020-11-19
  • php上传图片学习笔记与心得

    我们在php中上传文件就必须使用#_FILE变量了,这个自动全局变量 $_FILES 从 PHP 4.1.0 版本开始被支持。在这之前,从 4.0.0 版本开始,PHP 支持 $HTTP_POST_FILES 数组。这...2016-11-25
  • JS实现图片的不间断连续滚动的简单实例

    下面小编就为大家带来一篇JS实现图片的不间断连续滚动的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-06-12