PHP的GD - 对齐文本居中水平,减少字体大小,以保持它在图像内它在、字体大小、图像、文本

由网友(森屿未央)分享简介:希望你做得很好。我还是用PHP一个新手,所以做一些阅读后在检查一些职位,在这里我能够把一些文字上的图像使用PHP GD的imagecreatefrompng()函数,用户会来到一个形式,他们将能够进入他们的名字和这个名字将被写入在图像上,可惜我已经无法水平对齐文本中心,我尝试了所有可能的方法(我的方式很明显,而且必须...

希望你做得很好。

我还是用PHP一个新手,所以做一些阅读后在检查一些职位,在这里我能够把一些文字上的图像使用PHP GD的imagecreatefrompng()函数,用户会来到一个形式,他们将能够进入他们的名字和这个名字将被写入在图像上,可惜我已经无法水平对齐文本中心,我尝试了所有可能的方法(我的方式很明显,而且必须是错误的)与imagettfbbox,但我失败了我所有的努力,你可以请你们帮我一点点水平对齐的中锋?此外,由于我使用的是一种另类的大字体,我需要,如果大小减小输入的名称是那种长,所以这种方式不会超过图像的限制,并会留在中心。我得到的文本价值的一种形式,你可以检查在我的code开头:

I’m still a newbie with php so after making some reading and while checking some posts here I was able to put some text over an image with the imagecreatefrompng() function using the PHP GD, users will come to a form and they will be able to enter their name and the name will be written over the image , unfortunately I have been unable to align the text center horizontally, I tried all ways possible (my ways obviously and must be wrong) with imagettfbbox but I failed in all my attempts, could you please guys help me out a little bit to align the string center horizontally? Also since I’m using a kind of alternative big font I need that the size decrease if the name entered is kind of long so this way it will not surpass the image limits and will stay at the center. I’m getting the value of the text from a form as you may check at the beginning of my code:

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

  ?>

您的帮助将是非常美联社preciated,稍后我会打破我的头试图通过单击提交按钮,因为我不希望用户保存图片通过右击它来保存图像。

Your help will be highly appreciated, later on I will break my head trying to save the image by clicking a submit button since I do not want the users to save the image by right clicking on it.

感谢好朋友!

推荐答案

您所需要的图像的宽度和文本的宽度,以涉及两个。

you need the width of the image and the width of the text to relate both.

// 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 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);
阅读全文

相关推荐

最新文章