<?php
$dir = './img/'; // 源图片目录
$i = 10; // 开始的导入用户uid
$size = 'big'; // 生成的图片大小 设置大小 (small,middle,big)
$files1 = scandir($dir);
foreach($files1 as $value) {
if($value!='.' && $value!='..' && $value!='Thumbs.db') {
$newdir = get_avatar($i,$size);
copyimage($dir.$value,$newdir,$size);
$i++;
}
}
//echo $i;
function get_avatar($uid, $size = 'middle', $type = '') {
$size = in_array($size, array('big', 'middle', 'small')) ? $size : 'middle';
$uid = abs(intval($uid));
$uid = sprintf("%09d", $uid);
$dir1 = substr($uid, 0, 3);
$dir2 = substr($uid, 3, 2);
$dir3 = substr($uid, 5, 2);
$typeadd = $type == 'real' ? '_real' : '';
if(!is_dir($dir1)) {
mkdir($dir1);
}
if(!is_dir($dir1.'/'.$dir2)) {
mkdir($dir1.'/'.$dir2);
}
if(!is_dir($dir1.'/'.$dir2.'/'.$dir3)) {
mkdir($dir1.'/'.$dir2.'/'.$dir3);
}
return $dir1.'/'.$dir2.'/'.$dir3.'/'.substr($uid, -2).$typeadd."_avatar_$size.jpg";
}
function copyimage($upfile,$newfile,$size = 'middle') {
if($size == 'middle'){$px = 160;}
if($size == 'small'){$px = 48;}
if($size == 'big'){$px = 200;}
$image_path=$upfile;
$image_info=getimagesize($image_path);
if($image_info[2]==2) {
$image2=imagecreatefromjpeg($image_path);
}
$image2_x=imagesx($image2);
$image2_y=imagesy($image2);
if($image2_x > $px) {
$x = $px;
$y = intval($px*$image2_y/$image2_x);
} else {
$y = $image2_y;
$x = $image2_x;
}
$image1=imagecreatetruecolor($x,$y);
imagecopyresized($image1,$image2,0,0,0,0,$x,$y,$image2_x,$image2_y);
$Img = imagejpeg($image1,$newfile);
}
?>
来源:http://hi.baidu.com/cxh1984/blog/item/c32c870210a95f084bfb518e.html