做了一个图片上传处理类,功能有图片的拉伸,缩小以及加入水印。时间有点仓促,整理花费了好多时间,各位大侠如果觉得还可以点个赞呗。不多说,直接上代码,注释不清晰的大侠们可以直接查PHP的文档。

原图
原图

缩小图
图片描述

放大图
图片描述

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<html>
<head>
    <title>ZwelL图片上传程序</title>
    <style type="text/css">
        <!--
        body {
            font-size: 9pt;
        }

        input {
            background-color: #66CCFF;
            border: 1px inset #CCCCCC;
        }

        -->
    </style>
</head>

<body>
<form enctype="multipart/form-data" method="post" name="upform">
    上传文件:
    <input name="upfile" type="file">
    <input type="submit" value="上传"><br>
</form>
</body>
</html>

PHP代码如下

<?php

class FileUploadUtil
{
    public $max_file_size = 2000000;     //上传文件大小限制, 单位BYTE
    public $destination_folder = "upload/"; //上传文件路径

    //水印
    public $is_water = 1;      //是否附加水印(1为加水印,其他为不加水印);
    public $water_type = 1;      //水印类型(1为文字,2为图片)
    public $water_string = "liaoyizhe";  //水印字符串

    //缩放
    public $is_resize = true; //是否缩放
    public $resize_width = 1000; //缩放宽度
    public $resize_height = 1000; //缩放高度
    //上传文件类型列表
    public $file_types = array(
        'image/jpg',
        'image/jpeg',
        'image/png',
        'image/pjpeg',
        'image/gif',
        'image/bmp',
        'image/x-png'
    );

    public function fileUpload()
    {
        if ($_SERVER['REQUEST_METHOD'] != 'POST') {
            return "ACTION必须为POST";
        }
        if (!is_uploaded_file($_FILES["upfile"]["tmp_name"])) {
            return "图片不存在!";
        }
        //[name] => 123.jpeg [type] => image/jpeg
        //[tmp_name] => /Applications/XAMPP/xamppfiles/temp/phpdsYVOn [error] => 0 [size] => 2890
        $file = $_FILES["upfile"];
        if ($this->max_file_size < $file["size"]) {
            return "文件太大!";
        }
        if (!in_array($file["type"], $this->file_types)) {
            return "文件类型不符!" . $file["type"];
        }
        if (!file_exists($this->destination_folder)) {
            mkdir($this->destination_folder);
        }
        $filename = $file["tmp_name"];
        //获得文件类型
        $p_info = pathinfo($file["name"]);
        $f_type = $p_info['extension'];
        $destination = $this->destination_folder . time() . "." . $f_type;
        if (file_exists($destination)) {
            return "同名文件已经存在了";
        }
        if (!move_uploaded_file($filename, $destination)) {
            return "移动文件出错";
        }
        //获得图片信息
        //Array ( [0] => 200 [1] => 200 [2] => 2 [3] => width="200" height="200" [bits] => 8 [channels] => 3 [mime] => image/jpeg )
        $image_info = getimagesize($destination);
        if ($this->is_water) {

            $this->waterMark($destination, $image_info);
        }
        switch ($image_info[2]) {
            case 2:
                $simage = imagecreatefromjpeg($destination);//创建新图像
                $this->resizeImage($simage, $this->resize_width, $this->resize_height, $destination, "jpeg");
                break;
            case 3:
                $simage = imagecreatefrompng($destination);
                $this->resizeImage($simage, $this->resize_width, $this->resize_height, $destination, "png");
                break;
            case 6:
                $simage = imagecreatefromwbmp($destination);
                $this->resizeImage($simage, $this->resize_width, $this->resize_height, $destination, "wbmp");
                break;
        }
//        $p_info = pathinfo($destination);
//        $f_name = $p_info["basename"];
//        $image_size = getimagesize($filename);
//        echo " 文件名:" . $this->destination_folder . $f_name . "</font><br>";
//        echo " 宽度:" . $image_size[0];
//        echo " 长度:" . $image_size[1];
//        echo "<br> 大小:" . $file["size"] . " bytes";
        return $destination;
    }

    private function waterMark($destination, $image_size)
    {
        $iinfo = getimagesize($destination, $iinfo);
        $nimage = imagecreatetruecolor($image_size[0], $image_size[1]);
        $white = imagecolorallocate($nimage, 255, 255, 255);
        $black = imagecolorallocate($nimage, 0, 0, 0);
        imagefill($nimage, 0, 0, $white);
        switch ($iinfo[2]) {
            case 1:
                $simage = imagecreatefromgif($destination);
                break;
            case 2:
                $simage = imagecreatefromjpeg($destination);
                break;
            case 3:
                $simage = imagecreatefrompng($destination);
                break;
            case 6:
                $simage = imagecreatefromwbmp($destination);
                break;
            default:
                die("不支持的文件类型");
        }

        imagecopy($nimage, $simage, 0, 0, 0, 0, $image_size[0], $image_size[1]);
        imagefilledrectangle($nimage, 1, $image_size[1] - 15, 80, $image_size[1], $white);

        switch ($this->water_type) {
            case 1:   //加水印字符串
                imagestring($nimage, 2, 3, $image_size[1] - 15, $this->water_string, $black);
                break;
            case 2:   //加水印图片
                $simage1 = imagecreatefromgif("xplore.gif");
                imagecopy($nimage, $simage1, 0, 0, 0, 0, 85, 15);
                imagedestroy($simage1);
                break;
        }

        switch ($iinfo[2]) {
            case 1:
                imagejpeg($nimage, $destination);
                break;
            case 2:
                imagejpeg($nimage, $destination);
                break;
            case 3:
                imagepng($nimage, $destination);
                break;
            case 6:
                imagewbmp($nimage, $destination);
                //imagejpeg($nimage, $destination);
                break;
        }

        //覆盖原上传文件
        imagedestroy($nimage);
        imagedestroy($simage);
    }

    private function resizeImage($im, $max_width, $max_height, $name, $file_type)
    {
        $pic_width = imagesx($im);//源图像宽度
        $pic_height = imagesy($im);//源图像高度
        $width_tag = false;
        $height_tag = false;
        $width_ratio = 0;
        $height_ratio = 0;
        $ratio = 0;
        if (($max_width && $pic_width > $max_width) || ($max_height && $pic_height > $max_height)) {
            if ($max_width && $pic_width > $max_width) {
                $width_ratio = $max_width / $pic_width;
                $width_tag = true;
            }
            if ($max_height && $pic_height > $max_height) {
                $height_ratio = $max_height / $pic_height;
                $height_tag = true;
            }
            if ($width_tag && $height_tag) {
                if ($width_ratio < $height_ratio)
                    $ratio = $width_ratio;
                else
                    $ratio = $height_ratio;
            }
            if ($width_tag && !$height_tag) {
                $ratio = $width_ratio;
            }

            if ($height_tag && !$width_tag) {
                $ratio = $height_ratio;
            }
            $new_width = $pic_width * $ratio;
            $new_height = $pic_height * $ratio;
            if (function_exists("imagecopyresampled")) {
                $new_im = imagecreatetruecolor($new_width, $new_height);//创建一个空图片资源
                imagecopyresampled($new_im, $im, 0, 0, 0, 0, $new_width, $new_height, $pic_width, $pic_height);//等比缩放
            } else {
                $new_im = imagecreate($new_width, $new_height);//创建一个空图片资源
                imagecopyresized($new_im, $im, 0, 0, 0, 0, $new_width, $new_height, $pic_width, $pic_height);
            }
            switch ($file_type) {
                case "jpeg":
                    imagejpeg($new_im, $name);
                    break;
                case "png":
                    imagepng($new_im, $name);
                    break;
                case "wbmp":
                    imagewbmp($new_im, $name);
                    break;
            }
            imagedestroy($new_im);
        } else {
            if (function_exists("imagecopyresampled")) {
                $new_im = imagecreatetruecolor($max_width, $max_height);//创建一个空图片资源
                imagecopyresampled($new_im, $im, 0, 0, 0, 0, $max_width, $max_height, $pic_width, $pic_height);//等比缩放
                echo 1111;
            } else {
                $new_im = imagecreate($max_width, $max_width);//创建一个空图片资源
                imagecopyresized($new_im, $im, 0, 0, 0, 0, $max_width, $max_height, $pic_width, $pic_height);
            }
            switch ($file_type) {
                case "jpeg":
                    imagejpeg($new_im, $name);
                    break;
                case "png":
                    imagepng($new_im, $name);
                    break;
                case "wbmp":
                    imagewbmp($new_im, $name);
                    break;
            }
            imagedestroy($new_im);
        }
    }
}

$f = new FileUploadUtil();
$destination = $f->fileUpload();
$image_size = getimagesize($destination);
echo " 文件名:" . $destination . "</font><br>";
echo " 宽度:" . $image_size[0];
echo " 长度:" . $image_size[1];
?>

liaoyizhe
434 声望24 粉丝

语言不再强势,路的尽头是汇编