>  기사  >  백엔드 개발  >  PHP 사진에 텍스트를 쓰는 방법

PHP 사진에 텍스트를 쓰는 방법

藏色散人
藏色散人원래의
2019-09-28 09:12:495014검색

PHP 사진에 텍스트를 쓰는 방법

php 사진에 텍스트를 쓰는 것은 사진에 텍스트 워터마크를 추가하는 것과 같습니다. 워터마크는 일반적으로 텍스트 또는 사진 로고 워터마크입니다. 추가하는 방법.

1. 텍스트 워터마크

Text 워터마크는 이미지에 텍스트를 추가하는 작업으로 주로 gd 라이브러리의 imagefttext 방식을 사용하며 필요합니다. 글꼴 파일.

$dst_path = 'dst.jpg';
//创建图片的实例
$dst = imagecreatefromstring(file_get_contents($dst_path));
//打上文字
$font = './simsun.ttc';//字体路径
$black = imagecolorallocate($dst, 0x00, 0x00, 0x00);//字体颜色
imagefttext($dst, 13, 0, 20, 20, $black, $font, '快乐编程');
//输出图片
list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path);
switch ($dst_type) {
    case 1://GIF
        header('Content-Type: image/gif');
        imagegif($dst);
        break;
    case 2://JPG
        header('Content-Type: image/jpeg');
        imagejpeg($dst);
        break;
    case 3://PNG
        header('Content-Type: image/png');
        imagepng($dst);
        break;
    default:
        break;
}
imagedestroy($dst);

새 사진을 만든 다음 텍스트 워터마크를 인쇄하세요.

// imagecreatefromstring
// imageCreateFromPng  Create a new image from file or URL   创建图片对象
// Create a 300x100 image,新创建一张图片
$im = imagecreatetruecolor(500, 300);
// set color
$red = imagecolorallocate($im, 0xFF, 0x00, 0x00);
$black = imagecolorallocate($im, 0x00, 0x00, 0x00);
// Make the background red
// function imagefilledrectangle ($image, $x1, $y1, $x2, $y2, $color) {}
imagefilledrectangle($im, 0, 0, 300, 100, $red);
// Path to our ttf font file
$font_file = './font/Arial.ttf';
// imagefttext ($image, $size, $angle, $x, $y, $color, $fontfile, $text, $extrainfo = null )
// Draw the text 'PHP Manual' using font size 13
imagefttext($im, 13, 0, 150, 50, $black, $font_file, 'PHP Manual');
// Output image to the browser
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
clipboard.png

렌더링은 다음과 같습니다.

PHP 사진에 텍스트를 쓰는 방법#🎜 🎜## 🎜🎜#

2. 이미지 워터마크

이미지 워터마크는 하나의 이미지를 다른 이미지에 추가하는 것으로 주로 gd 라이브러리의 imagecopy와 imagecopymerge를 사용합니다.

$dst_path = 'myimage.jpg';
$src_path = 'http://www.logodashi.com/FileUpLoad/inspiration/636003768803214440.jpg';
//创建图片的实例
$dst = imagecreatefromstring(file_get_contents($dst_path));
$src = imagecreatefromstring(file_get_contents($src_path));
//获取水印图片的宽高
list($src_w, $src_h) = getimagesize($src_path);
//将水印图片复制到目标图片上,最后个参数50是设置透明度,这里实现半透明效果
imagecopymerge($dst, $src, 10, 10, 0, 0, $src_w, $src_h, 30);
//如果水印图片本身带透明色,则使用imagecopy方法
// imagecopy($dst, $src, 10, 10, 0, 0, $src_w, $src_h);
//输出图片
list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path);
switch ($dst_type) {
    case 1://GIF
        header('Content-Type: image/gif');
        imagegif($dst);
        break;
    case 2://JPG
        header('Content-Type: image/jpeg');
        imagejpeg($dst);
        break;
    case 3://PNG
        header('Content-Type: image/png');
        imagepng($dst);
        break;
    default:
        break;
}
imagedestroy($dst);
imagedestroy($src);

렌더링:

PHP 사진에 텍스트를 쓰는 방법

3. 이미지 처리와 관련된 기타 기능

#🎜 🎜 #

/*
 *返回图像的大小及图像类型
// Get the size of an image
$size = getimagesize("http://image18-c.poco.cn/mypoco/myphoto/20160901/20/17857099520160901203311082.jpg?750x956_120");
print_r($size);
// 打印结果
Array
(
    [0] => 750
    [1] => 956
    [2] => 2
    [3] => width="750" height="956"
    [bits] => 8
    [channels] => 3
    [mime] => image/jpeg
)
*/
/**
 * imagecopy — 拷贝图像的一部分
 */
// bool imagecopy ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h )
// 将 src_im 图像中坐标从 src_x,src_y 开始,宽度为 src_w,高度为 src_h 的一部分拷贝到 dst_im 图像中坐标为 dst_x 和 dst_y 的位置上。
/*
 *
 * http://php.net/manual/zh/function.imagecopymerge.php
 *imagecopymerge — 拷贝并合并图像的一部分
 *bool imagecopymerge ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h , int $pct )
 *
 *
 */
추천 튜토리얼: PHP 튜토리얼

위 내용은 PHP 사진에 텍스트를 쓰는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.