使用表单从文本创建图像并保存到服务器 [英] Create image from text using form and save to server
问题描述
我正在尝试在页面上使用php函数从文本输入创建图像.但是似乎什么也没有发生.这是我当前的代码-
I am trying to use a php function on my page to create an image from a text input. But nothing seems to be happening. This is my current code -
Php-
$to = $paths. '/card.png';
if (isset($_POST['make_pic'])) {
$text = $_POST['txt_input'];
$im = imagecreate(200, 80);
// White background and blue text
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 255);
imagestring($im, 4, 30, 25, $text, $textcolor);
ob_start();
imagepng($im);
$data = ob_get_clean();
file_put_contents($to,$data);
imagedestroy($im);
}
HTML-
<form name="picform" id="picform" action="" method="post" enctype="multipart/form-data">
<input type="text" class="input-field" name="txt_input" maxlength="50">
<input type="submit" id="make_pic" name="make_pic" value="Convert">
</form>
该表单没有操作,原因是我正在使用ajax-
The form doesn't have a action cause I'm using ajax -
$("#make_pic").click(function() {
$('#picform').ajaxForm();
});
更新
我正在尝试将一张图像放置在我创建的图像之上,从而创建一张最终图像.这是我当前的代码,该代码可用于创建图像,但不能放置叠加图像---
I am trying to get one image placed on top of the image I created, thus creating one final image. This is my current code, which works for creating the image but not placing the overlay image ---
$paths = "/uploads/". $current_user->ID."/".$v_Id;
$assets = "/uploads/assets";
$to = $paths. '/contact_card.png';
// Text from form to be used
$c_name1 = $_POST['c_name'];
$c_biz1 = $_POST['c_biz'];
$c_num = $_POST['c_num'];
$c_mail1 = $_POST['c_email'];
$c_name = strtoupper($c_name1);
$c_biz = strtoupper($c_biz1);
$c_mail = strtoupper($c_mail1);
$headliner = "Contact Info\r\n";
$text = $headliner."\r\n".$c_name."\r\n".$c_biz."\r\n".$c_num."\r\n".$c_mail;
$im = imagecreate(1280, 720);
$im2 = $paths. '/profile_pic.png';
// Black background
$bg = imagecolorallocate($im, 0, 0, 0);
// Text Colors
$black = imagecolorallocate($im, 0, 0, 0);
$grey = imagecolorallocate($im, 128, 128, 128);
$white = imagecolorallocate($im, 255, 255, 255);
// Font text options
$font = $assets.'/fonts/PlayfairDisplay-Regular.ttf';
$font_size = 24;
$angle = 45;
// Get image Width and Height
$image_width = imagesx($im);
$image_height = imagesy($im);
// Get Bounding Box Size
$text_box = imagettfbbox($font_size,$angle,$font,$text);
// Get your Text Width and Height
$text_width = $text_box[2]-$text_box[0];
$text_height = $text_box[7]-$text_box[1];
// Calculate coordinates of the text
$x = 440;
$y = 260;
// Add the text
imagettftext($im, $font_size, 0, $x, $y, $white, $font, $text);
// HERE I AM TRYING TO PLACE A PICTURE LEFT CENTERED ON TOP OF THE OTHER PICTURE
imagecopymerge($im, $im2, 0, 0, 0, 0, 50, 50, 100);
// Create the image and save to server
ob_start();
imagepng($im);
$data = ob_get_clean();
file_put_contents($to,$data);
imagedestroy($im);
推荐答案
PHP部分实际上对我有用,尽管可以将其替换:
The PHP part actually works for me, though this could be replaced:
ob_start();
imagepng($im);
$data = ob_get_clean();
file_put_contents($to,$data);
imagedestroy($im);
与此:
imagepng($im, $to);
imagedestroy($im);
但是,如果那是问题,我不是很肯定,因为您的现有代码对我有用.因此,请尝试检查$paths
是否为有效目录?
But I'm not positive if that's the issue, since your existing code worked for me. So try checking if the $paths
is a valid directory?
似乎什么都没发生
nothing seems to be happening
是因为创建图像后没有echo
(或输出某些内容)吗?好吧,我的意思是,图像可能已经成功(重新)创建,但是您的PHP/JS脚本不会告诉"您状态.因此,您什么都看不到.尝试echo
-像这样:
Could it be because you didn't echo
(or output something) after the image is created? Well I mean, the image may have been successfully (re-)created, but your PHP/JS script doesn't "tell" you of the status; hence you see "nothing". Try echo
-ing something like this:
if (isset($_POST['make_pic'])) {
...
imagedestroy($im);
echo 'Success!';
}
然后使用此JS:
$('#picform').ajaxForm({
success: function(res){
alert(res);
}
});
(更新#2:我有意删除了附加说明"部分,但您始终可以看到答案的修订查找该部分.)
(Update #2: I intentionally removed the "Additional Note" part, but you can always see the answer's revisions to find that part.)
这篇关于使用表单从文本创建图像并保存到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!