为了将文本添加到图像中,我使用来自网站的以下代码
<?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);
?>
Run Code Online (Sandbox Code Playgroud)
它运行良好,但我无法保存在图片中以保存文本文件我正在使用此功能
function write($post,$myFile){
$fh = fopen($myFile, 'a+') or die("can't open file");
fwrite($fh, $post);
fclose($fh);
}
Run Code Online (Sandbox Code Playgroud)
有什么办法可以将图像保存为 jpg 吗?
这是我自己的代码并且运行良好!只需将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);
?>
Run Code Online (Sandbox Code Playgroud)
我的代码与你的有点不同,其中一个不同是你没有改变指针的位置,你要放置角色的位置 我的意思是 $x:
imagettftext($jpg_image, 30, 0, $x, 200, $white, $font_path, $print_text);
Run Code Online (Sandbox Code Playgroud)
另一个不同的是字符,你给了 imagettftext 函数字符串(不是字符) ,但我给了一个字符。我认为字符比字符串更好,特别是在创建验证码方面。
| 归档时间: |
|
| 查看次数: |
16413 次 |
| 最近记录: |