PHP图片上传并处理类(伸缩and水印)

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

原图
PHP图片上传并处理类(伸缩and水印)_第1张图片

缩小图
PHP图片上传并处理类(伸缩and水印)_第2张图片

放大图
PHP图片上传并处理类(伸缩and水印)_第3张图片




    ZwelL图片上传程序
    



上传文件:

PHP代码如下

 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 . "
"; // echo " 宽度:" . $image_size[0]; // echo " 长度:" . $image_size[1]; // echo "
大小:" . $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 . "
"; echo " 宽度:" . $image_size[0]; echo " 长度:" . $image_size[1]; ?>

你可能感兴趣的:(php)