微型php框架 library/image.class.php

<?php

// 验证码类

class image {

protected $im;

protected $img_width;

protected $img_height;

protected $img_type;

// 生成随机数

static public function randStr($n = 4) {

if($n <= 0) {

return ‘‘;

}

$str = ‘abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ0123456789‘;

$str = substr(str_shuffle($str),0,$n);

return $str;

}

// 生成验证码

static public function chkcode($w=60,$h=25) {

// $w 宽, $h 高

$code = self::randStr(4);

// 造画布

$im = imagecreatetruecolor($w,$h);

$bak = imagecreatetruecolor($w,$h);

// 造颜色,灰底+蓝字

$gray = imagecolorallocate($im,100,100,100);

$blue = imagecolorallocate($im,0,0,255);

$bak_gray = imagecolorallocate($bak,100,100,100);

// 填充

imagefill($im,0,0,$gray);

imagefill($bak,0,0,$bak_gray);

// 写字

imagestring($im,5,10,5,$code,$blue);

/*****

把验证码存储到session里

*****/

/*****

把验证码的字符扭曲,供参考,不要求.

$niu = 3;

for($i=0;$i<60;$i++) {

// 按正弦函数计算Y轴的波动

$y = sin(deg2rad((720/$w) * $i)) * $niu;

imagecopy($bak,$im,$i,$y,$i,0,1,$h);

}

*****/

header(‘content-type: image/jpeg‘);

imagejpeg($bak);

}

static public function make_thumb($ori,$w=200,$h=200) {

// 判断原图大小,如果原图比缩略还小,不必处理.

// 读出大图当画布

$info = self::getinfo($ori);

if($info[‘func‘] === false) {

return false;

}

$createfunc = ‘imagecreatefrom‘ . $info[‘func‘]; // 分析出读取大图所用的函数名.

$src = $createfunc($ori);

// 创建小画布,并把背景做成灰色

$small = imagecreatetruecolor($w,$h);

$gray = imagecolorallocate($small,100,100,100);

imagefill($small,0,0,$gray);

// 复制大图到小图

$scale = min($w/$info[‘width‘], $h/$info[‘height‘]); // 以更小的缩小比例为准,才能装下

// 根据比例,算最终复制过去的块的大小.

$realw = $info[‘width‘] * $scale;

$realh = $info[‘height‘] * $scale;

// 生成小图

/*

bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )

*/

// 计算留白

$lw = round(($w - $realw)/2); // 计算左侧留的宽度

$lh = round(($h - $realh)/2); // 计算上部留的高度

imagecopyresampled($small,$src,$lw,$lh,0,0,$realw,$realh,$info[‘width‘],$info[‘height‘]);

/*

header(‘content-type: image/jpeg‘);

imagejpeg($small);

*/

// 计算小图片的存储路径

$thumburl = str_replace(‘.‘,‘_thumb.‘,$ori);

$imagefunc = ‘image‘ . $info[‘func‘];

if($imagefunc($small,$thumburl)) {

return str_replace(ROOT,‘‘,$thumburl);

} else {

return false;

}

}

static public function getinfo($ori) {

$arr = getimagesize($ori);

// 如果原始图片分析不出来,直接false

if($arr === false) {

return false;

}

$info = array();

$info[‘width‘] = $arr[0];

$info[‘height‘] = $arr[1];

switch($arr[2]) {

case 1:

$info[‘func‘] = ‘gif‘;

break;

case 2:

$info[‘func‘] = ‘jpeg‘;

break;

case 3:

$info[‘func‘] = ‘png‘;

break;

case 6:

$info[‘func‘] = ‘wbmp‘;

break;

default:

$info[‘func‘] = false;

}

return $info;

}

}

image::chkcode();

?>

微型php框架 library/image.class.php

时间: 2024-08-10 10:31:46

微型php框架 library/image.class.php的相关文章

视频教程--ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驱动连接 PostgreSQL数据库

说好的给园子里的朋友们录制与<ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驱动连接 PostgreSQL数据库> 这篇博客相对应的视频,由于一个月一来没有时间录制,今天我兑现了给朋友们的承诺.. 本次视频教程的目录为 视频.代码.资料,其中视频有4段,资料是我收集的相关资料.. 视频下载地址:http://pan.baidu.com/s/1c05sysC 希望大家多多支持... 郝喜路 2014年6月8日 11:11:02   http://haoxilu.cn

微型php框架 include/mysql.class.php

<?php defined('ACC')||exit('Access Denied'); // 封装mysql操作类,包括连接功能,及查询功能. class mysql extends absdb{ protected static $ins = null; protected $host;  // 主机名 protected $user;  // 用户名 protected $passwd; // 密码 protected $db;      // 数据库名 protected $port;

微型php框架 include/log.class.php

//错误操作类 <?php class log { public static function write($err) { $fh = fopen(ROOT . 'data/log.txt','a'); // 追加方式打开,允许从后面追加内容 $err = date('Y-m-d H:i:s',time()) . "\r\n" . $err; fwrite($fh,$err); fclose($fh); } } 微型php框架 include/log.class.php

微型php框架 include/config.php

配置文件 <?php defined('ACC')||exit('Access Denied'); $cfg = array(); $cfg['host'] = 'localhost'; $cfg['user'] = 'root'; $cfg['pwd'] = '111111'; $cfg['db'] = 'php0620'; $cfg['port'] = '3306'; $cfg['charset'] = 'utf8'; $cfg['updir'] = 'upload/'; 微型php框架 i

微型php框架 include/conf.class.php

<?php defined('ACC')||exit('Access Denied'); // 配置文件读取类 class conf { protected static $ins = null; protected $cfg = array('db'=>'java0620'); public static function getIns() { if(self::$ins === null) { self::$ins = new self(); } return self::$ins; }

微型php框架 include/absdb.class.php

<?php // 一个抽象的数据库操作类 abstract class absdb { /* 参数 String $sql sql语句 返回 数组 */ abstract public function getAll($sql); /* 参数 String $sql sql语句 返回 一维数组 */ abstract public function getRow($sql); /* 参数 String $sql sql语句 返回 单个值 */ abstract public function g

微型php框架 include/init.php

<?php defined('ACC')||exit('Access Denied'); // 初始化脚本,专门负责环境的侦测与配置 // 0: 先探测自身所在的位置 /* 在win下,路径 D:\www\info 没有问题 D:/www/info 没有问题 在linux 路径 /var/www/info ,正斜线 因此:统一转成正斜线 ,用linux的格式 */ // 检测根目录,并定义成常量 define('ROOT',str_replace('include/init.php','',st

微型php框架 model/Model.php

<?php defined('ACC')||exit('Access Denied'); class Model { protected $db = null; public function __construct() { try { $this->db = mysql::getIns(); } catch(Exception $e) { // $e就是前文中 throw出来的 $error (Exception的实例) // $e对象有一些方法和属性,能准确判断出问题的行. $err =

Weed3 for java 新的微型ORM框架

Weed3,微型ORM框架(支持:java sql,xml sql,annotation sql:存储过程:事务:缓存:监听:等...) 05年时开发了第一代: 08年时开发了第二代,那时候进入互联网公司,对性能有了全新的认识: 14年时开发了第三代.因为不喜欢滥用反射,不喜欢有很多配置,所以一直在执着的没放弃. 前两代,都是在.net开发的:第三代,重点放在了java上.应该算是个功能全面且最小的ORM框架,无其它依赖,仅0.1mb.对外的接口也不多,主要由DbContext上的四个接口发起所