 {"id":198,"date":"2010-09-01T10:11:30","date_gmt":"2010-09-01T02:11:30","guid":{"rendered":"http:\/\/www.qyuef.com\/?p=198"},"modified":"2010-09-01T10:11:30","modified_gmt":"2010-09-01T02:11:30","slug":"%e5%88%86%e4%ba%ab%e4%b8%80%e4%b8%aa%e4%b8%8d%e9%94%99%e7%9a%84php%e5%9b%be%e7%89%87%e4%b8%8a%e4%bc%a0%e7%b1%bb","status":"publish","type":"post","link":"http:\/\/www.qyuef.com\/?p=198","title":{"rendered":"\u5206\u4eab\u4e00\u4e2a\u4e0d\u9519\u7684php\u56fe\u7247\u4e0a\u4f20\u7c7b"},"content":{"rendered":"<p>\u6700\u8fd1\u5728\u505a\u4e00\u4e2a\u4f5c\u54c1\u56fe\u7247\u6295\u7968\u7cfb\u7edf\uff0c\u6700\u4e3b\u8981\u7684\u529f\u80fd\u5c31\u662f\u4e0a\u4f20\u56fe\u7247\u8fd9\u4e00\u5757\u3002\u5728\u7f51\u4e0a\u627e\u4e86\u4e00\u4e0b\u76f8\u5173\u4ee3\u7801\u3002\u6700\u540e\u627e\u5230\u4e00\u4e2a\u6bd4\u8f83\u7b26\u5408\u8981\u6c42\u7684\u4e0a\u4f20\u7c7b\uff0c\u4e3a\u4e86\u6ee1\u8db3\u7cfb\u7edf\u7684\u8981\u6c42\u3002\u81ea\u5df1\u505a\u4e86\u4e00\u4e9b\u4fee\u6539\u3002\u8fd9\u91cc\u8d34\u51fa\u6765\u5206\u4eab\u4e00\u4e0b\uff1a \u4f7f\u7528\u65b9\u6cd5\uff1a<\/p>\n<pre escaped=\"true\" lang=\"php\" line=\"1\">\ninclude 'Upload.class.php';\n    $max=\"upload\";    \/\/\u6587\u4ef6\u4e0a\u4f20\u8def\u5f84\n    $mix=\"small\";       \/\/\u7f29\u7565\u56fe\u8def\u5f84\uff08\u5fc5\u987b\u5728upload\u4e0b\u5efa\u7acb\uff09\n    $mark=\"mark\";    \/\/\u52a0\u6c34\u5f15\u7684\u56fe\u7247\u5b58\u653e\u8def\u5f84\n    $text = array(\"oktang\",\"2012\");   \/\/\u6c34\u5370\u5185\u5bb9\n    $img= new UpImages($max,$mix,$max);   \/\/\u5b9e\u4f8b\u5316\u7c7b\u6587\u4ef6\n    $photo = $img->upLoad(\"file\");  \/\/\u4e0a\u4f20\u7684\u6587\u4ef6\u57df\n    $img->maxWidth = $img->maxHeight = 600; \/\/\u8bbe\u7f6e\u9ad8\uff0c\u548c\u5bbd\n    $img->toFile = true;\n    $newSmallImg = $img->smallImg($photo);  \n    $newMark = $img->waterMark($photo,$text);\n    echo $newSmallImg;\n    echo $newMark;\n    echo \"<img src='\".$newSmallImg.\"' border='0'\/><br \/><br \/>\";\n    echo \"<img src='\".$newMark.\"' border='0'\/><br \/><br \/>\";\n<\/pre>\n<p>\u6587\u4ef6\uff1aUpload.class.php<\/p>\n<pre escaped=\"true\" lang=\"php\" line=\"1\">\nclass UpImages {\n        var $annexFolder = \"upload\";\/\/\u9644\u4ef6\u5b58\u653e\u70b9\uff0c\u9ed8\u8ba4\u4e3a\uff1aannex\n        var $smallFolder = \"small\";\/\/\u7f29\u7565\u56fe\u5b58\u653e\u8def\u5f84\uff0c\u6ce8\uff1a\u5fc5\u987b\u662f\u653e\u5728 $annexFolder\u4e0b\u7684\u5b50\u76ee\u5f55\uff0c\u9ed8\u8ba4\n \n\u4e3a\uff1asmallimg\n        var $markFolder = \"mark\";\/\/\u6c34\u5370\u56fe\u7247\u5b58\u653e\u5904\n        var $upFileType = \"jpg gif png\";\/\/\u4e0a\u4f20\u7684\u7c7b\u578b\uff0c\u9ed8\u8ba4\u4e3a\uff1ajpg gif png rar zip\n        var $upFileMax = 1024;\/\/\u4e0a\u4f20\u5927\u5c0f\u9650\u5236\uff0c\u5355\u4f4d\u662f\u201cKB\u201d\uff0c\u9ed8\u8ba4\u4e3a\uff1a1024KB\n        var $fontType;\/\/\u5b57\u4f53\n        var $maxWidth = 500; \/\/\u56fe\u7247\u6700\u5927\u5bbd\u5ea6 \n        var $maxHeight = 600; \/\/\u56fe\u7247\u6700\u5927\u9ad8\u5ea6 \n        function UpImages($annexFolder,$smallFolder,$includeFolder) {\n                $this->annexFolder = $annexFolder;\n                $this->smallFolder = $smallFolder;\n                $this->fontType = $includeFolder.\"\/04B_08__.TTF\";\n        }\n        function upLoad($inputName) {\n                $imageName = time();\/\/\u8bbe\u5b9a\u5f53\u524d\u65f6\u95f4\u4e3a\u56fe\u7247\u540d\u79f0\n                if(@empty($_FILES[$inputName][\"name\"])) die(\"\u6ca1\u6709\u4e0a\u4f20\u56fe\u7247\u4fe1\u606f\uff0c\u8bf7\u786e\u8ba4\");\n                $name = explode(\".\",$_FILES[$inputName][\"name\"]);\/\/\u5c06\u4e0a\u4f20\u524d\u7684\u6587\u4ef6\u4ee5\u201c.\u201d\u5206\u5f00\u53d6\u5f97\n \n\u6587\u4ef6\u7c7b\u578b\n                $imgCount = count($name);\/\/\u83b7\u5f97\u622a\u53d6\u7684\u6570\u91cf\n                $imgType = $name[$imgCount-1];\/\/\u53d6\u5f97\u6587\u4ef6\u7684\u7c7b\u578b\n                if(strpos($this->upFileType,$imgType) === false) die(error(\"\u4e0a\u4f20\u6587\u4ef6\u7c7b\u578b\u4ec5\u652f\u6301 \n \n\".$this->upFileType.\" \u4e0d\u652f\u6301 \".$imgType));\n                $photo = $imageName.\".\".$imgType;\/\/\u5199\u5165\u6570\u636e\u5e93\u7684\u6587\u4ef6\u540d\n                $uploadFile = $this->annexFolder.\"\/\".$photo;\/\/\u4e0a\u4f20\u540e\u7684\u6587\u4ef6\u540d\u79f0\n                $upFileok = move_uploaded_file($_FILES[$inputName][\"tmp_name\"],$uploadFile);\n                if($upFileok) {\n                        $imgSize = $_FILES[$inputName][\"size\"];\n                        $kSize = round($imgSize\/1024);\n                        if($kSize > ($this->upFileMax*1024)) {\n                                @unlink($uploadFile);\n                                die(error(\"\u4e0a\u4f20\u6587\u4ef6\u8d85\u8fc7 \".$this->upFileMax.\"KB\"));\n                        }\n                } else {\n                        die(error(\"\u4e0a\u4f20\u56fe\u7247\u5931\u8d25\uff0c\u8bf7\u786e\u8ba4\u4f60\u7684\u4e0a\u4f20\u6587\u4ef6\u4e0d\u8d85\u8fc7 $upFileMax KB \u6216\u4e0a\u4f20\u65f6\n \n\u95f4\u8d85\u65f6\"));\n                }\n                return $photo;\n        }\n        function getInfo($photo) {\n                $photo = $this->annexFolder.\"\/\".$photo;\n                $imageInfo = getimagesize($photo);\n                $imgInfo[\"width\"] = $imageInfo[0];\n                $imgInfo[\"height\"] = $imageInfo[1];\n                $imgInfo[\"type\"] = $imageInfo[2];\n                $imgInfo[\"name\"] = basename($photo);\n                return $imgInfo;\n        }\n        function smallImg($photo,$width=128,$height=128) {\n                $imgInfo = $this->getInfo($photo);\n                $photo = $this->annexFolder.\"\/\".$photo;\/\/\u83b7\u5f97\u56fe\u7247\u6e90\n                $newName = substr($imgInfo[\"name\"],0,strrpos($imgInfo[\"name\"], \n \n\".\")).\"_thumb.jpg\";\/\/\u65b0\u56fe\u7247\u540d\u79f0\n                if($imgInfo[\"type\"] == 1) {\n                        $img = imagecreatefromgif($photo);\n                } elseif($imgInfo[\"type\"] == 2) {\n                        $img = imagecreatefromjpeg($photo);\n                } elseif($imgInfo[\"type\"] == 3) {\n                        $img = imagecreatefrompng($photo);\n                } else {\n                        $img = \"\";\n                }\n                if(empty($img)) return False;\n                $width = ($width > $imgInfo[\"width\"]) ? $imgInfo[\"width\"] : $width; \n                $height = ($height > $imgInfo[\"height\"]) ? $imgInfo[\"height\"] : $height; \n                $srcW = $imgInfo[\"width\"]; \n                $srcH = $imgInfo[\"height\"]; \n                if ($srcW * $width > $srcH * $height) {\n                        $height = round($srcH * $width \/ $srcW);\n                } else {\n                        $width = round($srcW * $height \/ $srcH);\n                }\n                if (function_exists(\"imagecreatetruecolor\")) {\n                        $newImg = imagecreatetruecolor($width, $height);\n                        ImageCopyResampled($newImg, $img, 0, 0, 0, 0, $width, $height, $imgInfo\n \n[\"width\"], $imgInfo[\"height\"]);\n                } else {\n                        $newImg = imagecreate($width, $height);\n                        ImageCopyResized($newImg, $img, 0, 0, 0, 0, $width, $height, $imgInfo\n \n[\"width\"], $imgInfo[\"height\"]);\n                }\n                if ($this->toFile) {\n                        if (file_exists($this->annexFolder.\"\/\".$this->smallFolder.\"\/\".$newName)) \n \n@unlink($this->annexFolder.\"\/\".$this->smallFolder.\"\/\".$newName);\n                        ImageJPEG($newImg,$this->annexFolder.\"\/\".$this-\n \n>smallFolder.\"\/\".$newName);\n                        return $this->annexFolder.\"\/\".$this->smallFolder.\"\/\".$newName;\n                } else {\n                        ImageJPEG($newImg);\n                }\n                ImageDestroy($newImg);\n                ImageDestroy($img);\n                return $newName;\n        }\n        function waterMark($photo,$text) {\n                $imgInfo = $this->getInfo($photo);\n                $photo = $this->annexFolder.\"\/\".$photo;\n                $newName = substr($imgInfo[\"name\"], 0, strrpos($imgInfo[\"name\"], \".\")) . \n \n\"_mark.jpg\";\n                switch ($imgInfo[\"type\"]) {\n                        case 1:\n                                $img = imagecreatefromgif($photo);\n                        break;\n                        case 2:\n                                $img = imagecreatefromjpeg($photo);\n                        break;\n                        case 3:\n                                $img = imagecreatefrompng($photo);\n                        break;\n                        default:\n                                return False;\n                }\n                if (empty($img)) return False;\n                $width = ($this->maxWidth > $imgInfo[\"width\"]) ? $imgInfo[\"width\"] : $this-\n \n>maxWidth; \n                $height = ($this->maxHeight > $imgInfo[\"height\"]) ? $imgInfo[\"height\"] : $this-\n \n>maxHeight; \n                $srcW = $imgInfo[\"width\"]; \n                $srcH = $imgInfo[\"height\"]; \n                if ($srcW * $width > $srcH * $height) {\n                        $height = round($srcH * $width \/ $srcW);\n                } else {\n                        $width = round($srcW * $height \/ $srcH);\n                }\n                if (function_exists(\"imagecreatetruecolor\")) {\n                        $newImg = imagecreatetruecolor($width, $height);\n                        ImageCopyResampled($newImg, $img, 0, 0, 0, 0, $width, $height, $imgInfo\n \n[\"width\"], $imgInfo[\"height\"]);\n                } else {\n                        $newImg = imagecreate($width, $height);\n                        ImageCopyResized($newImg, $img, 0, 0, 0, 0, $width, $height, $imgInfo\n \n[\"width\"], $imgInfo[\"height\"]);\n                }\n \n                $white = imageColorAllocate($newImg, 255, 255, 255);\n                $black = imageColorAllocate($newImg, 0, 0, 0);\n                $alpha = imageColorAllocateAlpha($newImg, 230, 230, 230, 40);\n                ImageFilledRectangle($newImg, 0, $height-26, $width, $height, $alpha);\n                ImageFilledRectangle($newImg, 13, $height-20, 15, $height-7, $black);\n                ImageTTFText($newImg, 4.9, 0, 20, $height-14, $black, $this->fontType, $text[0]);\n                ImageTTFText($newImg, 4.9, 0, 20, $height-6, $black, $this->fontType, $text[1]);\n                if($this->toFile) {\n                        if (file_exists($this->annexFolder.\"\/\".$this->markFolder.\"\/\".$newName)) \n \n@unlink($this->annexFolder.\"\/\".$this->markFolder.\"\/\".$newName);\n                        ImageJPEG($newImg,$this->annexFolder.\"\/\".$this->markFolder.\"\/\".$newName);\n                        return $this->annexFolder.\"\/\".$this->markFolder.\"\/\".$newName;\n                } else {\n                        ImageJPEG($newImg);\n                }\n                ImageDestroy($newImg);\n                ImageDestroy($img);\n                return $newName;\n        }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u5728\u505a\u4e00\u4e2a\u4f5c\u54c1\u56fe\u7247\u6295\u7968\u7cfb\u7edf\uff0c\u6700\u4e3b\u8981\u7684\u529f\u80fd\u5c31\u662f\u4e0a\u4f20\u56fe\u7247\u8fd9\u4e00\u5757\u3002\u5728\u7f51\u4e0a\u627e\u4e86\u4e00\u4e0b\u76f8\u5173\u4ee3\u7801\u3002\u6700\u540e\u627e\u5230\u4e00\u4e2a\u6bd4\u8f83\u7b26\u5408\u8981\u6c42\u7684\u4e0a\u4f20\u7c7b\uff0c\u4e3a\u4e86\u6ee1\u8db3\u7cfb\u7edf\u7684\u8981\u6c42\u3002\u81ea\u5df1\u505a\u4e86\u4e00\u4e9b\u4fee\u6539\u3002\u8fd9\u91cc\u8d34\u51fa\u6765\u5206\u4eab\u4e00\u4e0b\uff1a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[26,45],"tcat":[],"_links":{"self":[{"href":"http:\/\/www.qyuef.com\/index.php?rest_route=\/wp\/v2\/posts\/198"}],"collection":[{"href":"http:\/\/www.qyuef.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.qyuef.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.qyuef.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.qyuef.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=198"}],"version-history":[{"count":0,"href":"http:\/\/www.qyuef.com\/index.php?rest_route=\/wp\/v2\/posts\/198\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.qyuef.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.qyuef.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=198"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.qyuef.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=198"},{"taxonomy":"tcat","embeddable":true,"href":"http:\/\/www.qyuef.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftcat&post=198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}