本教程是一款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;
- $path_im = $path_dim;
- $path_sim = $path_xim;
- $simclearly=75;
- $simclearlypng =$phpv>=512?7:75;
- $smallmark = $smallmark;
- $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:111cn.net/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:111cn.net/profile");
- exit;
- }
-
- if($max_file_size < $file["size"]) {
- dsetcookie('setok','upload2');
- header("location:111cn.net/profile");
- exit;
- }
- if(!in_array($file["type"],$uptypes)) {
- dsetcookie('setok','upload3');
- header("location:111cn.net/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:111cn.net/profile");
- exit;
- }
- $pinfo = pathinfo($all_path);
- $fname = $pinfo[basename];
-
- switch($src_type) {
- case 1:
- $src_im = @imagecreatefromgif($all_path);
- break;
- case 2:
- $src_im = @imagecreatefromjpeg($all_path);
- break;
- case 3:
- $src_im = @imagecreatefrompng($all_path);
- break;
-
-
-
- default:
- dsetcookie('setok','upload3');
- header("location:111cn.net/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);
- break;
- case 2:@imagejpeg($dst_sim,$sall_path,$simclearly);
- break;
- case 3:@imagepng($dst_sim,$sall_path,$simclearlypng);
- break;
-
-
- break;
- }
-
- @imagedestroy($dst_sim);
- }
- @imagedestroy($src_im);
- return $newname.".".$filetype;
- }
|