🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 第九课 图片处理生成 ``` <pre class="calibre18">``` <span class="token6">// 1.设置背景图片路径</span> $src <span class="token">=</span> <span class="token4">"1.png"</span><span class="token2">;</span> <span class="token6">// 2.获取图片基本信息</span> $info <span class="token">=</span> <span class="token1">getimagesize</span><span class="token2">(</span>$src<span class="token2">)</span><span class="token2">;</span> <span class="token6">// 3.获取图片类型</span> $type <span class="token">=</span> <span class="token1">image_type_to_extension</span><span class="token2">(</span>$info<span class="token2">[</span><span class="token3">2</span><span class="token2">]</span><span class="token2">,</span> <span class="token3">false</span><span class="token2">)</span><span class="token2">;</span> <span class="token6">// 4.函数---在内存中创建图片的副本</span> $fun <span class="token">=</span> <span class="token4">"imagecreatefrom{$type}"</span><span class="token2">;</span> <span class="token6">// 5.在内存中创建图片的副本</span> $image <span class="token">=</span> $<span class="token1">fun</span><span class="token2">(</span>$src<span class="token2">)</span><span class="token2">;</span> <span class="token6">/* 操作图片 */</span> <span class="token6">// 1.设置水印图片路径</span> $src_water <span class="token">=</span> <span class="token4">"2.jpg"</span><span class="token2">;</span> <span class="token6">// 2.获取水印图片信息</span> $info_water <span class="token">=</span> <span class="token1">getimagesize</span><span class="token2">(</span>$src_water<span class="token2">)</span><span class="token2">;</span> <span class="token6">// 3.获取水印图片类型</span> $type_water <span class="token">=</span> <span class="token1">image_type_to_extension</span><span class="token2">(</span>$info_water <span class="token2">[</span><span class="token3">2</span><span class="token2">]</span><span class="token2">,</span> <span class="token3">false</span><span class="token2">)</span><span class="token2">;</span> <span class="token6">// 4.函数---在内存中创建图片的副本</span> $fun_water <span class="token">=</span> <span class="token4">"imagecreatefrom{$type_water}"</span><span class="token2">;</span> <span class="token6">// 5.在内存中创建图片的副本</span> $image_water <span class="token">=</span> $<span class="token1">fun_water</span><span class="token2">(</span>$src_water<span class="token2">)</span><span class="token2">;</span> <span class="token6">// 6.合并图片,生成最终水印图片 100不透明 0透明</span> <span class="token1">imagecopymerge</span><span class="token2">(</span>$image<span class="token2">,</span> $image_water<span class="token2">,</span> <span class="token3">175</span><span class="token2">,</span> <span class="token3">375</span><span class="token2">,</span> <span class="token3">0</span><span class="token2">,</span> <span class="token3">0</span><span class="token2">,</span> $info_water<span class="token2">[</span><span class="token3">0</span><span class="token2">]</span><span class="token2">,</span> $info_water<span class="token2">[</span><span class="token3">1</span><span class="token2">]</span><span class="token2">,</span> <span class="token3">100</span><span class="token2">)</span><span class="token2">;</span> <span class="token6">//文字水印</span> $font <span class="token">=</span> <span class="token4">"fz.ttf"</span><span class="token2">;</span> $content <span class="token">=</span> <span class="token4">"Yoby,你好,欢迎关注我们哦"</span><span class="token2">;</span> $col <span class="token">=</span> <span class="token1">imagecolorallocate</span><span class="token2">(</span>$image<span class="token2">,</span> <span class="token3">125</span><span class="token2">,</span> <span class="token3">255</span><span class="token2">,</span> <span class="token3">255</span><span class="token2">)</span><span class="token2">;</span> <span class="token6">//20号字体 50,300 是水平垂直坐标</span> <span class="token1">imagettftext</span><span class="token2">(</span>$image<span class="token2">,</span> <span class="token3">20</span><span class="token2">,</span> <span class="token3">0</span><span class="token2">,</span> <span class="token3">50</span><span class="token2">,</span> <span class="token3">300</span><span class="token2">,</span> $col<span class="token2">,</span> $font<span class="token2">,</span> $content<span class="token2">)</span><span class="token2">;</span> <span class="token6">/* 输出图片 */</span> <span class="token6">// 在浏览器中输出图片</span> <span class="token1">header</span><span class="token2">(</span><span class="token4">"Content-type:"</span><span class="token2">.</span>$info<span class="token2">[</span><span class="token4">"mime"</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> $func <span class="token">=</span> <span class="token4">"image{$type}"</span><span class="token2">;</span> $<span class="token1">func</span><span class="token2">(</span>$image<span class="token2">)</span><span class="token2">;</span> <span class="token6">// 保存图片</span> $<span class="token1">func</span><span class="token2">(</span>$image<span class="token2">,</span> <span class="token4">'11.'</span><span class="token2">.</span>$type<span class="token2">)</span><span class="token2">;</span> <span class="token6">/* 销毁图片 */</span> <span class="token1">imagedestroy</span><span class="token2">(</span>$image<span class="token2">)</span><span class="token2">;</span> ``` ```