Home > Backend Development > PHP Tutorial > Add a logo anywhere on the image

Add a logo anywhere on the image

WBOY
Release: 2016-07-28 08:26:19
Original
1557 people have browsed it

The principle of this is to ‘draw’ the logo at the designated position of the picture.
The steps are as follows:
1. Get basic information about pictures and logos
getimagesize

2. Determine the image resource type and create the corresponding image resource
getPicType (custom function)

3. Get the position coordinates of the logo
getLocal (custom function)

4. Draw the logo
imagecopyresampled

5. Output image
outputImage (custom function)

6. Release image resources
The effect of imagedestroy

is as shown below:
 向图片任意位置添加logo

The code is as follows:

<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>
Copy after login
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces how to add a logo to any position of the picture, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template