gd2验证码

<?php
//创建画布
$image = imagecreatetruecolor(300,200);
//分配背景色
$bg = imagecolorallocate($image,mt_rand(2,200),mt_rand(2,200),mt_rand(2,200));
//填充颜色
imagefill($image,0,0,$bg);
$font = 5;
$array = array_merge(range(0,9),range('A','Z'));
$index = array_rand($array,5);
shuffle($index);
$string = '';
foreach($index as $value){
    $string .=$array[$value];
}
$x = (imagesx($image) - imagefontwidth($font)*strlen($string))/2;
$y = (imagesy($image) -imagefontheight($font))/2;
//设置字符串颜色
$color2 = imagecolorallocate($image,mt_rand(2,200),mt_rand(2,200),mt_rand(2,200));
//将提供的字符串,写入图片
imagestring($image,$font,$x,$y,$string,$color2);
//输出图像
header('content-type:image/jpeg');
imagejpeg($image);
//销毁图像
imagedestroy($image);
<?php
//自定义图像
$filename = './timg.jpg';
$index = strrpos($filename,'.');
$ext = substr($filename,$index+1);
$array = array(
    'jpg' => 'imagecreatefromjpeg',
    'png' => 'imagecreatefrompng ',
    'gif' => 'imagecreatefromgif'
);
$image = $array[$ext]($filename);
//$image = imagecreatefromjpeg($filename);
$font = 5;
$array = array_merge(range(0,9),range('A','Z'));
$index = array_rand($array,5);
shuffle($index);
$string = '';
foreach($index as $value){
    $string .=$array[$value];
}
$x = (imagesx($image) - imagefontwidth($font)*strlen($string))/2;
$y = (imagesy($image) -imagefontheight($font))/2;
//设置字符串颜色
$color2 = imagecolorallocate($image,mt_rand(2,200),mt_rand(2,200),mt_rand(2,200));
//将提供的字符串,写入图片
imagestring($image,$font,$x,$y,$string,$color2);
//输出图像
header('content-type:image/jpeg');
imagejpeg($image);
//销毁图像
imagedestroy($image);

 

<?php
//缩略图
$filename = './timg.jpg';
$index = strrpos($filename,'.');
$ext = substr($filename,$index+1);
$array = array(
    'jpg' => 'imagecreatefromjpeg',
    'png' => 'imagecreatefrompng ',
    'gif' => 'imagecreatefromgif'
);
$image = $array[$ext]($filename);
//得到图像宽高1
// $src_w = imagesx($image);
// $src_h = imagesy($image);
//得到图像宽高2
list($src_w,$src_h) = getimagesize($filename);
$dst_w = $src_w*0.5;
$dst_h = $src_h*0.5;
$dst_image = imagecreatetruecolor($dst_w,$dst_h);\
//重新拷贝获取大小1  速度快
//imagecopyresampled($dst_image,$image,0,0,0,0,$dst_w, $dst_h, $src_w, $src_h);
//重新拷贝获取大小2  速度慢
imagecopyresized($dst_image,$image,0,0,0,0,$dst_w, $dst_h, $src_w, $src_h);

header('content-type:image/jpeg');
imagejpeg($dst_image);
//销毁图像
imagedestroy($image,$dst_image);

 

posted @ 2019-12-18 15:50  za_szybko  阅读(179)  评论(0编辑  收藏  举报