将文字叠加到图像上

时间:2014-02-21 17:07:08

标签: php forms gd

我要说的是初学者,当谈到php时。我把这个基本脚本放在一起,但显然它不能正常工作。我的目标是将表单文本发布到图像,覆盖,就像原始脚本一样。我的问题是,我不确定如何实现这一目标。我尝试使用基本的帖子功能无济于事。任何帮助或建议将非常感谢使其正常运行。我相信它对我所在的其他人来说也是一个有用的工具。此外,这是最有效的方法吗?将表单和叠加脚本分开保存会更好吗?

ORIGINAL:

<?php
header("Content-type: image/jpeg");
$imgPath = 'image.jpg';
$image = imagecreatefromjpeg($imgPath);
$color = imagecolorallocate($image, 255, 255, 255);
$string = "yay text";
$fontSize = 3;
$x = 115;
$y = 185;
imagestring($image, $fontSize, $x, $y, $string, $color);
imagejpeg($image);
?>

我的补充:

<?php
header("Content-type: image/jpeg");

$name = "";
$location = "";

if(isset($_POST['submit']))
    {   
        $name = $_POST['name'];
        $location = $_POST['location']; 
    }

$imgPath = 'nearyou.jpg';
$image = imagecreatefromjpeg($imgPath);
$color = imagecolorallocate($image, 255, 255, 255);
//$echo = "Name\n" . "Location";    
$echo = $name . "<br />" . $location;
$fontSize = 6;
$font = "HelveticaNeue.ttf";
$x = 10;
$y = 460;
imagestring($image, $font, $fontSize, $x, $y, $echo, $color);
imagejpeg($image);
?>


<form method="POST" name="overlay_form" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>"> 
name<br />
<input type="text" name="name" value='<?php echo htmlentities($name) ?>'><br /><br />

location<br />
<input type="text" name="location" value='<?php echo htmlentities($location) ?>'><br /><br />

<input type="submit" value="Submit" name='submit'>
</form>

我的解决方案:

overlay_form.php:

<?php
$name = "";
$location = "";

if(isset($_POST['submit']))
{   
$name = $_POST['name'];
$location = $_POST['location']; 
}

?>
<form method="POST" name="contact_form" action="overlay_post.php"> 
name<br />
<input type="text" name="name" value='<?php echo htmlentities($name) ?>'><br /><br />

location name<br />
<input type="text" name="location" value='<?php echo htmlentities($location) ?>'><br /><br />

<input type="submit" value="Submit" name='submit'>
</form>

overlay_post.php:

<?php
header("Content-type: image/jpeg");
$imgPath = 'nearyou.jpg';
$image = imagecreatefromjpeg($imgPath);
$color = imagecolorallocate($image, 255, 255, 255);
$echo = $name . "\r\n" . $location ;
$fontSize = 6;
$x = 30;
$y = 460;
imagestring($image, $fontSize, $x, $y, $echo, $color);
imagejpeg($image);
?>

我现在的问题是,新行和回车在图像上显示为奇数字符。我还想分别设计每一条线。我可以使用一些帮助。

0 个答案:

没有答案