php 把ip生成在图片上

 更新时间:2016年11月25日 16:58  点击:1313
在很多地方我们都会看到把ip地址生成在图片哦,今天我们就来告诉你如何利用php 把ip生成在图片上哦,好了下面我们就来看看实例吧。
 代码如下 复制代码

$img = imagecreate(180,50);

  $ip = $_server['remote_addr'];

  imagecolortransparent($img,$bgcolor);

  $bgcolor = imagecolorallocate($img, 0x2c,0x6d,0xaf); // 背景颜色

  $shadow = imagecolorallocate($img, 250,0,0); // 阴影颜色

  $textcolor = imagecolorallocate($img, oxff,oxff,oxff); // 字体颜色

  imagettftext($img,10,0,78,30,$shadow,"c:/windows/fonts/tahoma.ttf",$ip);

  //显示背景

  imagettftext($img,10,0,25,28,$textcolor,"c:/windows/fonts/tahoma.ttf","your ip is".$ip);

  // 显示ip

  imagepng($img);

  imagecreatefrompng($img);

  imagedestroy($img);

其实就利用了php生成图片功能,生成验证码也经常会用到这样的功能哦。

这款关于php 中文汉字验证码生成程序其实很简单的,生成中文汉字验证码需要字体来支持了,下面我们用的arial.ttf来支持生成,否则生成中文图片验证码会出乱码的。
 代码如下 复制代码

class securecode
{
    private static $instance=null;
    private $code = '';
    private $fontfile;
    private $validate;
    private $image;
    private $specialadd = 'special string for securecode';
    private $codeexpire=86400;
    private $codecookiename='secure_code';

    /**
     * 构造方法
     */
    private function securecode()
    {
        $this->fontfile = dirname( __file__ ) . '/arial.ttf';
    }

    private function __construct()
    {
        $this->securecode();
    }
   
    public static function getinstance()
    {
        if (self::$instance==null)
            self::$instance=new self();
       
        return self::$instance;
    }

    /**
     * 指定字体文件所在路径,默认为当前文件夹下arial.ttf文件
     * @param $fontfile 文件路径
     * @return void
     */
    function loadfont($fontfile)
    {
        $this->fontfile = $fontfile;
    }

    /**
     * 图片输出方法,在执行本方法前程序不应该有任何形式的输出
     * @return void;
     */
    function stroke()
    {
        $this->savecode();
        self::sendheader();
        imagegif( $this->validate );
        imagedestroy( $this->validate );
        imagedestroy( $this->image );
    }

    /**
     * 图片保存方法
     * @param $filename 保存路径
     * @return void
     */
    function save($filename)
    {
        $this->savecode();
        imagegif( $this->validate , $filename );
        imagedestroy( $this->validate );
        imagedestroy( $this->image );
    }
   
  /**
     * 验证码验证方法
     * @param $input 要验证的字符串,即用户的输入内容
     * @return boolean 验证结果
     */
    function verify($input)
    {
        $input=strtolower($input);
        $targetcode=$this->authcode($input);
        $code=$this->getcookie();
        if (empty($code)||$code!=$targetcode)
            $result= false;
        else
            $result=true;
        $_cookie[$this->codecookiename]='';
        setcookie ( $this->codecookiename, '', - 1 );
        return $result;
    }

    /**
     * 图片创建方法
     * @return void;
     */
    function createimage()
    {
        $this->randcode();
       
        $size = 30;
        $width = 90;
        $height = 35;
        $degrees = array (
            rand( 0 , 30 ), rand( 0 , 30 ), rand( 0 , 30 ), rand( 0 , 30 )
        );
       

        for ($i = 0; $i < 4; ++$i)
        {
            if (rand() % 2);
            else $degrees[$i] = -$degrees[$i];
        }
       
        $this->image = imagecreatetruecolor( $size , $size );
        $this->validate = imagecreatetruecolor( $width , $height );
        $back = imagecolorallocate( $this->image , 255 , 255 , 255 );
        $border = imagecolorallocate( $this->image , 0 , 0 , 0 );
        imagefilledrectangle( $this->validate , 0 , 0 , $width , $height , $back );
       
        for ($i = 0; $i < 4; ++$i)
        {
            $temp = self::rgbtohsv( rand( 0 , 250 ) , rand( 0 , 150 ) , rand( 0 , 250 ) );
           
            if ($temp[2] > 60) $temp[2] = 60;
           
            $temp = self::hsvtorgb( $temp[0] , $temp[1] , $temp[2] );
            $textcolor[$i] = imagecolorallocate( $this->image , $temp[0] , $temp[1] , $temp[2] );
        }
       
        for ($i = 0; $i < 200; ++$i)
        {
            $randpixelcolor = imagecolorallocate( $this->validate , rand( 0 , 255 ) , rand( 0 , 255 ) , rand( 0 , 255 ) );
            imagesetpixel( $this->validate , rand( 1 , 87 ) , rand( 1 , 35 ) , $randpixelcolor );
        }
       
        $temp = self::rgbtohsv( rand( 220 , 255 ) , rand( 220 , 255 ) , rand( 220 , 255 ) );
       
        if ($temp[2] < 200) $temp[2] = 255;
       
        $temp = self::hsvtorgb( $temp[0] , $temp[1] , $temp[2] );
        $randlinecolor = imagecolorallocate( $this->image , $temp[0] , $temp[1] , $temp[2] );
       
        self::imagelinethick( $this->validate , $textcolor[rand( 0 , 3 )] );
       
        imagefilledrectangle( $this->image , 0 , 0 , $size , $size , $back );
        putenv( 'gdfontpath=' . realpath( '.' ) );
       
        // name the font to be used (note the lack of the .ttf extension
        imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[0] , $this->fontfile , $this->code[0] );
       
        $this->image = imagerotate( $this->image , $degrees[0] , $back );
        imagecolortransparent( $this->image , $back );
        imagecopymerge( $this->validate , $this->image , 1 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 );
       
        $this->image = imagecreatetruecolor( $size , $size );
        imagefilledrectangle( $this->image , 0 , 0 , $size , $size , $back );
        imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[1] , $this->fontfile , $this->code[1] );
        $this->image = imagerotate( $this->image , $degrees[1] , $back );
        imagecolortransparent( $this->image , $back );
        imagecopymerge( $this->validate , $this->image , 21 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 );
       
        $this->image = imagecreatetruecolor( $size , $size );
        imagefilledrectangle( $this->image , 0 , 0 , $size - 1 , $size - 1 , $back );
        imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[2] , $this->fontfile , $this->code[2] );
        $this->image = imagerotate( $this->image , $degrees[2] , $back );
        imagecolortransparent( $this->image , $back );
        imagecopymerge( $this->validate , $this->image , 41 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 );
       
        $this->image = imagecreatetruecolor( $size , $size );
        imagefilledrectangle( $this->image , 0 , 0 , $size - 1 , $size - 1 , $back );
        imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[3] , $this->fontfile , $this->code[3] );
        $this->image = imagerotate( $this->image , $degrees[3] , $back );
        imagecolortransparent( $this->image , $back );
        imagecopymerge( $this->validate , $this->image , 61 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 );
        imagerectangle( $this->validate , 0 , 0 , $width - 1 , $height - 1 , $border );
    }

 

本文章免费提供一款php图片处理类:缩略,裁剪,圆角,倾斜代码,他可以切出不同风格的图片哦,哈哈,比其它的在线切图要好很多了哦。

 代码如下 复制代码
class resizeimage
{
   //图片类型
   var $type;
   //实际宽度
   var $width;
   //实际高度
   var $height;
   //改变后的宽度
   var $resize_width;
   //改变后的高度
   var $resize_height;
   //是否裁图
   var $cut;
   //源图象
   var $srcimg;
   //目标图象地址
   var $dstimg;
   //圆角源
   var $corner;
   var $im;

function resizeimage($img, $corner, $wid, $hei,$c, $corner_radius, $angle)
   {
       $this->srcimg = $img;
           $this->corner = $corner;
       $this->resize_width = $wid;
       $this->resize_height = $hei;
       $this->cut = $c;
           $this->corner_radius = $corner_radius;
           $this->angle = $angle;
       //图片的类型
       $this->type = substr(strrchr($this->srcimg,"."),1);
       //初始化图象
       $this->initi_img();
       //目标图象地址
       $this -> dst_img();
       //--
       $this->width = imagesx($this->im);
       $this->height = imagesy($this->im);
       //生成图象
       $this->newimg();
       imagedestroy ($this->im);
   }
   function newimg()
   {
       //改变后的图象的比例
       $resize_ratio = ($this->resize_width)/($this->resize_height);
       //实际图象的比例
       $ratio = ($this->width)/($this->height);
       if(($this->cut)=="1")
       //裁图
       {
           if($ratio>=$resize_ratio)
           //高度优先
           {
               $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
               imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height);
                           $tmp = $this->rounded_corner($newimg,$this->resize_width);
               imagepng ($tmp,$this->dstimg);
           }
           if($ratio<$resize_ratio)
           //宽度优先
           {
               $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
               imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio));
                           $tmp = $this->rounded_corner($newimg);
               imagepng ($tmp,$this->dstimg);
           }
       }
       else
      

 

 

本款php生成验证码代码原理生成随机数-->创建图片-->随机数写进图片-->保存在session中 ,看一下流程验证码图片生成 通知浏览器将要输出PNG图片 准备好随机数发生器种子 srand((double)microtime()*1000000); 将四位整数验证码绘入图片

 代码如下 复制代码

/*=====================
产生随机字符串函数
=====================*/
function random($length) {
 $hash = '';
 $chars = 'abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz';
 $max = strlen($chars) - 1;
 mt_srand((double)microtime() * 1000000);
 for($i = 0; $i < $length; $i++) {
  $hash .= $chars[mt_rand(0, $max)];
 }
 return $hash;
}

//验证码图片生成

 session_start();
    //通知浏览器将要输出png图片
    header("content-type: image/png");
    //准备好随机数发生器种子 
    //srand((double)microtime()*1000000);
    //准备图片的相关参数  
    $im = imagecreate(62,22);
    $black = imagecolorallocate($im, 0,0,0);  //rgb黑色标识符
    $white = imagecolorallocate($im, 255,255,255); //rgb白色标识符
    $gray = imagecolorallocate($im, 179,183,185); //rgb灰色标识符
    //开始作图    
    imagefill($im,0,0,$gray);
    //while(($randval=rand()%100000)<10000);{
        //$_session["check_code"] = $randval;
        //将四位整数验证码绘入图片
  $randval=random(4);
  $_session["check_code"]=$randval;
        imagestring($im, 5, 10, 3, $randval, $white);
    //}
    //加入干扰象素   
    for($i=0;$i<150;$i++){
        $randcolor = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
        imagesetpixel($im, rand()%70 , rand()%30 , $white);
    }
    //输出验证图片
    imagepng($im);
    //销毁图像标识符
    imagedestroy($im);

?>

本款是一款php 读取目录下图像文件哦,代码,他利用了opendir来打开目录然后获取文件后缀名,判断是否为指定文件。
 代码如下 复制代码

$directory = 'gallery';

$allowed_types=array('jpg','jpeg','gif','png');
$file_parts=array();
$ext='';
$title='';
$i=0;

$dir_handle = @opendir($directory) or die("there is an error with your image directory!");

while ($file = readdir($dir_handle))
{
 if($file=='.' || $file == '..') continue;
 
 $file_parts = explode('.',$file);
 $ext = strtolower(array_pop($file_parts));

 $title = implode('.',$file_parts);
 $title = htmlspecialchars($title);
 
 $nomargin='';
 
 if(in_array($ext,$allowed_types))
 {
  if(($i+1)%4==0) $nomargin='nomargin';
 
  echo '
  <div class="pic '.$nomargin.'" style="background:url('.$directory.'/'.$file.') no-repeat 50% 50%;">
  <a href="'.$directory.'/'.$file.'" title="'.$title.'" target="_blank">'.$title.'</a>
  </div>';
  
  $i++;
 }
}

closedir($dir_handle);

[!--infotagslink--]

相关文章

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

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

    这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
  • Python 图片转数组,二进制互转操作

    这篇文章主要介绍了Python 图片转数组,二进制互转操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-09
  • Photoshop古装美女图片转为工笔画效果制作教程

    今天小编在这里就来给各位Photoshop的这一款软件的使用者们来说说把古装美女图片转为细腻的工笔画效果的制作教程,各位想知道方法的使用者们,那么下面就快来跟着小编一...2016-09-14
  • php抓取网站图片并保存的实现方法

    php如何实现抓取网页图片,相较于手动的粘贴复制,使用小程序要方便快捷多了,喜欢编程的人总会喜欢制作一些简单有用的小软件,最近就参考了网上一个php抓取图片代码,封装了一个php远程抓取图片的类,测试了一下,效果还不错分享...2015-10-30
  • 源码分析系列之json_encode()如何转化一个对象

    这篇文章主要介绍了源码分析系列之json_encode()如何转化一个对象,对json_encode()感兴趣的同学,可以参考下...2021-04-22
  • php中去除文字内容中所有html代码

    PHP去除html、css样式、js格式的方法很多,但发现,它们基本都有一个弊端:空格往往清除不了 经过不断的研究,最终找到了一个理想的去除html包括空格css样式、js 的PHP函数。...2013-08-02
  • jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮

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

    下面小编就为大家带来一篇利用JS实现点击按钮后图片自动切换的简单方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-10-25
  • index.php怎么打开?如何打开index.php?

    index.php怎么打开?初学者可能不知道如何打开index.php,不会的同学可以参考一下本篇教程 打开编辑:右键->打开方式->经文本方式打开打开运行:首先你要有个支持运行PH...2017-07-06
  • Photoshop枪战电影海报图片制作教程

    Photoshop的这一款软件小编相信很多的人都已经是使用过了吧,那么今天小编在这里就给大家带来了用Photoshop软件制作枪战电影海报的教程,想知道制作步骤的玩家们,那么下面...2016-09-14
  • php二维码生成

    本文介绍两种使用 php 生成二维码的方法。 (1)利用google生成二维码的开放接口,代码如下: /** * google api 二维码生成【QRcode可以存储最多4296个字母数字类型的任意文本,具体可以查看二维码数据格式】 * @param strin...2015-10-21
  • Java生成随机姓名、性别和年龄的实现示例

    这篇文章主要介绍了Java生成随机姓名、性别和年龄的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-10-01
  • python opencv通过4坐标剪裁图片

    图片剪裁是常用的方法,那么如何通过4坐标剪裁图片,本文就详细的来介绍一下,感兴趣的小伙伴们可以参考一下...2021-06-04
  • C#生成随机数功能示例

    这篇文章主要介绍了C#生成随机数功能,涉及C#数学运算与字符串操作相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • js实现上传图片及时预览

    这篇文章主要为大家详细介绍了js实现上传图片及时预览的相关资料,具有一定的参考价值,感兴趣的朋友可以参考一下...2016-05-09
  • 使用PHP下载CSS文件中的图片的代码

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

    复制代码 代码如下:<?php function jb51(){ print_r(func_get_args()); echo "<br>"; echo func_get_arg(1); echo "<br>"; echo func_num_args(); } jb51("www","j...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
  • C#中图片旋转和翻转(RotateFlipType)用法分析

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