{"id":1268,"date":"2022-03-27T22:06:04","date_gmt":"2022-03-27T14:06:04","guid":{"rendered":"http:\/\/www.tra56.com\/?p=1268"},"modified":"2022-03-27T22:06:04","modified_gmt":"2022-03-27T14:06:04","slug":"1268","status":"publish","type":"post","link":"https:\/\/www.tra56.com\/1268.html","title":{"rendered":"PHP\u7ed9\u56fe\u7247\u52a0\u4e0a\u56fe\u7247\u6c34\u5370\u548c\u6587\u5b57\u6c34\u5370\u5b9e\u4f8b"},"content":{"rendered":"
function imageWaterMark($groundImage, $waterPos = 0, $waterImage = "", $waterText = "", $textFont = 5, $textColor = "#FF0000") {\r\n\r\n $isWaterImage = FALSE;\r\n\r\n $formatMsg = "\u6682\u4e0d\u652f\u6301\u8be5\u6587\u4ef6\u683c\u5f0f\uff0c\u8bf7\u7528\u56fe\u7247\u5904\u7406\u8f6f\u4ef6\u5c06\u56fe\u7247\u8f6c\u6362\u4e3aGIF\u3001JPG\u3001PNG\u683c\u5f0f\u3002";\r\n\r\n \/\/\u8bfb\u53d6\u6c34\u5370\u6587\u4ef6\r\n\r\n if (!empty($waterImage) && file_exists($waterImage)) {\r\n\r\n $isWaterImage = TRUE;\r\n\r\n $water_info = getimagesize($waterImage);\r\n\r\n $water_w = $water_info[0]; \/\/\u53d6\u5f97\u6c34\u5370\u56fe\u7247\u7684\u5bbd\r\n\r\n $water_h = $water_info[1]; \/\/\u53d6\u5f97\u6c34\u5370\u56fe\u7247\u7684\u9ad8\r\n\r\n switch ($water_info[2]) {\/\/\u53d6\u5f97\u6c34\u5370\u56fe\u7247\u7684\u683c\u5f0f\r\n case 1:$water_im = imagecreatefromgif($waterImage);\r\n break;\r\n\r\n case 2:$water_im = imagecreatefromjpeg($waterImage);\r\n break;\r\n\r\n case 3:$water_im = imagecreatefrompng($waterImage);\r\n break;\r\n\r\n default:die($formatMsg);\r\n }\r\n }\r\n\r\n \/\/\u8bfb\u53d6\u80cc\u666f\u56fe\u7247\r\n\r\n if (!empty($groundImage) && file_exists($groundImage)) {\r\n\r\n $ground_info = getimagesize($groundImage);\r\n\r\n $ground_w = $ground_info[0]; \/\/\u53d6\u5f97\u80cc\u666f\u56fe\u7247\u7684\u5bbd\r\n\r\n $ground_h = $ground_info[1]; \/\/\u53d6\u5f97\u80cc\u666f\u56fe\u7247\u7684\u9ad8\r\n\r\n switch ($ground_info[2]) {\/\/\u53d6\u5f97\u80cc\u666f\u56fe\u7247\u7684\u683c\u5f0f\r\n case 1:$ground_im = imagecreatefromgif($groundImage);\r\n break;\r\n\r\n case 2:$ground_im = imagecreatefromjpeg($groundImage);\r\n break;\r\n\r\n case 3:$ground_im = imagecreatefrompng($groundImage);\r\n break;\r\n\r\n default:die($formatMsg);\r\n }\r\n } else {\r\n\r\n die("\u9700\u8981\u52a0\u6c34\u5370\u7684\u56fe\u7247\u4e0d\u5b58\u5728\uff01");\r\n }\r\n\r\n \/\/\u6c34\u5370\u4f4d\u7f6e\r\n\r\n if ($isWaterImage) {\/\/\u56fe\u7247\u6c34\u5370\r\n $w = $water_w;\r\n\r\n $h = $water_h;\r\n\r\n $label = "\u56fe\u7247\u7684";\r\n } else {\/\/\u6587\u5b57\u6c34\u5370\r\n $temp = imagettfbbox(ceil($textFont * 5), 0, ".\/cour.ttf", $waterText); \/\/\u53d6\u5f97\u4f7f\u7528 TrueType \u5b57\u4f53\u7684\u6587\u672c\u7684\u8303\u56f4\r\n\r\n $w = $temp[2] - $temp[6];\r\n\r\n $h = $temp[3] - $temp[7];\r\n\r\n unset($temp);\r\n\r\n $label = "\u6587\u5b57\u533a\u57df";\r\n }\r\n\r\n if (($ground_w < $w) || ($ground_h < $h)) {\r\n\r\n echo "\u9700\u8981\u52a0\u6c34\u5370\u7684\u56fe\u7247\u7684\u957f\u5ea6\u6216\u5bbd\u5ea6\u6bd4\u6c34\u5370" . $label . "\u8fd8\u5c0f\uff0c\u65e0\u6cd5\u751f\u6210\u6c34\u5370\uff01";\r\n\r\n return;\r\n }\r\n\r\n switch ($waterPos) {\r\n\r\n case 0:\/\/\u968f\u673a\r\n\r\n $posX = rand(0, ($ground_w - $w));\r\n\r\n $posY = rand(0, ($ground_h - $h));\r\n\r\n break;\r\n\r\n case 1:\/\/1\u4e3a\u9876\u7aef\u5c45\u5de6\r\n\r\n $posX = 0;\r\n\r\n $posY = 0;\r\n\r\n break;\r\n\r\n case 2:\/\/2\u4e3a\u9876\u7aef\u5c45\u4e2d\r\n\r\n $posX = ($ground_w - $w) \/ 2;\r\n\r\n $posY = 0;\r\n\r\n break;\r\n\r\n case 3:\/\/3\u4e3a\u9876\u7aef\u5c45\u53f3\r\n\r\n $posX = $ground_w - $w;\r\n\r\n $posY = 0;\r\n\r\n break;\r\n\r\n case 4:\/\/4\u4e3a\u4e2d\u90e8\u5c45\u5de6\r\n\r\n $posX = 0;\r\n\r\n $posY = ($ground_h - $h) \/ 2;\r\n\r\n break;\r\n\r\n case 5:\/\/5\u4e3a\u4e2d\u90e8\u5c45\u4e2d\r\n\r\n $posX = ($ground_w - $w) \/ 2;\r\n\r\n $posY = ($ground_h - $h) \/ 2;\r\n\r\n break;\r\n\r\n case 6:\/\/6\u4e3a\u4e2d\u90e8\u5c45\u53f3\r\n\r\n $posX = $ground_w - $w;\r\n\r\n $posY = ($ground_h - $h) \/ 2;\r\n\r\n break;\r\n\r\n case 7:\/\/7\u4e3a\u5e95\u7aef\u5c45\u5de6\r\n\r\n $posX = 0;\r\n\r\n $posY = $ground_h - $h;\r\n\r\n break;\r\n\r\n case 8:\/\/8\u4e3a\u5e95\u7aef\u5c45\u4e2d\r\n\r\n $posX = ($ground_w - $w) \/ 2;\r\n\r\n $posY = $ground_h - $h;\r\n\r\n break;\r\n\r\n case 9:\/\/9\u4e3a\u5e95\u7aef\u5c45\u53f3\r\n\r\n $posX = $ground_w - $w - 10; \/\/ -10 \u662f\u8ddd\u79bb\u53f3\u4fa710px \u53ef\u4ee5\u81ea\u5df1\u8c03\u8282\r\n\r\n $posY = $ground_h - $h - 10; \/\/ -10 \u662f\u8ddd\u79bb\u5e95\u90e810px \u53ef\u4ee5\u81ea\u5df1\u8c03\u8282\r\n\r\n break;\r\n\r\n default:\/\/\u968f\u673a\r\n\r\n $posX = rand(0, ($ground_w - $w));\r\n\r\n $posY = rand(0, ($ground_h - $h));\r\n\r\n break;\r\n }\r\n\r\n \/\/\u8bbe\u5b9a\u56fe\u50cf\u7684\u6df7\u8272\u6a21\u5f0f\r\n\r\n imagealphablending($ground_im, true);\r\n\r\n if ($isWaterImage) {\/\/\u56fe\u7247\u6c34\u5370\r\n imagecopy($ground_im, $water_im, $posX, $posY, 0, 0, $water_w, $water_h); \/\/\u62f7\u8d1d\u6c34\u5370\u5230\u76ee\u6807\u6587\u4ef6\r\n } else {\/\/\u6587\u5b57\u6c34\u5370\r\n if (!emptyempty($textColor) && (strlen($textColor) == 7)) {\r\n\r\n $R = hexdec(substr($textColor, 1, 2));\r\n\r\n $G = hexdec(substr($textColor, 3, 2));\r\n\r\n $B = hexdec(substr($textColor, 5));\r\n } else {\r\n\r\n die("\u6c34\u5370\u6587\u5b57\u989c\u8272\u683c\u5f0f\u4e0d\u6b63\u786e\uff01");\r\n }\r\n\r\n imagestring($ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B));\r\n }\r\n\r\n \/\/\u751f\u6210\u6c34\u5370\u540e\u7684\u56fe\u7247\r\n\r\n @unlink($groundImage);\r\n\r\n switch ($ground_info[2]) {\/\/\u53d6\u5f97\u80cc\u666f\u56fe\u7247\u7684\u683c\u5f0f\r\n case 1:imagegif($ground_im, $groundImage);\r\n break;\r\n\r\n case 2:imagejpeg($ground_im, $groundImage);\r\n break;\r\n\r\n case 3:imagepng($ground_im, $groundImage);\r\n break;\r\n\r\n default:die($errorMsg);\r\n }\r\n\r\n \/\/\u91ca\u653e\u5185\u5b58\r\n\r\n if (isset($water_info))\r\n unset($water_info);\r\n\r\n if (isset($water_im))\r\n imagedestroy($water_im);\r\n\r\n unset($ground_info);\r\n\r\n imagedestroy($ground_im);\r\n}<\/pre>\n\u6587\u5b57\u6c34\u5370\uff1a<\/span><\/p>\n
waterText('banner.jpg', 9, '','sucaihuo.com'); \/\/\u80cc\u666f\u56fe\u7247,$waterPos\u6c34\u5370\u4f4d\u7f6e,\u6c34\u5370\u56fe\u7247\u5730\u5740<\/pre>\n\u56fe\u7247\u6c34\u5370\uff1a<\/span><\/span><\/p>\n
imageWaterMark('404.jpg', 9, 'logo.gif'); \/\/\u80cc\u666f\u56fe\u7247,$waterPos\u6c34\u5370\u4f4d\u7f6e,\u6c34\u5370\u56fe\u7247\u5730\u5740<\/pre>\n\u6ce8\u610f\uff1a$waterImage \u548c $waterText \u6700\u597d\u4e0d\u8981\u540c\u65f6\u4f7f\u7528\uff0c\u9009\u5176\u4e2d\u4e4b\u4e00\u5373\u53ef\uff0c\u4f18\u5148\u4f7f\u7528 $waterImage\u3002<\/span><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"
function imageWaterMark($groundImage, $waterP […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,92],"tags":[],"class_list":["post-1268","post","type-post","status-publish","format-standard","hentry","category-php","category-phpcode"],"_links":{"self":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/1268"}],"collection":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/comments?post=1268"}],"version-history":[{"count":1,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/1268\/revisions"}],"predecessor-version":[{"id":1269,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/1268\/revisions\/1269"}],"wp:attachment":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/media?parent=1268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/categories?post=1268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/tags?post=1268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}