作了一個圖片上傳處理類,功能有圖片的拉伸,縮小以及加入水印。時間有點倉促,整理花費了好多時間,各位大俠若是以爲還能夠點個讚唄。很少說,直接上代碼,註釋不清晰的大俠們能夠直接查PHP的文檔。php
原圖
css
縮小圖
html
放大圖
post
<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代碼以下ui
<?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]; ?>