如何将生成的图像(来自图像创建从jepeg)保存在服务器(而不是PC中)


How to save generated image (from imagecreatefromjepeg) in the server (not in the PC)?

我被要求创建一个 这将要求文本输入和要上传的照片。 然后创建一个显示这些图像的图像。 然后必须将生成的图像保存到服务器(不使用数据库)。 我这里有一个成功生成图像的代码(在这种情况下,我使用了 imagejpeg 和 imagecreatefromjpeg。 但是我仍然需要将其保存到带有文件扩展名.jpg的服务器。 我还需要给它一个唯一的名字。

我尝试使用header('Content-Disposition: attachment; filename="image.jpg"'); 但它的作用是将其保存到不在服务器中的 PC 上。 下面是我的代码。 请放心编辑它。 另外,请留下一些评论,以便我能够理解它,而不仅仅是复制粘贴。 提前非常感谢您的帮助。 我现在真的需要让它工作。 再次感谢

    //for textbox input
$title = $_POST['title'];
$story = "My super story begins with" . $_POST['story'] . " My task was " . $_POST['task'] ." With the super power of ". $_POST['power'] ." I solved it by ". $_POST['solve'] ." The result was". $_POST['result'];

//header('Content-Disposition: attachment; filename="image.jpg"'); //this works for saving img to PC or downloading it force .jpg ext
header('Content-Type: image/jpeg');

$upload = $uploadFilename; //this is for getting the uploaded file
$im = imagecreatefromjpeg("bg2.jpg");
$img2 = imagecreatefromjpeg($upload);
$black = imagecolorallocate($im, 0, 0, 0);
$font = 'arialbi.ttf';
$font2 = 'ariali.ttf';
imagettftext($im, 24, 0, $width_sum, 300, $black, $font, $title);

$newtext = wordwrap($story, 35, "'n", true);
$newertext2 = explode  ("'n", $newtext);
imagettftext($im, 8, 0, 300, 362, $black, $font, $story);
imagettftext($im, 8, 0, 300, 374, $black, $font2,$story);
imagettftext($im, 8, 0, 300, 386, $black, $font, $story);
imagettftext($im, 8, 0, 300, 398, $black, $font2, $story);
imagettftext($im, 8, 0, 300, 410, $black, $font, $story);
imagettftext($im, 8, 0, 300, 422, $black, $font2, $story);
imagettftext($im, 8, 0, 300, 434, $black, $font,$story);
imagettftext($im, 8, 0, 300, 446, $black, $font2, $story);
imagettftext($im, 8, 0, 300, 458, $black, $font,$story);
imagettftext($im, 8, 0, 300, 570, $black, $font2, $story);
imagettftext($im, 8, 0, 300, 582, $black, $font, $story);

imagecopymerge($im, $img2, 10, 350, 0, 0, imagesx($img2), imagesy($img2), 100);
imagejpeg($im, null, 100);

//closing for imagejpeg
imagejpeg($im);
imagedestroy($im);

好吧,正如imagejpeg手册所说:第二个参数是要存储图像的文件名。

try imagejpeg($im, "/PATH/IMAGE_NAME.jpeg")

这应该会有所帮助。