这个的原理是把logo‘画’图片的指定位置.
步骤如下:
1.获取图片和logo的基本信息
getimagesize
2.判断图片资源类型并创建对应图片资源
getPicType (自定义函数)
3.获取logo的位置坐标
getLocal (自定义函数)
4.把logo画上去
imagecopyresampled
5.输出图像
outputImage (自定义函数)
6.释放图片资源
imagedestroy
效果如图:
代码如下:
<code><span>/** *<span> @function</span> 向图像中添加logo *<span> @param</span> $picname *<span> @param</span> $logo *<span> @param</span> int $local logo添加的位置:1:左上角,2:右上角,3:左下角,4:右下角,5:中上,6:中间,7:中下 * <span> @param</span> string $pre *<span> @return</span> 返回一个新的图片名 */</span><span><span>function</span><span>addLogo</span><span>(<span>$picname</span>,<span>$logo</span>,<span>$local</span>=<span>"1"</span>,<span>$pre</span>=<span>"l_"</span>)</span> {</span><span>$picInfo</span> = getimagesize(<span>$picname</span>); <span>//获取图片的基本信息</span><span>$logoInfo</span> = getimagesize(<span>$logo</span>); <span>//获取logo的基本信息</span><span>$picW</span> = <span>$picInfo</span>[<span>0</span>];<span>//获取pic宽度</span><span>$picH</span> = <span>$picInfo</span>[<span>1</span>];<span>//获取pic高度</span><span>$logoW</span> = <span>$logoInfo</span>[<span>0</span>]; <span>//获取logo宽度</span><span>$logoH</span> = <span>$logoInfo</span>[<span>1</span>]; <span>//获取logo高度</span><span>//获取图片资源类型并创建对应图片资源</span><span>$picim</span> = getPicType(<span>$picInfo</span>[<span>2</span>],<span>$picname</span>); <span>$logoim</span> = getPicType(<span>$logoInfo</span>[<span>2</span>],<span>$logo</span>); <span>//把logo画上去</span><span>$XY</span> = getLocal(<span>$local</span>,<span>$picW</span>,<span>$picH</span>,<span>$logoW</span>,<span>$logoH</span>);<span>//获取logo的位置坐标</span> imagecopyresampled(<span>$picim</span>,<span>$logoim</span>,<span>$XY</span>[<span>0</span>],<span>$XY</span>[<span>1</span>],<span>0</span>,<span>0</span>,<span>$logoW</span>,<span>$logoH</span>,<span>$logoW</span>,<span>$logoH</span>); <span>//输出图像</span><span>$newPicName</span> = outputImage(<span>$picname</span>,<span>$pre</span>,<span>$picim</span>); <span>//释放图片资源</span> imagedestroy(<span>$picim</span>); imagedestroy(<span>$logoim</span>); <span>return</span><span>$newPicName</span>; } <span>/** *<span> @function</span> 获取对应位置的左上角坐标 *<span> @param</span> $local 位置 *<span> @param</span> $picW 图片宽 *<span> @param</span> $picH 图片高 *<span> @param</span> $logoW logo宽 *<span> @param</span> $logoH logo高 *<span> @return</span> 返回宽高数组 */</span><span><span>function</span><span>getLocal</span><span>(<span>$local</span>,<span>$picW</span>,<span>$picH</span>,<span>$logoW</span>,<span>$logoH</span>)</span> {</span><span>$XY</span> = <span>array</span>(); <span>switch</span>(<span>$local</span>) { <span>case</span><span>1</span>: <span>$XY</span> = <span>array</span>(<span>0</span>,<span>0</span>); <span>break</span>; <span>case</span><span>2</span>: <span>$XY</span> = <span>array</span>(<span>$picW</span>-<span>$logoW</span>,<span>0</span>); <span>break</span>; <span>case</span><span>3</span>: <span>$XY</span> = <span>array</span>(<span>0</span>,<span>$picH</span>-<span>$logoH</span>); <span>break</span>; <span>case</span><span>4</span>: <span>$XY</span> = <span>array</span>(<span>$picW</span>-<span>$logoW</span>,<span>$picH</span>-<span>$logoH</span>); <span>break</span>; <span>case</span><span>5</span>: <span>$XY</span> = <span>array</span>(<span>$picW</span>/<span>2</span> - <span>$logoW</span>/<span>2</span>,<span>0</span>); <span>break</span>; <span>case</span><span>6</span>: <span>$XY</span> = <span>array</span>(<span>$picW</span>/<span>2</span> - <span>$logoW</span>/<span>2</span>,<span>$picH</span>/<span>2</span> - <span>$logoH</span>/<span>2</span>); <span>break</span>; <span>case</span><span>7</span>: <span>$XY</span> = <span>array</span>(<span>$picW</span>/<span>2</span> - <span>$logoW</span>/<span>2</span>,<span>$picH</span>-<span>$logoH</span>); <span>break</span>; <span>default</span>: <span>die</span>(<span>"不支持这个位置的logo添加"</span>); <span>break</span>; } <span>return</span><span>$XY</span>; } <span>/** * function 判断并返回图片的类型(以资源方式返回) *<span> @param</span> int $type 图片类型 *<span> @param</span> string $picname 图片名字 *<span> @return</span> 返回对应图片资源 */</span><span><span>function</span><span>getPicType</span><span>(<span>$type</span>,<span>$picname</span>)</span> {</span><span>$im</span>=<span>null</span>; <span>switch</span>(<span>$type</span>) { <span>case</span><span>1</span>: <span>//GIF</span><span>$im</span> = imagecreatefromgif(<span>$picname</span>); <span>break</span>; <span>case</span><span>2</span>: <span>//JPG</span><span>$im</span> = imagecreatefromjpeg(<span>$picname</span>); <span>break</span>; <span>case</span><span>3</span>: <span>//PNG</span><span>$im</span> = imagecreatefrompng(<span>$picname</span>); <span>break</span>; <span>case</span><span>4</span>: <span>//BMP</span><span>$im</span> = imagecreatefromwbmp(<span>$picname</span>); <span>break</span>; <span>default</span>: <span>die</span>(<span>"不认识图片类型"</span>); <span>break</span>; } <span>return</span><span>$im</span>; } <span>/** * function 输出图像 *<span> @param</span> string $picname 图片名字 *<span> @param</span> string $pre 新图片名前缀 *<span> @param</span> resourse $nim 要输出的图像资源 *<span> @return</span> 返回新的图片名 */</span><span><span>function</span><span>outputImage</span><span>(<span>$picname</span>,<span>$pre</span>,<span>$nim</span>)</span> {</span><span>$info</span> = getimagesize(<span>$picname</span>); <span>$picInfo</span> = pathInfo(<span>$picname</span>); <span>$newPicName</span> = <span>$picInfo</span>[<span>'dirname'</span>].<span>'/'</span>.<span>$pre</span>.<span>$picInfo</span>[<span>'basename'</span>];<span>//输出文件的路径</span><span>switch</span>(<span>$info</span>[<span>2</span>]) { <span>case</span><span>1</span>: imagegif(<span>$nim</span>,<span>$newPicName</span>); <span>break</span>; <span>case</span><span>2</span>: imagejpeg(<span>$nim</span>,<span>$newPicName</span>); <span>break</span>; <span>case</span><span>3</span>: imagepng(<span>$nim</span>,<span>$newPicName</span>); <span>break</span>; <span>case</span><span>4</span>: imagewbmp(<span>$nim</span>,<span>$newPicName</span>); <span>break</span>; } <span>return</span><span>$newPicName</span>; }</code>
以上就介绍了 向图片任意位置添加logo,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。