{"id":982,"date":"2021-06-07T15:31:37","date_gmt":"2021-06-07T07:31:37","guid":{"rendered":"http:\/\/blog.yyhcw.com\/?p=982"},"modified":"2021-06-07T15:31:37","modified_gmt":"2021-06-07T07:31:37","slug":"php%e5%b0%86%e4%ba%8c%e7%bb%b4%e7%a0%81%e5%8f%8a%e6%96%87%e5%ad%97%e6%b7%bb%e5%8a%a0%e5%88%b0%e6%8c%87%e5%ae%9a%e5%9b%be%e7%89%87","status":"publish","type":"post","link":"https:\/\/blog.yyhcw.com\/post\/982.html","title":{"rendered":"PHP\u5c06\u4e8c\u7ef4\u7801\u53ca\u6587\u5b57\u6dfb\u52a0\u5230\u6307\u5b9a\u56fe\u7247"},"content":{"rendered":"<!--wp-compress-html--><!--wp-compress-html no compression--><p>php\u901a\u8fc7\u7b2c\u4e09\u65b9\u7c7b\u53ef\u4ee5\u5f88\u5bb9\u6613\u7684\u5c06\u6307\u5b9a\u5185\u5bb9\u751f\u6210\u4e3a<a href=\"https:\/\/blog.yyhcw.com\/post\/tag\/%e4%ba%8c%e7%bb%b4%e7%a0%81\" class=\"tag-tooltip\" data-toggle=\"tooltip\" title=\"\u67e5\u770b\u66f4\u591a\u5173\u4e8e \u4e8c\u7ef4\u7801 \u7684\u6587\u7ae0\" target=\"_blank\">\u4e8c\u7ef4\u7801<\/a>\u56fe\u7247\uff0c\u90a3\u4e48\u5982\u4f55\u5c06\u751f\u6210\u7684\u4e8c\u7ef4\u7801\u56fe\u7247\u4e0e\u6307\u5b9a\u7684\u80cc\u666f\u56fe\u7247\u76f8\u7ed3\u5408\u5e76\u6dfb\u52a0\u6587\u5b57\u4fe1\u606f\u5462\uff1f<\/p>\n<p>\u8fd9\u91cc\u76f4\u63a5\u4e0a\u4ee3\u7801\uff1a<\/p>\n<pre class=\"lang:php decode:true\" title=\"PHP\u4e8c\u7ef4\u7801\u6216\u56fe\u7247\u6dfb\u52a0\u5230\u6307\u5b9a\u56fe\u7247\u4e0a\">$dstPath = 'weixin.png';\r\n$srcPath = 'qrcode.png';\r\n\/\/1.\u6dfb\u52a0\u4e8c\u7ef4\u7801\u5230\u56fe\u7247\r\n$tmpfile = tempnam(sys_get_temp_dir(), 'dedemao');\r\n$dstFile = picMerge($dstPath,$srcPath,165,225,0,0,100,$tmpfile);\r\n\r\n\/\/2.\u6dfb\u52a0\u6587\u5b57\u5230\u56fe\u7247\r\n$font = '.\/hei.ttf';        \/\/\u5b57\u4f53\r\n$fontSize = 26;\r\n$text = '\u7ec7\u68a6\u732b\u7f51\u9875\u8bbe\u8ba1\u5de5\u4f5c\u5ba4';\r\naddFontToPic($dstFile,$font,$fontSize,$text,700);\r\nunlink($tmpfile);\r\n\r\n\/**\r\n * <a href=\"https:\/\/blog.yyhcw.com\/post\/tag\/%e5%9b%be%e7%89%87%e5%90%88%e5%b9%b6\" class=\"tag-tooltip\" data-toggle=\"tooltip\" title=\"\u67e5\u770b\u66f4\u591a\u5173\u4e8e \u56fe\u7247\u5408\u5e76 \u7684\u6587\u7ae0\" target=\"_blank\">\u56fe\u7247\u5408\u5e76<\/a>\r\n * \u5c06\u6e90\u56fe\u7247\u8986\u76d6\u5230\u76ee\u6807\u56fe\u7247\u4e0a\r\n * @param string $dstPath \u76ee\u6807\u56fe\u7247\u8def\u5f84\r\n * @param string $srcPath \u6e90\u56fe\u7247\u8def\u5f84\r\n * @param int $dstX \u6e90\u56fe\u7247\u8986\u76d6\u5230\u76ee\u6807\u7684X\u8f74\u5750\u6807\r\n * @param int $dstY \u6e90\u56fe\u7247\u8986\u76d6\u5230\u76ee\u6807\u7684Y\u8f74\u5750\u6807\r\n * @param int $srcX\r\n * @param int $srcY\r\n * @param int $pct \u900f\u660e\u5ea6\r\n * @param string $filename \u8f93\u51fa\u7684\u6587\u4ef6\u540d\uff0c\u4e3a\u7a7a\u5219\u76f4\u63a5\u5728\u6d4f\u89c8\u5668\u4e0a\u8f93\u51fa\u663e\u793a\r\n * @return string $filename \u5408\u5e76\u540e\u7684\u6587\u4ef6\u540d\r\n *\/\r\nfunction picMerge($dstPath,$srcPath,$dstX=0,$dstY=0,$srcX=0,$srcY=0,$pct=100,$filename='')\r\n{\r\n    \/\/\u521b\u5efa\u56fe\u7247\u7684\u5b9e\u4f8b\r\n    $dst = imagecreatefromstring(file_get_contents($dstPath));\r\n    $src = imagecreatefromstring(file_get_contents($srcPath));\r\n    \/\/\u83b7\u53d6\u6c34\u5370\u56fe\u7247\u7684\u5bbd\u9ad8\r\n    list($src_w, $src_h) = getimagesize($srcPath);\r\n    \/\/\u5c06\u6c34\u5370\u56fe\u7247\u590d\u5236\u5230\u76ee\u6807\u56fe\u7247\u4e0a\uff0c\u6700\u540e\u4e2a\u53c2\u657050\u662f\u8bbe\u7f6e\u900f\u660e\u5ea6\uff0c\u8fd9\u91cc\u5b9e\u73b0\u534a\u900f\u660e\u6548\u679c\r\n    imagecopymerge($dst, $src, 165, 225, 0, 0, $src_w, $src_h, 100);\r\n    \/\/\u5982\u679c\u6c34\u5370\u56fe\u7247\u672c\u8eab\u5e26\u900f\u660e\u8272\uff0c\u5219\u4f7f\u7528imagecopy\u65b9\u6cd5\r\n    \/\/imagecopy($dst, $src, 10, 10, 0, 0, $src_w, $src_h);\r\n    \/\/\u8f93\u51fa\u56fe\u7247\r\n    list($dst_w, $dst_h, $dst_type) = getimagesize($dstPath);\r\n    switch ($dst_type) {\r\n        case 1:\/\/GIF\r\n            if(!$filename){\r\n                header('Content-Type: image\/gif');\r\n                imagegif($dst);\r\n            }else{\r\n                imagegif($dst,$filename);\r\n            }\r\n            break;\r\n        case 2:\/\/JPG\r\n            if(!$filename){\r\n                header('Content-Type: image\/jpeg');\r\n                imagejpeg($dst);\r\n            }else{\r\n                imagejpeg($dst,$filename);\r\n            }\r\n            break;\r\n        case 3:\/\/PNG\r\n            if(!$filename){\r\n                header('Content-Type: image\/png');\r\n                imagepng($dst);\r\n            }else{\r\n                imagepng($dst,$filename);\r\n            }\r\n            break;\r\n        default:\r\n            break;\r\n    }\r\n    imagedestroy($dst);\r\n    imagedestroy($src);\r\n    return $filename;\r\n}\r\n\r\n\/**\r\n * \u6dfb\u52a0\u6587\u5b57\u5230\u56fe\u7247\u4e0a\r\n * @param $dstPath \u76ee\u6807\u56fe\u7247\r\n * @param $fontPath \u5b57\u4f53\u8def\u5f84\r\n * @param $fontSize \u5b57\u4f53\u5927\u5c0f\r\n * @param $text \u6587\u5b57\u5185\u5bb9\r\n * @param $dstY \u6587\u5b57Y\u5750\u6807\u503c\r\n * @param string $filename \u8f93\u51fa\u6587\u4ef6\u540d\uff0c\u4e3a\u7a7a\u5219\u5728\u6d4f\u89c8\u5668\u4e0a\u76f4\u63a5\u8f93\u51fa\u663e\u793a\r\n * @return string \u8fd4\u56de\u6587\u4ef6\u540d\r\n *\/\r\nfunction addFontToPic($dstPath,$fontPath,$fontSize,$text,$dstY,$filename='')\r\n{\r\n    \/\/\u521b\u5efa\u56fe\u7247\u7684\u5b9e\u4f8b\r\n    $dst = imagecreatefromstring(file_get_contents($dstPath));\r\n    \/\/\u6253\u4e0a\u6587\u5b57\r\n    $fontColor = imagecolorallocate($dst, 0x00, 0x00, 0x00);\/\/\u5b57\u4f53\u989c\u8272\r\n    $width = imagesx ( $dst );\r\n    $height = imagesy ( $dst );\r\n    $fontBox = imagettfbbox($fontSize, 0, $fontPath, $text);\/\/\u6587\u5b57\u6c34\u5e73\u5c45\u4e2d\u5b9e\u8d28\r\n    imagettftext ( $dst, $fontSize, 0, ceil(($width - $fontBox[2]) \/ 2), $dstY, $fontColor, $fontPath, $text);\r\n    \/\/\u8f93\u51fa\u56fe\u7247\r\n    list($dst_w, $dst_h, $dst_type) = getimagesize($dstPath);\r\n    switch ($dst_type) {\r\n        case 1:\/\/GIF\r\n            if(!$filename){\r\n                header('Content-Type: image\/gif');\r\n                imagegif($dst);\r\n            }else{\r\n                imagegif($dst,$filename);\r\n            }\r\n            break;\r\n        case 2:\/\/JPG\r\n            if(!$filename){\r\n                header('Content-Type: image\/jpeg');\r\n                imagejpeg($dst);\r\n            }else{\r\n                imagejpeg($dst,$filename);\r\n            }\r\n            break;\r\n        case 3:\/\/PNG\r\n            if(!$filename){\r\n                header('Content-Type: image\/png');\r\n                imagepng($dst);\r\n            }else{\r\n                imagepng($dst,$filename);\r\n            }\r\n            break;\r\n        default:\r\n            break;\r\n    }\r\n    imagedestroy($dst);\r\n    return $filename;\r\n}<\/pre>\n<p>\u6700\u7ec8\u6548\u679c\u5982\u56fe\uff1a\u6536\u6b3e\u4e8c\u7ef4\u7801\u88ab\u6211\u5904\u7406\u4e86\u4e0b\uff1a<br \/>\n<img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-983\" src=\"http:\/\/blog.yyhcw.com\/wp-content\/uploads\/2021\/06\/bc83ced5aa2861a6.png\" alt=\"\" width=\"264\" height=\"332\" \/><\/p>\n<p>\u793a\u4f8b\u4ee3\u7801\u4e0b\u8f7d\uff1a<a href=\"http:\/\/file.dedemao.com\/code\/watermark.zip\">http:\/\/file.dedemao.com\/code\/watermark.zip<\/a><\/p>\n<!--wp-compress-html no compression--><!--wp-compress-html-->","protected":false},"excerpt":{"rendered":"php\u901a\u8fc7\u7b2c\u4e09\u65b9\u7c7b\u53ef\u4ee5\u5f88\u5bb9\u6613\u7684\u5c06\u6307\u5b9a\u5185\u5bb9\u751f\u6210\u4e3a\u4e8c\u7ef4\u7801\u56fe\u7247\uff0c\u90a3\u4e48\u5982\u4f55\u5c06\u751f\u6210\u7684\u4e8c\u7ef4\u7801\u56fe\u7247\u4e0e\u6307\u5b9a\u7684\u80cc\u666f\u56fe\u7247\u76f8\u7ed3\u5408\u5e76\u6dfb \u00b7\u00b7\u00b7","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[14,269,271,270],"views":3390,"_links":{"self":[{"href":"https:\/\/blog.yyhcw.com\/api\/wp\/v2\/posts\/982"}],"collection":[{"href":"https:\/\/blog.yyhcw.com\/api\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.yyhcw.com\/api\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.yyhcw.com\/api\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.yyhcw.com\/api\/wp\/v2\/comments?post=982"}],"version-history":[{"count":0,"href":"https:\/\/blog.yyhcw.com\/api\/wp\/v2\/posts\/982\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.yyhcw.com\/api\/wp\/v2\/media?parent=982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.yyhcw.com\/api\/wp\/v2\/categories?post=982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.yyhcw.com\/api\/wp\/v2\/tags?post=982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}