本文件可以用于自动创建目录下所有JPG图片的缩略图,放到图片目录下运行即可。
缺点:长宽不一的图片会被拉伸变形,不能智能裁切,需要智能裁切的,请自行研究。
$config = array();
$config['path'] = “./“;
$config['t_width'] = 120;
$config['t_height'] = 98;
$config['ignore'] = array(“”,“.“,“..“);
$config['prefix'] = “thumb_“;
$done = 0;
define(“IMAGE_JPG“, 2);
define(“ENDL“, “\n“);
if($handle = opendir($config['path'])) {
while(false !== ($file = readdir($handle))) {
if(!array_search($file,$config['ignore'])) {
list($im_width, $im_height, $type) = getimagesize($file);
if($type != IMAGE_JPG) {
continue;
}
$op .= “found -> <a href=’{$file}‘>$file</a>“ . ENDL;
$im = @imagecreatefromjpeg($file);
if(!$im) {
$op .= “fail -> couldn’t create sour image pointer.“ . ENDL;
continue;
}
if(file_exists($config['prefix'] . $file) || substr($file, 0, strlen($config['prefix'])) == $config['prefix']) {
$op .= “note -> this file has already got a thumbnail.“ . ENDL;
continue;
}
$to = imagecreatetruecolor($config['t_width'],$config['t_height']);
if(!$to) {
$op .= “fail -> couldn’t create dest image pointer.“ . ENDL;
continue;
}
if(!imagecopyresampled($to, $im, 0, 0, 0, 0, $config['t_width'], $config['t_height'], $im_width, $im_height)) {
$op .= “fail -> couldn’t create thumbnail. php fail.“ . ENDL;
continue;
}
//保存文件
imagejpeg($to, $config['prefix'] . $file);
$op .= “done -> created thumb: <a href=’{$config['prefix']}{$file}’>{$config['prefix']}{$file}</a>“ . ENDL;
$done++;
}
}
}
closedir($handle);
$op .= “fin -> {$done} file(s) written“ . ENDL;
echo “<pre>“;
echo $op;
echo “</pre>“;
exit;
?>
今天我找了喜悦国际村里所有的分页函数和类,没有一个简单好用点的.
有位大虾发的那个仿GOOGLE的我拿来试了,页码老是少1,调了半天不好用,还是自己写吧.
高手有兴趣可以帮忙改进下.方法一看就比较笨的 ![]()
不过尽管方法笨,但用起来自我感觉还是挺好的.
这个函数只需要2个参数 当前页码和总页数,$site是路径,文件名可以按自己的需要修改
这个函数不能传递其他参数,如果要传递其他参数,在函数里自己加一个参数就行了
<?php
//计数
$sql = “Select count(*) FROM `andycms_title`;”;
$numrs = mysql_query($sql) or die(mysql_error());
$numrow = mysql_fetch_row($numrs);
$num = $numrow[0]; //总记录
$total = ceil($num / $perpage); //总页数
//当前页码
if(!isset($_GET['page']) || preg_match(‘~[^0-9]+~’,($_GET['page'])) || $_GET['page'] <= 0 || $_GET['page'] > $total) $page = 1;
else $page = $_GET['page'];
$startnum = ($page-1) * $perpage; //记录集开始数
?>
<?php
// Andy分页函数
function fenye ($p,$total)
{
global $site;
$prevs = $p - 10; if ( $prevs <= 0) { $prevs = 1; }
$prev = $prevs - 1; if ( $prev <= 0) {$prev = 1;}
$nexts = $p + 9; if ( $nexts > $total) { $nexts = $total; }
$next = $nexts + 1; if ( $next > $total) {$next = $total;}
$pagenavi = “<a href=\”$site/?page=1\”>首页</a> “;
$pagenavi.= “<a href=\”$site/?page=$prev\”>上页</a> “;
for ( $i = $prevs; $i <= $p-1; $i++ ) {
$pagenavi.= “<a href=\”$site/?page=$i\”>$i</a> “;
}
$pagenavi.= “<strong>$p</strong> “;
for ( $i = $p+1; $i <= $nexts; $i++ ) {
$pagenavi.= “<a href=\”$site/?page=$i\”>$i</a> “;
}
$pagenavi.= “<a href=\”$site/?page=$next\”>下页</a> “;
$pagenavi.= “<a href=\”$site/?page=$total\”>尾页</a> “;
return $pagenavi;
}
?>
效果:
page=1
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 尾页
page=10
首页 上页 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 下页 尾页
page=20
首页 上页 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 下页 尾页
另外,可以用一下这个函数,Discuz类型的
<?php
// 分页函数
function multi($num, $perpage, $curr_page, $mpurl) {
$multipage = ”;
if($num > $perpage) {
$page = 10;
$offset = 2;
$pages = ceil($num / $perpage);
$from = $curr_page - $offset;
$to = $curr_page + $page - $offset - 1;
if($page > $pages) {
$from = 1;
$to = $pages;
} else {
if($from < 1) {
$to = $curr_page + 1 - $from;
$from = 1;
if(($to - $from) < $page && ($to - $from) < $pages) {
$to = $page;
}
} elseif($to > $pages) {
$from = $curr_page - $pages + $to;
$to = $pages;
if(($to - $from) < $page && ($to - $from) < $pages) {
$from = $pages - $page + 1;
}
}
}
$multipage .= “<a href=\”$mpurl?page=1\”>首页</a> “;
for($i = $from; $i <= $to; $i++) {
if($i != $curr_page) {
$multipage .= “<a href=\”$mpurl?page=$i\”>$i</a> “;
} else {
$multipage .= ‘<strong>’.$i.‘</strong> ‘;
}
}
$multipage .= $pages > $page ? ” … <a href=\”$mpurl?page=$pages\”>尾页</a>” : ” <a href=\”$mpurl&page=$pages\”>首页</a>”;
}
return $multipage;
}
?>
效果:
page=1
首页 1 2 3 4 5 6 7 8 9 10 … 尾页
page=10
首页 8 9 10 11 12 13 14 15 16 17 … 尾页
page=20
首页 18 19 20 21 22 23 24 25 26 27 … 尾页
—–
mt_rand() 比rand() 快四倍
很多老的 libc 的随机数发生器具有一些不确定和未知的特性而且很慢。PHP 的 rand() 函数默认使用 libc
随机数发生器。mt_rand() 函数是非正式用来替换它的。该函数用了 Mersenne Twister
中已知的特性作为随机数发生器,mt_rand() 可以产生随机数值的平均速度比 libc 提供的 rand() 快四倍。 —–
以下是手册中关于PHP变量的指针的例子,理解一下.
注意其中几行加粗的,变量的变化.
In
response to the example by mdiricks. Extending the example given by
mdiricks, the following code provides an explains the concept of
re-referencing that is involved in making a call to function foo with
the prototype foo(& var):
<!– C re-referenced –>
<?$a = ‘eh’;
$b = & $a; // $b == ‘eh’
$c = & $b; // $c == ‘eh’
$d = ‘meh’;echo “\$a = $a\n”; //$a = eh
echo “\$b = $b\n”; //$b = eh
echo “\$c = $c\n”; //$c = eh
echo “\$d = $d\n”; //$d = meh$c = & $d ;// $c == ‘meh’
echo “\n”;echo “\$a = $a\n”; //$a = eh
echo “\$b = $b\n”; //$b = eh
echo “\$c = $c\n”; //$c = meh
echo “\$d = $d\n”; //$d = meh?>
<!– Value of c changed –>
<?$a = ‘eh’;
$b = & $a;// $b == ‘eh’
$c = & $b;// $c == ‘eh’
$d = ‘meh’;echo “\$a = $a\n”; //$a = eh
echo “\$b = $b\n”; //$b = eh
echo “\$c = $c\n”; //$c = eh
echo “\$d = $d\n”; //$d = meh$c = ‘meh’ ;// $c == ‘meh’. And also, $a = $b == ‘meh’
echo “\n”;echo “\$a = $a\n”; //$a = meh
echo “\$b = $b\n”; //$b = meh
echo “\$c = $c\n”; //$c = meh
echo “\$d = $d\n”; //$d = meh?>
This results in the following o/p:
<!– C re-referenced –>
$a = eh
$b = eh
$c = eh
$d = meh$a = eh
$b = eh
$c = meh
$d = meh<!– Value of c changed –>
$a = eh
$b = eh
$c = eh
$d = meh$a = meh
$b = meh
$c = meh
$d = meh