그림의 지정된 위치에 로고를 '그리는' 것이 원리입니다.
단계는 다음과 같습니다.
1. 사진과 로고에 대한 기본 정보 얻기
getimagesize
2. 이미지 리소스 유형을 결정하고 해당 이미지 리소스를 생성합니다
getPicType(맞춤 함수)
3. 로고의 위치 좌표를 가져옵니다
getLocal(맞춤 함수)
4. 로고 그리기
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>
위 내용은 내용을 포함해 사진의 어느 위치에나 로고를 추가하는 방법을 소개하고 있으니 PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되길 바랍니다.