php生成背景颜色图片验证码

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

imagecreatetruecolor()创建一个真彩色图像后,
并不会自动把imagecolorallocate()方法注册的第一个颜色作为背景色,而必须用imagefill()去填充


//设置颜色

$bg = imagecolorallocate($im, 240, 240, 0);//设置背景颜色
imagefill($im,0,0,$bg);//载入背景颜色
$te = imagecolorallocate($im, 0, 0, 0);//字符串颜色
//将字符串加到图片上
imagestring($im,rand(3,6),rand(5,60),rand(5,15),$rand,$te);
//输出图片
header("Content-type: image/jpeg");
imagejpeg($im);

imagecreatetruecolor()返回一个图像标识符代表指定大小的黑色形象。

根据你的PHP和GD版本中函数定义与否。对于PHP 4.0.6通过4.1.x这个函数总是存在的


更多详细内容请查看:php教程er/24/php-imagecreatetruecolor.htm">http://www.111cn.net/phper/24/php-imagecreatetruecolor.htm

php教程生成缩略图类,支持自定义高和宽。还支持按高和宽截图

<?php 
class resizeimage 

    //图片类型 
    var $type; 
    //实际宽度 
    var $width; 
    //实际高度 
    var $height; 
    //改变后的宽度 
    var $resize_width; 
    //改变后的高度 
    var $resize_height; 
    //是否裁图 
    var $cut; 
    //源图象 
    var $srcimg; 
    //目标图象地址 
    var $dstimg; 
    //临时创建的图象 
    var $im; 
    function resizeimage($img, $wid, $hei,$c,$dstpath) 
    { 
        $this->srcimg = $img; 
        $this->resize_width = $wid; 
        $this->resize_height = $hei; 
        $this->cut = $c; 
        //图片的类型 

$this->type = strtolower(substr(strrchr($this->srcimg,"."),1)); 
        //初始化图象 
        $this->initi_img(); 
        //目标图象地址 
        $this -> dst_img($dstpath); 
        //-- 
        $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); 
                ImageJpeg ($newimg,$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)); 
                ImageJpeg ($newimg,$this->dstimg); 
            } 
        } 
        else
        //不裁图 
        { 
            if($ratio>=$resize_ratio) 
            { 
                $newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio); 
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height); 
                ImageJpeg ($newimg,$this->dstimg); 
            } 
            if($ratio<$resize_ratio) 
            { 
                $newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height); 
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height); 
                ImageJpeg ($newimg,$this->dstimg); 
            } 
        } 
    } 
    //初始化图象 
    function initi_img() 
    { 
        if($this->type=="jpg") 
        { 
            $this->im = imagecreatefromjpeg($this->srcimg); 
        } 
        if($this->type=="gif") 
        { 
            $this->im = imagecreatefromgif($this->srcimg); 
        } 
        if($this->type=="png") 
        { 
            $this->im = imagecreatefrompng($this->srcimg); 
        } 
    } 
    //图象目标地址 
    function dst_img($dstpath) 
    { 
        $full_length  = strlen($this->srcimg); 
        $type_length  = strlen($this->type); 
        $name_length  = $full_length-$type_length; 

        $name         = substr($this->srcimg,0,$name_length-1); 
        $this->dstimg = $dstpath; 

//echo $this->dstimg; 
    } 

$resizeimage = new resizeimage("11.jpg", "200", "150", "1","17.jpg"); 

此代码可以为上传的图片加鲨鱼同时还可以比例缩放图片,有需要的朋友可以参考一下。

<?php教程
$uptypes=array('image/jpg', //上传文件类型列表
'image/jpeg',
'image/png',
'image/pjpeg',
'image/gif',
'image/bmp',
'image/x-png');
$max_file_size=5000000; //上传文件大小限制, 单位BYTE
$destination_folder="upload/"; //上传文件路径
$watermark=1; //是否附加水印(1为加水印,其他为不加水印);
$watertype=1; //水印类型(1为文字,2为图片)
$waterposition=1; //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中);
$waterstring="newphp.site.cz"; //水印字符串
$waterimg="xplore.gif"; //水印图片
$imgpreview=1; //是否生成预览图(1为生成,其他为不生成);
$imgpreviewsize=1/2; //缩略图比例
?>
<html>
<head>
<title>M4U BLOG - fywyj.cn</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css教程">body,td{font-family:tahoma,verdana,arial;font-size:11px;line-height:15px;background-color:white;color:#666666;margin-left:20px;}
strong{font-size:12px;}
aink{color:#0066CC;}
a:hover{color:#FF6600;}
aisited{color:#003366;}
a:active{color:#9DCC00;}
table.itable{}
td.irows{height:20px;background:url("index.php?i=dots" repeat-x bottom}</style>
</head>
<body>
<center><form enctype="multipart/form-data" method="post" name="upform">
上传文件: <br><br><br>
<input name="upfile" type="file" style="width:200;border:1 solid #9a9999; font-size:9pt; background-color:#ffffff" size="17">
<input type="submit" value="上传" style="width:30;border:1 solid #9a9999; font-size:9pt; background-color:#ffffff" size="17"><br><br><br>
允许上传的文件类型为:jpg|jpeg|png|pjpeg|gif|bmp|x-png|swf <br><br>
<a href="index.php">返回</a>
</form>

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (!is_uploaded_file($_FILES["upfile"][tmp_name]))
//是否存在文件
{
echo "<font color='red'>文件不存在!</font>";
exit;
}

$file = $_FILES["upfile"];
if($max_file_size < $file["size"])
//检查文件大小
{
echo "<font color='red'>文件太大!</font>";
exit;
}

if(!in_array($file["type"], $uptypes))
//检查文件类型
{
echo "<font color='red'>只能上传图像文件或Flash!</font>";
exit;
}

if(!file_exists($destination_folder))
mkdir($destination_folder);

$filename=$file["tmp_name"];
$image_size = getimagesize($filename);
$pinfo=pathinfo($file["name"]);
$ftype=$pinfo[extension];
$destination = $destination_folder.time().".".$ftype;
if (file_exists($destination) && $overwrite != true)
{
echo "<font color='red'>同名文件已经存在了!</a>";
exit;
}

if(!move_uploaded_file ($filename, $destination))
{
echo "<font color='red'>移动文件出错!</a>";
exit;
}

$pinfo=pathinfo($destination);
$fname=$pinfo[basename];
echo " <font color=red>已经成功上传</font><br>文件名: <font color=blue>".$destination_folder.$fname."</font><br>";
echo " 宽度:".$image_size[0];
echo " 长度:".$image_size[1];
if($watermark==1)
{
$iinfo=getimagesize($destination,$iinfo);
$nimage=imagecreatetruecolor($image_size[0],$image_size[1]);
$white=imagecolorallocate($nimage,255,255,255);
$black=imagecolorallocate($nimage,0,0,0);
$red=imagecolorallocate($nimage,255,0,0);
imagefill($nimage,0,0,$white);
switch ($iinfo[2])
{
case 1:
$simage =imagecreatefromgif($destination);
break;
case 2:
$simage =imagecreatefromjpeg($destination);
break;
case 3:
$simage =imagecreatefrompng($destination);
break;
case 6:
$simage =imagecreatefromwbmp($destination);
break;
default:
die("<font color='red'>不能上传此类型文件!</a>");
exit;
}

imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]);
imagefilledrectangle($nimage,1,$image_size[1]-15,80,$image_size[1],$white);

switch($watertype)
{
case 1: //加水印字符串
imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black);
break;
case 2: //加水印图片
$simage1 =imagecreatefromgif("xplore.gif");
imagecopy($nimage,$simage1,0,0,0,0,85,15);
imagedestroy($simage1);
break;
}

switch ($iinfo[2])
{
case 1:
//imagegif($nimage, $destination);
imagejpeg($nimage, $destination);
break;
case 2:
imagejpeg($nimage, $destination);
break;
case 3:
imagepng($nimage, $destination);
break;
case 6:
imagewbmp($nimage, $destination);
//imagejpeg($nimage, $destination);
break;
}

//覆盖原上传文件
imagedestroy($nimage);
imagedestroy($simage);
}

if($imgpreview==1)
{
echo "<br>图片预览:<br>";
echo "<a href="".$destination."" target='_blank'><img src="".$destination."" width=".($image_size[0]*$imgpreviewsize)." height=".($image_size[1]*$imgpreviewsize);
echo " alt="图片预览:r文件名:".$destination."r上传时间:" border='0'></a>";
}
}
?>
</center>
</body>
</html>

可以教你个最简单的办法,对于JPG格式的图片,看最后两个字节是否是FFD9


如果不是的话,那么一般是不正常的。如果是其它格式的话,这个判断方式就不适用了。
正常的JPG文件都是以FFD8开头,FFD9结尾的,如果丢失了文件尾部,JPG仍然可以被识别,但是就会丢失部分图像数据。

--------
当然,我说的这种只是简单的判断,可能丢失前的那段结尾的正好是FFD9,但显然还是不正确的,但基本够用。如果追求严谨,那么就得阅读JPEG的格式规范文档了,
这已经不属于PHP的知识范畴了。

我试过判断图片格式和像素
你这显然不行,不够深入,要严谨,就必须深入了解JPG的存储。
至于php教程的GD库是否有相应的判断函数,我没关注过,你可以google,我就不多说了。欢迎你研究出来后共享

分享一个读取jpg的php文件

$adress="IMG_XXX.JPG";
$exif = read_exif_data ($adress);
while(list($k,$v)=each($exif)) {
if($k=="Thumbnail"){
$fp=fopen ("/www/home/image/Thumbnail$adress",
'a');
fwrite ($fp, $v);
fclose ($fp);
echo "<br />n";
echo "n";
echo "<br />n";
}else{
echo "$k: $v<br />n";
}
}

说 明:$maxwidth和$maxheight只能传递一个,如果传最大宽度将自动计算高度,如果传最大高度将自动计算宽度 * 返 回 值:如果创建成功返回文件保存的地址,否则返回false

 说    明:$maxwidth和$maxheight只能传递一个,如果传最大宽度将自动计算高度,如果传最大高度将自动计算宽度
* 返 回 值:如果创建成功返回文件保存的地址,否则返回false

<?php教程
/************************************************************************
* 函数名称:createSmallImg()
* 函数说明:创建等比例图片
* 输入参数:
 $dir 保存路径
 $source_img 原图片名称
 $small_ex 缩率图文件名后缀
 $maxwidth 最大宽度
 $maxheight 最大高度
* 说    明:$maxwidth和$maxheight只能传递一个,如果传最大宽度将自动计算高度,如果传最大高度将自动计算宽度
* 返 回 值:如果创建成功返回文件保存的地址,否则返回false
* 编 写 者:李小宇
* 编写时间:2011/8/18
**************************************************************************/
function createSmallImg($dir,$source_img,$small_ex="_s",$maxwidth='',$maxheight='') {
 if(!empty($maxwidth) && !empty($maxheight)) {
  return false;
 }
 $img_name=substr($source_img,0,-4);
 $img_ex = strtolower(substr(strrchr($source_img,"."),1));
 /*注释的这段用作直接在浏览器上显示图片
 $im=imagecreatefromjpeg($file);
 header("Content-type: image/jpeg");
 imagejpeg($im);*/
 switch($img_ex) {
  case "jpg":
   $src_img=imagecreatefromjpeg($dir.$source_img);
   break;
  case "gif":
   $src_img=imagecreatefromgif($dir.$source_img);
   break;
  case "png":
   $src_img=imagecreatefrompng($dir.$source_img);
   break;
 }
 $old_width=imagesx($src_img);
 $old_height=imagesy($src_img);
 if(!empty($maxheight) && $old_height>=$maxheight) {
  $new_height=$maxheight;
  $new_width=round(($old_width*$new_height)/$old_height);
 } elseif(!empty($maxwidth) && $old_width>=$maxwidth) {
  $new_width=$maxwidth;
  $new_height=round(($old_height*$new_width)/$old_width);
 }
 if(!empty($new_width) || !empty($new_height)) {
  if($img_ex=="jpg" || $img_ex=="png") {
   $dst_img=imagecreatetruecolor($new_width,$new_height);
  } else {
   $dst_img=imagecreate($new_width,$new_height);
  }
  imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_width,$new_height,$old_width,$old_height);
  $smallname=$dir.$img_name.$small_ex.".".$img_ex;
  switch($img_ex) {
   case "jpg":
    imagejpeg($dst_img,$smallname,100);
    break;
   case "gif":
    imagegif($dst_img,$smallname);
    break;
   case "png":
    imagepng($dst_img,$smallname);
    break;
  }
 }
 return $smallname;
}

[!--infotagslink--]

相关文章

  • ps把文字背景变透明的操作方法

    ps软件是现在非常受大家喜欢的一款软件,有着非常不错的使用功能。这次文章就给大家介绍下ps把文字背景变透明的操作方法,喜欢的一起来看看。 1、使用Photoshop软件...2017-07-06
  • PHP 验证码不显示只有一个小红叉的解决方法

    最近想自学PHP ,做了个验证码,但不知道怎么搞的,总出现一个如下图的小红叉,但验证码就是显示不出来,原因如下 未修改之前,出现如下错误; (1)修改步骤如下,原因如下,原因是apache权限没开, (2)点击打开php.int., 搜索extension=ph...2013-10-04
  • JS实现随机生成验证码

    这篇文章主要为大家详细介绍了JS实现随机生成验证码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-06
  • php二维码生成

    本文介绍两种使用 php 生成二维码的方法。 (1)利用google生成二维码的开放接口,代码如下: /** * google api 二维码生成【QRcode可以存储最多4296个字母数字类型的任意文本,具体可以查看二维码数据格式】 * @param strin...2015-10-21
  • jQuery Real Person验证码插件防止表单自动提交

    本文介绍的jQuery插件有点特殊,防自动提交表单的验证工具,就是我们经常用到的验证码工具,先给大家看看效果。效果图如下: 使用说明 需要使用jQuery库文件和Real Person库文件 同时需要自定义验证码显示的CSS样式 使用实例...2015-11-08
  • Java生成随机姓名、性别和年龄的实现示例

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

    这篇文章主要介绍了C#生成随机数功能,涉及C#数学运算与字符串操作相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • jQuery为动态生成的select元素添加事件的方法

    下面小编就为大家带来一篇jQuery为动态生成的select元素添加事件的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-09-01
  • Jquery插件实现点击获取验证码后60秒内禁止重新获取

    通过jquery.cookie.js插件可以快速实现“点击获取验证码后60秒内禁止重新获取(防刷新)”的功能效果图:先到官网(http://plugins.jquery.com/cookie/)下载cookie插件,放到相应文件夹,代码如下:复制代码 代码如下: <!DOCTYPE ht...2015-03-15
  • PHP自动生成后台导航网址的最佳方法

    经常制作开发不同的网站的后台,写过很多种不同的后台导航写法。 最终积累了这种最写法,算是最好的吧...2013-09-29
  • php生成唯一数字id的方法汇总

    关于生成唯一数字ID的问题,是不是需要使用rand生成一个随机数,然后去数据库查询是否有这个数呢?感觉这样的话有点费时间,有没有其他方法呢?当然不是,其实有两种方法可以解决。 1. 如果你只用php而不用数据库的话,那时间戳+随...2015-11-24
  • php实现点击可刷新验证码

    验证码类文件 CreateImg.class.php <&#63;php class ValidationCode { private $width,$height,$codenum; public $checkcode; //产生的验证码 private $checkimage; //验证码图片 private $disturbColor = ''; /...2015-11-08
  • ps快速抠出白色背景下的透明玻璃杯教程

    ps软件是现在很多人比较喜欢的,这次文章就给大家介绍ps软件的抠图技巧,ps快速抠出白色背景下的透明玻璃杯,一起来看看。 &#8195;&#8195;1、如果是白底抠图的话,先将...2017-07-06
  • JavaScript实现点击按钮切换网页背景色的方法

    本文实例讲述了JavaScript实现点击按钮切换网页背景色的方法。分享给大家供大家参考,具体如下:这里演示JavaScript用按钮随意变换背景颜色,每点击一下按钮,就可以随机改变一种网页背景颜色。颜色值事先是保存在JS的数组里...2015-10-21
  • 基于JavaScript实现验证码功能

    这篇文章主要介绍了基于JavaScript实现验证码功能的相关资料...2017-04-03
  • js生成随机数的方法实例

    js生成随机数主要用到了内置的Math对象的random()方法。用法如:Math.random()。它返回的是一个 0 ~ 1 之间的随机数。有了这么一个方法,那生成任意随机数就好理解了。比如实际中我们可能会有如下的需要: (1)生成一个 0 - 1...2015-10-21
  • 单击按钮发送验证码,出现倒计时的简单实例

    下面小编就为大家带来一篇单击按钮发送验证码,出现倒计时的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧 代码...2017-07-06
  • PHP验证码生成与验证例子

    验证码是一个现在WEB2.0中常见的一个功能了,像注册、登录又或者是留言页面,都需要注册码来验证当前操作者的合法性,我们会看到有些网站没有验证码,但那是更高级的验证了,...2016-11-25
  • PHP生成不同颜色、不同大小的tag标签函数

    复制代码 代码如下:function getTagStyle(){ $minFontSize=8; //最小字体大小,可根据需要自行更改 $maxFontSize=18; //最大字体大小,可根据需要自行更改 return 'font-size:'.($minFontSize+lcg_value()*(abs($maxFo...2013-10-04
  • 基于Pytorch版yolov5的滑块验证码破解思路详解

    这篇文章主要介绍了基于Pytorch版yolov5的滑块验证码破解思路详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-02-25