使用形式从文字创建图像并保存到服务器(Create image from text using fo

2019-09-26 03:08发布

我试图用一个PHP函数我的网页上,从文本输入创建的图像。 但似乎没有发生。 这是我当前的代码 -

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技术动作的原因 -

$("#make_pic").click(function() {
    $('#picform').ajaxForm();
});

UPDATE

我试图让放在我创建的图像上一个图像,从而创建一个最终图像。 这是我当前的代码,该协会致力于创建图像,但没有放置叠加图像---

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

Answer 1:

PHP的一部分实际上对我的作品,虽然这可以被替换为:

ob_start();
imagepng($im);
$data = ob_get_clean();
file_put_contents($to,$data);
imagedestroy($im);

有了这个:

imagepng($im, $to);
imagedestroy($im);

但我不是正面的,如果是这样的问题,因为现有的代码为我工作。 因此,尝试检查是否$paths是一个有效的目录?

UPDATE

似乎没有任何要发生的事情

难道是因为你没有echo创建映像后(或输出的东西)? 嗯,我的意思是,图像可能成功(重新)被创建,但你的PHP / JS脚本没有“告诉”你的状态; 因此你看到“无”。 尝试echo -ing分词是这样的:

if (isset($_POST['make_pic'])) {
  ...
  imagedestroy($im);

  echo 'Success!';
}

然后使用此JS:

$('#picform').ajaxForm({
  success: function(res){
    alert(res);
  }
});

(更新2:我故意去掉了“附加说明”部分,但你总是可以看到答案的修订发现的一部分。)



文章来源: Create image from text using form and save to server