希望你过得很好。
我仍然是 php 的新手,所以在阅读了一些内容并检查了一些帖子之后,我能够使用 PHP GD 使用 imagecreatefrompng() 函数在图像上放置一些文本,用户将进入一个表单,他们将能够输入他们的名字,并且名字将写在图像上,不幸的是我无法水平对齐文本中心,我尝试了所有可能的方法(我的方法显然是错误的)与 imagettfbbox 但我所有的尝试都失败了,你们能帮我水平对齐弦中心吗?另外,由于我使用的是一种替代大字体,如果输入的名称有点长,我需要减小尺寸,这样它就不会超过图像限制并保持在中心。我正在从表单中获取文本的值,您可以在我的代码开头进行检查:
<?php
$nombre=$_POST['nombre'];
//Set the Content Type
header('Content-type: image/jpeg');
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('fabian.jpg');
// Allocate A Color For The Text
$white = imagecolorallocate($jpg_image, 255, 255, 255);
// Set Path to Font File
$font_path = 'fabian.TTF';
// Set Text to Be Printed On Image , I set it to uppercase
$text =strtoupper($nombre);
// Print Text On Image
imagettftext($jpg_image, 75, 0, 50, 400, $white, $font_path, $text);
// Send Image to Browser
imagepng($jpg_image);
// Clear Memory
imagedestroy($jpg_image);
?>
我们将非常感谢您的帮助,稍后我将尝试通过单击提交按钮来保存图像,因为我不希望用户通过右键单击图像来保存图像。
谢谢朋友们!
您需要图像的宽度和文本的宽度来关联两者。
// get image dimensions
list($img_width, $img_height,,) = getimagesize("fabian.jpg");
// find font-size for $txt_width = 80% of $img_width...
$font_size = 1;
$txt_max_width = intval(0.8 * $img_width);
do {
$font_size++;
$p = imagettfbbox($font_size, 0, $font_path, $text);
$txt_width = $p[2] - $p[0];
// $txt_height=$p[1]-$p[7]; // just in case you need it
} while ($txt_width <= $txt_max_width);
// now center the text
$y = $img_height * 0.9; // baseline of text at 90% of $img_height
$x = ($img_width - $txt_width) / 2;
imagettftext($jpg_image, $font_size, 0, $x, $y, $white, $font_path, $text);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)