gpt4 book ai didi

php - 向图像添加文本后如何保存图像

转载 作者:行者123 更新时间:2023-12-03 05:09:51 26 4
gpt4 key购买 nike

为了向图像添加文本,我使用网站中的以下代码

<?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);
?>

它运行良好,但我无法保存在图片中以保存文本文件,我正在使用它功能

function write($post,$myFile){
$fh = fopen($myFile, 'a+') or die("can't open file");
fwrite($fh, $post);
fclose($fh);
}

有什么办法可以将图像保存为 jpg 格式吗?

最佳答案

检查此代码。它与您的代码相同,唯一的区别是

imagejpeg($jpg_image,"imagefolderpath/image.jpg");

而不是imagejpeg($jpg_image);
也许对你有帮助。

<?php
header('Content-type: image/jpeg');
$jpg_image = imagecreatefromjpeg('sunset.jpg');
$white = imagecolorallocate($jpg_image, 255, 255, 255);
$font_path = 'font.TTF';
$text = "This is a sunset!";
imagettftext($jpg_image, 25, 0, 75, 300, $white, $font_path, $text);
imagejpeg($jpg_image,"imagefolderpath/image.jpg");
imagedestroy($jpg_image);
?>

关于php - 向图像添加文本后如何保存图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26074071/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com