我被要求创建一个将要求输入文本和上传照片的内容。然后创建一个图像,其中显示这些。然后必须将生成的图像保存到服务器(不使用数据库)。我在这里有一个成功生成图像的代码(在这种情况下我使用了 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);