{"id":1262,"date":"2026-01-06T16:16:22","date_gmt":"2026-01-06T08:16:22","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=1262"},"modified":"2026-01-06T16:16:22","modified_gmt":"2026-01-06T08:16:22","slug":"php%e5%ae%9e%e7%8e%b0%e6%b7%bb%e5%8a%a0%e5%9b%be%e7%89%87%e6%b0%b4%e5%8d%b0%e7%9a%84%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2026\/01\/06\/php%e5%ae%9e%e7%8e%b0%e6%b7%bb%e5%8a%a0%e5%9b%be%e7%89%87%e6%b0%b4%e5%8d%b0%e7%9a%84%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a0%81\/","title":{"rendered":"PHP\u5b9e\u73b0\u6dfb\u52a0\u56fe\u7247\u6c34\u5370\u7684\u793a\u4f8b\u4ee3\u7801"},"content":{"rendered":"\n<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u4efd<strong>\u53ef\u76f4\u63a5\u8fd0\u884c\u3001\u57fa\u4e8e PHP GD \u5e93\u7684\u300c\u56fe\u7247\u6c34\u5370\u300d\u5b8c\u6574\u793a\u4f8b<\/strong>\uff0c\u5305\u542b<br>\u2714 \u56fe\u7247\u6c34\u5370<br>\u2714 \u900f\u660e\u5ea6\u63a7\u5236<br>\u2714 \u591a\u683c\u5f0f\u652f\u6301<br>\u2714 \u5e38\u89c1\u5751\u8bf4\u660e<\/p>\n\n\n\n<p>\u975e\u5e38\u9002\u5408\u535a\u5ba2\u3001CMS\u3001\u4e0a\u4f20\u7cfb\u7edf\u4f7f\u7528\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e00\u3001\u6548\u679c\u8bf4\u660e<\/h1>\n\n\n\n<p>\u5728\u539f\u56fe\u53f3\u4e0b\u89d2\u6dfb\u52a0\u4e00\u5f20 <strong>PNG \u6c34\u5370\u56fe\uff08\u652f\u6301\u900f\u660e\uff09<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e8c\u3001\u73af\u5883\u8981\u6c42<\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li>PHP \u2265 7.0<\/li>\n\n\n\n<li>\u5f00\u542f <strong>GD \u6269\u5c55<\/strong><\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nphp -m | grep gd\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e09\u3001\u5b8c\u6574\u793a\u4f8b\u4ee3\u7801\uff08\u56fe\u7247\u6c34\u5370\uff09<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;?php\n\n\/**\n * \u7ed9\u56fe\u7247\u6dfb\u52a0\u56fe\u7247\u6c34\u5370\n * @param string $src \u539f\u56fe\u8def\u5f84\n * @param string $water \u6c34\u5370\u56fe\u8def\u5f84\uff08PNG\u63a8\u8350\uff09\n * @param string $savePath \u4fdd\u5b58\u8def\u5f84\uff08\u4e3a\u7a7a\u5219\u8986\u76d6\u539f\u56fe\uff09\n * @param int $alpha \u6c34\u5370\u900f\u660e\u5ea6 0-100\n *\/\nfunction addImageWatermark($src, $water, $savePath = &#039;&#039;, $alpha = 60)\n{\n    if (!file_exists($src) || !file_exists($water)) {\n        return false;\n    }\n\n    \/\/ \u83b7\u53d6\u539f\u56fe\u4fe1\u606f\n    $srcInfo = getimagesize($src);\n    $waterInfo = getimagesize($water);\n\n    list($srcW, $srcH, $srcType) = $srcInfo;\n    list($waterW, $waterH) = $waterInfo;\n\n    \/\/ \u521b\u5efa\u539f\u56fe\u8d44\u6e90\n    switch ($srcType) {\n        case IMAGETYPE_JPEG:\n            $srcImg = imagecreatefromjpeg($src);\n            break;\n        case IMAGETYPE_PNG:\n            $srcImg = imagecreatefrompng($src);\n            break;\n        case IMAGETYPE_GIF:\n            $srcImg = imagecreatefromgif($src);\n            break;\n        default:\n            return false;\n    }\n\n    \/\/ \u521b\u5efa\u6c34\u5370\u8d44\u6e90\n    $waterImg = imagecreatefrompng($water);\n\n    \/\/ \u6c34\u5370\u4f4d\u7f6e\uff08\u53f3\u4e0b\u89d2\uff0c\u7559 10px \u8fb9\u8ddd\uff09\n    $dstX = $srcW - $waterW - 10;\n    $dstY = $srcH - $waterH - 10;\n\n    \/\/ \u5408\u5e76\u6c34\u5370\uff08\u652f\u6301\u900f\u660e\uff09\n    imagecopymerge(\n        $srcImg,\n        $waterImg,\n        $dstX,\n        $dstY,\n        0,\n        0,\n        $waterW,\n        $waterH,\n        $alpha\n    );\n\n    \/\/ \u4fdd\u5b58\u56fe\u7247\n    $savePath = $savePath ?: $src;\n\n    switch ($srcType) {\n        case IMAGETYPE_JPEG:\n            imagejpeg($srcImg, $savePath, 90);\n            break;\n        case IMAGETYPE_PNG:\n            imagepng($srcImg, $savePath);\n            break;\n        case IMAGETYPE_GIF:\n            imagegif($srcImg, $savePath);\n            break;\n    }\n\n    \/\/ \u91ca\u653e\u8d44\u6e90\n    imagedestroy($srcImg);\n    imagedestroy($waterImg);\n\n    return true;\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u56db\u3001\u4f7f\u7528\u793a\u4f8b<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n$srcImage   = &#039;.\/upload\/photo.jpg&#039;;\n$waterImage = &#039;.\/watermark\/logo.png&#039;;\n\naddImageWatermark($srcImage, $waterImage, &#039;.\/upload\/photo_water.jpg&#039;, 70);\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e94\u3001\u652f\u6301\u7684\u6c34\u5370\u4f4d\u7f6e\u6269\u5c55<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/ \u5de6\u4e0a\n$dstX = 10;\n$dstY = 10;\n\n\/\/ \u5de6\u4e0b\n$dstX = 10;\n$dstY = $srcH - $waterH - 10;\n\n\/\/ \u53f3\u4e0a\n$dstX = $srcW - $waterW - 10;\n$dstY = 10;\n\n\/\/ \u5c45\u4e2d\n$dstX = ($srcW - $waterW) \/ 2;\n$dstY = ($srcH - $waterH) \/ 2;\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u516d\u3001PNG \u6c34\u5370\u900f\u660e\u5ea6\u95ee\u9898\uff08\u91cd\u70b9\uff09<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">\u2757 imagecopymerge \u7684\u5751<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5bf9 <strong>PNG \u539f\u751f\u900f\u660e\u652f\u6301\u4e0d\u597d<\/strong><\/li>\n\n\n\n<li>\u4f1a\u5bfc\u81f4\u6c34\u5370\u53d8\u7070<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 \u89e3\u51b3\u65b9\u6848\uff08\u63a8\u8350\uff09<\/h3>\n\n\n\n<p>\u4f7f\u7528 <code>imagecopy<\/code> + \u4fdd\u7559 Alpha\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimagealphablending($srcImg, true);\nimagesavealpha($srcImg, true);\n\nimagecopy(\n    $srcImg,\n    $waterImg,\n    $dstX,\n    $dstY,\n    0,\n    0,\n    $waterW,\n    $waterH\n);\n\n<\/pre><\/div>\n\n\n<p>\uff08\u6b64\u65b9\u5f0f\u4f7f\u7528 PNG \u81ea\u5e26\u900f\u660e\u5ea6\uff09<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e03\u3001\u5e38\u89c1\u95ee\u9898\u6c47\u603b<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">\u2753 \u4e3a\u4ec0\u4e48\u6c34\u5370\u6a21\u7cca\uff1f<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6c34\u5370\u56fe\u7247\u672c\u8eab\u5206\u8fa8\u7387\u4f4e<\/li>\n\n\n\n<li>JPEG \u538b\u7f29\u8d28\u91cf\u8fc7\u4f4e<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2753 GIF \u52a8\u56fe\u80fd\u52a0\u6c34\u5370\u5417\uff1f<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u274c GD \u4e0d\u652f\u6301\u52a8\u56fe\u9010\u5e27<\/li>\n\n\n\n<li>\u2705 \u9700 Imagick<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2753 \u56fe\u7247\u592a\u5c0f\u600e\u4e48\u529e\uff1f<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nif ($srcW &amp;lt; $waterW || $srcH &amp;lt; $waterH) {\n    return false;\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u516b\u3001Imagick\uff08\u66f4\u9ad8\u7ea7\uff0c\u53ef\u9009\uff09<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n$img = new Imagick(&#039;photo.jpg&#039;);\n$water = new Imagick(&#039;logo.png&#039;);\n\n$img-&gt;compositeImage($water, Imagick::COMPOSITE_OVER, 20, 20);\n$img-&gt;writeImage(&#039;out.jpg&#039;);\n\n<\/pre><\/div>\n\n\n<p>\u2714 \u652f\u6301 GIF<br>\u2714 \u66f4\u597d\u900f\u660e\u5904\u7406<br>\u274c \u6269\u5c55\u5b89\u88c5\u590d\u6742<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e5d\u3001\u4e00\u53e5\u8bdd\u603b\u7ed3<\/h1>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>GD \u591f\u7528\uff0cImagick \u66f4\u5f3a\uff1bPNG \u6c34\u5370\u6700\u4f73\uff1b\u6ce8\u610f\u900f\u660e\u5ea6\u5904\u7406<\/strong><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u4efd\u53ef\u76f4\u63a5\u8fd0\u884c\u3001\u57fa\u4e8e PHP GD \u5e93\u7684\u300c\u56fe\u7247\u6c34\u5370\u300d\u5b8c\u6574\u793a\u4f8b\uff0c\u5305\u542b\u2714 \u56fe&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2026\/01\/06\/php%e5%ae%9e%e7%8e%b0%e6%b7%bb%e5%8a%a0%e5%9b%be%e7%89%87%e6%b0%b4%e5%8d%b0%e7%9a%84%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a0%81\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-1262","post","type-post","status-publish","format-standard","hentry","category-12"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1262","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=1262"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1262\/revisions"}],"predecessor-version":[{"id":1263,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1262\/revisions\/1263"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=1262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=1262"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=1262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}