Maison php教程 php手册 php图片上传并生成缩略图效果

php图片上传并生成缩略图效果

Jun 13, 2016 am 09:48 AM
php 上传 图片 Paquet 效果 教程 生成

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         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            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                $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;
}
?>

Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

Article chaud

Combien de temps faut-il pour battre Split Fiction?
3 Il y a quelques semaines By DDD
Repo: Comment relancer ses coéquipiers
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Comment obtenir des graines géantes
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
1 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌

Article chaud

Combien de temps faut-il pour battre Split Fiction?
3 Il y a quelques semaines By DDD
Repo: Comment relancer ses coéquipiers
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Comment obtenir des graines géantes
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
1 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌

Tags d'article chaud

Bloc-notes++7.3.1

Bloc-notes++7.3.1

Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise

SublimeText3 version chinoise

Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1

Envoyer Studio 13.0.1

Puissant environnement de développement intégré PHP

Dreamweaver CS6

Dreamweaver CS6

Outils de développement Web visuel

SublimeText3 version Mac

SublimeText3 version Mac

Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Guide d'installation et de mise à niveau de PHP 8.4 pour Ubuntu et Debian Guide d'installation et de mise à niveau de PHP 8.4 pour Ubuntu et Debian Dec 24, 2024 pm 04:42 PM

Guide d'installation et de mise à niveau de PHP 8.4 pour Ubuntu et Debian

Date et heure de CakePHP Date et heure de CakePHP Sep 10, 2024 pm 05:27 PM

Date et heure de CakePHP

Téléchargement de fichiers CakePHP Téléchargement de fichiers CakePHP Sep 10, 2024 pm 05:27 PM

Téléchargement de fichiers CakePHP

Routage CakePHP Routage CakePHP Sep 10, 2024 pm 05:25 PM

Routage CakePHP

Configuration du projet CakePHP Configuration du projet CakePHP Sep 10, 2024 pm 05:25 PM

Configuration du projet CakePHP

Discuter de CakePHP Discuter de CakePHP Sep 10, 2024 pm 05:28 PM

Discuter de CakePHP

Guide rapide CakePHP Guide rapide CakePHP Sep 10, 2024 pm 05:27 PM

Guide rapide CakePHP

Comment configurer Visual Studio Code (VS Code) pour le développement PHP Comment configurer Visual Studio Code (VS Code) pour le développement PHP Dec 20, 2024 am 11:31 AM

Comment configurer Visual Studio Code (VS Code) pour le développement PHP

See all articles