Php 如何在向图像添加文本后保存图像
为了将文本添加到图像中,我使用了以下网站代码Php 如何在向图像添加文本后保存图像,php,gd,Php,Gd,为了将文本添加到图像中,我使用了以下网站代码 <?php //Set the Content Type header('Content-type: image/jpeg'); // Create Image From Existing File $jpg_image = imagecreatefromjpeg('sunset.jpg'); // Allocate A Color For The Text $white = imagecol
<?php
//Set the Content Type
header('Content-type: image/jpeg');
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('sunset.jpg');
// Allocate A Color For The Text
$white = imagecolorallocate($jpg_image, 255, 255, 255);
// Set Path to Font File
$font_path = 'font.TTF';
// Set Text to Be Printed On Image
$text = "This is a sunset!";
// Print Text On Image
imagettftext($jpg_image, 25, 0, 75, 300, $white, $font_path, $text);
// Send Image to Browser
imagejpeg($jpg_image);
// Clear Memory
imagedestroy($jpg_image);
?>
有什么方法可以将图像保存在jpg中吗?要将图像保存在文件中而不是输出它,只需更改行
imagejpeg($jpg\u image)
toimagejpeg($jpg_image,'yourfile.jpg')
(您还可以删除标题('Content-type:image/jpeg');
(在本例中)。这是我自己的代码,运行良好!只需将name.jpg的名称更改为所需的文件名:
<?php
header('Content-type: image/jpeg');
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('sunset.jpg');
//$jpg_image=imagecreatetruecolor(100,100);
// Allocate A Color For The Text
$white = imagecolorallocate($jpg_image, 255, 255, 255);
// Set Path to Font File
$font_path = 'font1.TTF';
// Set Text to Be Printed On Image
$text = "This is a sunset!";
// Print Text On Image
$x=20;
for($i=0;$i<=strlen($text);$i++){
$print_text=substr($text,$i,1);
$x+=20;
imagettftext($jpg_image, 30, 0, $x, 200, $white, $font_path, $print_text);
}
// Send Image to Browser
imagejpeg($jpg_image,'name.jpg');
// Clear Memory
imagedestroy($jpg_image);
?>
imagettftext($jpg_image, 30, 0, $x, 200, $white, $font_path, $print_text);
另一个不同的是character,您将字符串(不是字符)赋给了imagettftext函数,但我只赋了一个字符。我认为字符比字符串更好,尤其是在创建验证码方面。检查这段代码。它和你的代码一样,唯一的区别是
imagejpeg($jpg_image,“imagefolderpath/image.jpg”)代码>
而不是imagejpeg($jpg_image)代码>
也许对你有帮助。
imagettftext($jpg_image, 30, 0, $x, 200, $white, $font_path, $print_text);