php圖片上傳並生成縮略圖效果 本教程是一款php圖片上傳然後,把上傳的圖片生成小圖片哦,是一款非常好的文件上傳類,如果你正在找類程序可以進來看看
php教程圖片上傳並生成縮略圖效果
本教程是一款php圖片上傳然後,把上傳的圖片生成小圖片哦,是一款非常好的文件上傳類,如果你正在找類程序可以進來看看
*/
function uploadimage($upname,$smallmark=1,$dstsw,$dstsh=0,$path_dim,$path_xim,$newname,$smallname=0,$filetype="null") {
global $webaddr,$_files,$my;
$phpv=str_replace('.', '', php_version);
$filename=$upname;
$max_file_size = 2147483648; //上傳文件大小限制, 單位byte 2m
$path_im = $path_dim; //生成大圖保存文件夾路徑
$path_sim = $path_xim; //縮略圖保存文件夾路徑
$simclearly=75;
$simclearlypng =$phpv>=512?7:75; //縮略圖清晰度0-100,數字越大越清晰,文件尺寸越大
$smallmark = $smallmark; //是否生成縮略圖(1為加生成,其他為不);
$dst_sw =$dstsw; //定義縮略圖寬度,高度我采用等比例縮放,所以只要比較寬度就可以了
$uptypes=array(
'image/jpg',
'image/jpeg',
'image/png',
'image/pjpeg',
'image/gif',
'image/bmp',
'image/x-png'
);if (!is_uploaded_file($_files[$filename][tmp_name])) {
dsetcookie('setok','upload1');
header("location:bKjia.c0m/profile");
exit;
}
$file = $_files[$filename];
$pinfo = pathinfo($file["name"]);
if ($filetype=="null") {
$filetype = $pinfo['extension'];
}
if (!in_array(strtolower($pinfo['extension']),array("jpg","jpeg","png","gif"))) {
dsetcookie('setok','upload3');
header("location:bKjia.c0m/profile");
exit;
}if($max_file_size < $file["size"]) {//檢查文件大小
dsetcookie('setok','upload2');
header("location:bKjia.c0m/profile");
exit;
}
if(!in_array($file["type"],$uptypes)) { //檢查文件類型
dsetcookie('setok','upload3');
header("location:bKjia.c0m/profile");
exit;
}
if(!file_exists($path_im)) {
mkdir($path_im);
}$filename = $file["tmp_name"];
$im_size = getimagesize($filename);$src_w = $im_size[0];
$src_h = $im_size[1];
$src_type = $im_size[2];$all_path = $path_im.$newname.".".$filetype;//路徑+文件名,目前以上傳時間命名
if (file_exists($all_path)) {
@unlink($all_path);
}
if(!move_uploaded_file ($filename,$all_path)) {
dsetcookie('setok','upload4');
header("location:bKjia.c0m/profile");
exit;
}
$pinfo = pathinfo($all_path);
$fname = $pinfo[basename];switch($src_type) {//判斷源圖片文件類型
case 1://gif
$src_im = @imagecreatefromgif($all_path);//從源圖片文件取得圖像
break;
case 2://jpg
$src_im = @imagecreatefromjpeg($all_path);
break;
case 3://png
$src_im = @imagecreatefrompng($all_path);
break;
//case 6:
//$src_im=imagecreatefromwbmp($all_path);
//break;
default:
dsetcookie('setok','upload3');
header("location:bKjia.c0m/profile");
exit;
}if($smallmark == 1) {
if(!file_exists($path_sim)) {//檢查縮略圖目錄是否存在,不存在創建
mkdir($path_sim);
}
if ($smallname) $newname=$smallname;
$sall_path = $path_sim.$newname.".".$filetype;
if (file_exists($sall_path)) {
@unlink($sall_path);
}
if($src_w <= $dst_sw) { // 原圖尺寸 <= 縮略圖尺寸
if ($dstsh==0) {
$dst_sim = @imagecreatetruecolor($src_w,$src_h); //新建縮略圖真彩位圖
$sx=$sy=0;
} else {
$dst_sim = @imagecreatetruecolor($dstsw,$dstsh); //新建縮略圖真彩位圖
$sx=($dstsw-$src_w)/2;
$sy=($dstsh-$src_h)/2;
}
$img = @imagecreatefrompng("images/phbg.png");
@imagecopymerge($dst_sim,$img,0,0,0,0,$dstsw,$dstsh,100); //原圖圖像寫入新建真彩位圖中
@imagecopymerge($dst_sim,$src_im,$sx,$sy,0,0,$src_w,$src_h,100); //原圖圖像寫入新建真彩位圖中
}if($src_w > $dst_sw) { // 原圖尺寸 > 縮略圖尺寸
$dst_sh = $dst_sw/$src_w*$src_h;
if ($dst_sh<$dstsh) {
$dst_sh=$dstsh;
$dst_sw=$dst_sh/$src_h*$src_w;
}
if ($dstsh==0) {
$dst_sim = @imagecreatetruecolor($dst_sw,$dst_sh); //新建縮略圖真彩位圖(等比例縮小原圖尺寸)
} else {
$dst_sim = @imagecreatetruecolor($dstsw,$dstsh); //新建縮略圖真彩位圖(等比例縮小原圖尺寸)
}
@imagecopyresampled($dst_sim,$src_im,0,0,0,0,$dst_sw,$dst_sh,$src_w,$src_h); //原圖圖像寫入新建真彩位圖中
}switch($src_type) {
case 1:@imagegif($dst_sim,$sall_path,$simclearly);//生成gif文件,圖片清晰度0-100
break;
case 2:@imagejpeg($dst_sim,$sall_path,$simclearly);//生成jpg文件,圖片清晰度0-100
break;
case 3:@imagepng($dst_sim,$sall_path,$simclearlypng);//生成png文件,圖片清晰度0-100
break;
//case 6:
//imagewbmp($dst_sim,$sall_path);
break;
}
//釋放緩存
@imagedestroy($dst_sim);
}
@imagedestroy($src_im);
return $newname.".".$filetype;
}
?>