我想添加文本到jpg创建一个新图像。
服务器上已经有image_1.jpg了我想把用户提交的副本放在image_1.jpg上面创建一个新图像将副本和原始图像合并成一个新的栅格化jpg
我知道你可以在php
中使用GD Libraries
来栅格化副本,但是你能分层吗?我的网站是用PHP写的,但我愿意使用第三方插件。
回答:(旧帖子)但我需要什么http://blog.rafaeldohms.com.br/2008/02/12/adding-text-to-images-in-real-time-with-php/
使用GD和Freetype2(如果两者都已安装),那么您可以使用以下步骤将文本添加到JPEG中。
-
使用
imagecreatefromjpeg()
从文件创建图像资源 -
使用Freetype2库添加文本到该图像,通过函数
imagefttext()
(注意,如果您只安装了Freetype而不是Freetype2,您也可以使用函数imagettftext()
)。 -
使用
imagejpeg()
保存修改后的图像
例子:
[我刚刚在浏览器中输入了这个,从来没有运行过-所以如果需要修改,道歉。]
/**
* Annotate an image with text using the GD2 and Freetype2 libraries
*
* @author Orbling@StackOverflow
*
* @param string $sourceFileName Source image path
* @param string $destinationFileName Destination image path
* @param string $text Text to use for annotation
* @param string $font Font definition file path
* @param float $fontSize Point size of text
* @param array $fontColour Font colour definition, expects
array('r' => #, 'g' => #, 'b' => #),
defaults to black
* @param int $x x-coordinate of text annotation
* @param int $y y-coordinate of text annotation
* @param float $rotation Angle of rotation for text annotation,
in degrees, anticlockwise from left-to-right
* @param int $outputQuality JPEG quality for output image
*
* @return bool Success status
*/
function imageannotate($sourceFileName, $destinationFileName,
$text, $font, $fontSize, array $fontColour = NULL,
$x, $y, $rotation = 0, $outputQuality = 90) {
$image = @imagecreatefromjpeg($sourceFileName);
if ($image === false) {
return false;
}
if (is_array($fontColour) && array_key_exists('r', $fontColour)
&& array_key_exists('g', $fontColour)
&& array_key_exists('b', $fontColour)) {
$colour = imagecolorallocate($image, $fontColour['r'],
$fontColour['g'],
$fontColour['b']);
if ($colour === false) {
return false;
}
} else {
$colour = @imagecolorallocate($image, 0, 0, 0);
}
if (@imagefttext($image, $fontSize, $rotation,
$x, $y, $colour, $font, $text) === false) {
return false;
}
return @imagejpeg($image, $destinationFileName, $outputQuality);
}
NB。为了调试,我会删除@
符号。