{"id":922,"date":"2025-12-24T10:54:01","date_gmt":"2025-12-24T02:54:01","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=922"},"modified":"2025-12-24T10:54:01","modified_gmt":"2025-12-24T02:54:01","slug":"%e8%a7%a3%e6%9e%90php-%e4%b8%ad-gd-%e5%ba%93%e7%9a%84%e4%bd%bf%e7%94%a8%e6%8a%80%e5%b7%a7%e4%b8%8e%e5%9b%be%e5%83%8f%e5%a4%84%e7%90%86%e6%a1%88%e4%be%8b","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/12\/24\/%e8%a7%a3%e6%9e%90php-%e4%b8%ad-gd-%e5%ba%93%e7%9a%84%e4%bd%bf%e7%94%a8%e6%8a%80%e5%b7%a7%e4%b8%8e%e5%9b%be%e5%83%8f%e5%a4%84%e7%90%86%e6%a1%88%e4%be%8b\/","title":{"rendered":"\u89e3\u6790PHP \u4e2d GD \u5e93\u7684\u4f7f\u7528\u6280\u5de7\u4e0e\u56fe\u50cf\u5904\u7406\u6848\u4f8b"},"content":{"rendered":"\n<p>\u597d\u7684\uff0c\u6211\u6765\u7ed9\u4f60\u6574\u7406\u4e00\u4efd <strong>PHP GD \u5e93\u4f7f\u7528\u6280\u5de7\u4e0e\u5b9e\u7528\u56fe\u50cf\u5904\u7406\u6848\u4f8b<\/strong>\uff0c\u4ece\u57fa\u7840\u6982\u5ff5\u3001\u5e38\u7528\u51fd\u6570\uff0c\u5230\u5b9e\u9645\u6848\u4f8b\uff0c\u65b9\u4fbf\u76f4\u63a5\u4e0a\u624b\u3002\u5185\u5bb9\u5c3d\u91cf\u6761\u7406\u5316\uff0c\u9002\u5408\u53c2\u8003\u4e0e\u5feb\u901f\u5f00\u53d1\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">PHP GD \u5e93\u4f7f\u7528\u6280\u5de7\u4e0e\u56fe\u50cf\u5904\u7406\u6848\u4f8b<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">1\ufe0f\u20e3 GD \u5e93\u7b80\u4ecb<\/h2>\n\n\n\n<p>GD \u5e93\u662f PHP \u5185\u7f6e\u7684\u56fe\u50cf\u5904\u7406\u6269\u5c55\uff0c\u7528\u4e8e\u751f\u6210\u3001\u4fee\u6539\u6216\u8f93\u51fa\u56fe\u7247\u3002\u652f\u6301\u591a\u79cd\u56fe\u50cf\u683c\u5f0f\uff0c\u5982 JPEG\u3001PNG\u3001GIF\u3001WebP \u7b49\u3002<br>\u5e38\u7528\u573a\u666f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u751f\u6210\u9a8c\u8bc1\u7801<\/li>\n\n\n\n<li>\u7f29\u653e\u6216\u88c1\u526a\u56fe\u7247<\/li>\n\n\n\n<li>\u6dfb\u52a0\u6587\u5b57\u6c34\u5370<\/li>\n\n\n\n<li>\u52a8\u6001\u56fe\u8868\u6216\u6d77\u62a5\u751f\u6210<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u6ce8\u610f\uff1a\u4f7f\u7528\u524d\u9700\u786e\u4fdd GD \u5e93\u5df2\u5b89\u88c5\uff0c\u53ef\u901a\u8fc7 <code>phpinfo()<\/code> \u67e5\u770b\u3002<\/p>\n<\/blockquote>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;?php\nif (function_exists(&#039;gd_info&#039;)) {\n    echo &quot;GD \u5df2\u5b89\u88c5&quot;;\n} else {\n    echo &quot;GD \u672a\u5b89\u88c5&quot;;\n}\n?&gt;\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2\ufe0f\u20e3 \u5e38\u7528 GD \u51fd\u6570\u5206\u7c7b<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u521b\u5efa\/\u8bfb\u53d6\u56fe\u7247<\/strong><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/ \u521b\u5efa\u7a7a\u767d\u753b\u5e03\n$image = imagecreatetruecolor(200, 100);  \n\n\/\/ \u4ece\u6587\u4ef6\u8bfb\u53d6\n$image = imagecreatefromjpeg(&#039;test.jpg&#039;);\n$image = imagecreatefrompng(&#039;test.png&#039;);\n$image = imagecreatefromgif(&#039;test.gif&#039;);\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>\u989c\u8272\u76f8\u5173<\/strong><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/ \u5206\u914d\u989c\u8272\n$white = imagecolorallocate($image, 255, 255, 255);\n$red   = imagecolorallocate($image, 255, 0, 0);\n$blue  = imagecolorallocate($image, 0, 0, 255);\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>\u7ed8\u56fe<\/strong><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/ \u7ed8\u5236\u77e9\u5f62\nimagerectangle($image, 10, 10, 190, 90, $red);\n\n\/\/ \u7ed8\u5236\u7ebf\u6761\nimageline($image, 0, 0, 200, 100, $blue);\n\n\/\/ \u7ed8\u5236\u5706\nimageellipse($image, 100, 50, 80, 80, $white);\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>\u6587\u5b57\u64cd\u4f5c<\/strong><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/ \u7cfb\u7edf\u5b57\u4f53\nimagestring($image, 5, 50, 30, &#039;Hello GD&#039;, $white);\n\n\/\/ TTF \u5b57\u4f53\n$font = __DIR__.&#039;\/msyh.ttf&#039;; \/\/ \u5b57\u4f53\u6587\u4ef6\nimagettftext($image, 20, 0, 50, 50, $red, $font, &#039;GD \u5b57\u4f53\u6587\u5b57&#039;);\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>\u56fe\u50cf\u8f93\u51fa\u4e0e\u4fdd\u5b58<\/strong><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nheader(&#039;Content-Type: image\/png&#039;);\nimagepng($image);            \/\/ \u8f93\u51fa\u5230\u6d4f\u89c8\u5668\nimagejpeg($image, &#039;out.jpg&#039;); \/\/ \u4fdd\u5b58\u5230\u6587\u4ef6\nimagedestroy($image);        \/\/ \u9500\u6bc1\u8d44\u6e90\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3\ufe0f\u20e3 \u5b9e\u7528\u6280\u5de7<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4fdd\u6301\u900f\u660e\u80cc\u666f<\/strong><\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimagealphablending($image, false);\nimagesavealpha($image, true);\n\n<\/pre><\/div>\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>\u7f29\u653e\u56fe\u7247<\/strong><\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n$new = imagecreatetruecolor($new_width, $new_height);\nimagecopyresampled($new, $original, 0,0,0,0, $new_width, $new_height, $orig_width, $orig_height);\n\n<\/pre><\/div>\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>\u88c1\u526a\u56fe\u7247<\/strong><\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n$crop = imagecrop($original, &#x5B;&#039;x&#039;=&gt;50,&#039;y&#039;=&gt;50,&#039;width&#039;=&gt;100,&#039;height&#039;=&gt;100]);\n\n<\/pre><\/div>\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><strong>\u6dfb\u52a0\u6c34\u5370<\/strong><\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n$stamp = imagecreatefrompng(&#039;watermark.png&#039;);\nimagecopy($original, $stamp, 10, 10, 0, 0, imagesx($stamp), imagesy($stamp));\n\n<\/pre><\/div>\n\n\n<ol start=\"5\" class=\"wp-block-list\">\n<li><strong>\u52a8\u6001\u9a8c\u8bc1\u7801\u751f\u6210<\/strong><\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n$image = imagecreatetruecolor(120, 40);\n$bg = imagecolorallocate($image, 255, 255, 255);\nimagefill($image, 0, 0, $bg);\n\n$code = substr(str_shuffle(&#039;ABCDEFGHJKLMNPQRSTUVWXYZ23456789&#039;),0,5);\nfor($i=0;$i&amp;lt;5;$i++){\n    $color = imagecolorallocate($image, rand(0,150), rand(0,150), rand(0,150));\n    imagestring($image, 5, 20*$i+5, 10, $code&#x5B;$i], $color);\n}\n\nheader(&#039;Content-Type: image\/png&#039;);\nimagepng($image);\nimagedestroy($image);\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4\ufe0f\u20e3 \u56fe\u50cf\u5904\u7406\u6848\u4f8b<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u6848\u4f8b 1\uff1a\u751f\u6210\u7f29\u7565\u56fe<\/strong><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfunction createThumbnail($src, $dest, $width, $height){\n    $info = getimagesize($src);\n    switch($info&#x5B;2]){\n        case IMAGETYPE_JPEG: $img = imagecreatefromjpeg($src); break;\n        case IMAGETYPE_PNG:  $img = imagecreatefrompng($src); break;\n        case IMAGETYPE_GIF:  $img = imagecreatefromgif($src); break;\n        default: return false;\n    }\n    $thumb = imagecreatetruecolor($width, $height);\n    imagecopyresampled($thumb, $img, 0,0,0,0, $width, $height, $info&#x5B;0], $info&#x5B;1]);\n    imagejpeg($thumb, $dest);\n    imagedestroy($img);\n    imagedestroy($thumb);\n}\n\ncreateThumbnail(&#039;big.jpg&#039;, &#039;thumb.jpg&#039;, 150, 150);\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>\u6848\u4f8b 2\uff1a\u7ed9\u56fe\u7247\u6dfb\u52a0\u6587\u5b57\u6c34\u5370<\/strong><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n$image = imagecreatefromjpeg(&#039;photo.jpg&#039;);\n$color = imagecolorallocatealpha($image, 255, 255, 255, 50); \/\/ \u534a\u900f\u660e\n$font = __DIR__.&#039;\/msyh.ttf&#039;;\nimagettftext($image, 20, 0, 10, 30, $color, $font, &#039;\u00a9\u7248\u6743&#039;);\n\nimagejpeg($image, &#039;watermarked.jpg&#039;);\nimagedestroy($image);\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>\u6848\u4f8b 3\uff1a\u52a8\u6001\u62fc\u63a5\u6d77\u62a5<\/strong><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n$bg = imagecreatefromjpeg(&#039;bg.jpg&#039;);\n$avatar = imagecreatefromjpeg(&#039;user.jpg&#039;);\n\n$avatar_new = imagecreatetruecolor(100,100);\nimagecopyresampled($avatar_new, $avatar, 0,0,0,0,100,100,imagesx($avatar), imagesy($avatar));\n\nimagecopy($bg, $avatar_new, 50, 50, 0,0, 100,100);\n\nheader(&#039;Content-Type: image\/jpeg&#039;);\nimagejpeg($bg);\nimagedestroy($bg);\nimagedestroy($avatar_new);\nimagedestroy($avatar);\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5\ufe0f\u20e3 \u603b\u7ed3\u4e0e\u6280\u5de7<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f18\u5148\u4f7f\u7528 <code>imagecreatetruecolor<\/code><\/strong> \u4fdd\u8bc1 24 \u4f4d\u8272\u5f69\u548c\u6297\u952f\u9f7f\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528 <code>imagecopyresampled<\/code><\/strong> \u4ee3\u66ff <code>imagecopyresized<\/code>\uff0c\u4fdd\u8bc1\u7f29\u653e\u8d28\u91cf\u3002<\/li>\n\n\n\n<li><strong>\u6ce8\u610f\u5185\u5b58\u7ba1\u7406<\/strong>\uff1aGD \u4f1a\u5360\u7528\u5927\u91cf\u5185\u5b58\uff0c\u5904\u7406\u5927\u56fe\u65f6\u9700\u8981 <code>ini_set('memory_limit', '512M');<\/code>\u3002<\/li>\n\n\n\n<li><strong>PNG \u548c GIF \u900f\u660e\u5904\u7406<\/strong>\uff1a\u52a1\u5fc5\u4f7f\u7528 <code>imagealphablending<\/code> \u548c <code>imagesavealpha<\/code>\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u597d\u7684\uff0c\u6211\u6765\u7ed9\u4f60\u6574\u7406\u4e00\u4efd PHP GD \u5e93\u4f7f\u7528\u6280\u5de7\u4e0e\u5b9e\u7528\u56fe\u50cf\u5904\u7406\u6848\u4f8b\uff0c\u4ece\u57fa\u7840\u6982\u5ff5\u3001&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/12\/24\/%e8%a7%a3%e6%9e%90php-%e4%b8%ad-gd-%e5%ba%93%e7%9a%84%e4%bd%bf%e7%94%a8%e6%8a%80%e5%b7%a7%e4%b8%8e%e5%9b%be%e5%83%8f%e5%a4%84%e7%90%86%e6%a1%88%e4%be%8b\/\">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-922","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\/922","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=922"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/922\/revisions"}],"predecessor-version":[{"id":923,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/922\/revisions\/923"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=922"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=922"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=922"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}