使用GD函数时,使用php调整文本大小

I did found a topic similar to this, but I do not know if the solution is the same. So here is my question:

I'm using the GD functions to bild a web card generating program. The thing is that the card's backgound is generating by the $image = imagecreatefrompng(); function. The card need's also a $cardname as "title" and a $desription as desription. For that I used the imagettftext(); function. But there is a problem, the card's size is 333x485, I need the text to be resized in order to fit in the background without resizing its height, but only the width!

To be more to the point, the $cardname should have width = 240 and height = 34, but if it doesn't fit, it goes off the background, I need a function that will resize its width in order to fit in 240px and leave the height to 34px always!

To understand it more look here: http://yugiohcardmaker.net. in the "name" you can add as much text you like, it will always fit in and in the right width and height!

I'm not going to try and code this as it will take too long, but here's the basic process:

  1. Get the size of the bounding box for your text with imagettfbbox();

  2. Create a new image with imagecreatetruecolor();

  3. Write your text into your new image with imagettftext();

  4. Use imagecopyresampled() to copy the new image with your text to your existing card, setting the parameters to shrink the width but not the height.

Note: the bounding box parameters returned by imagettfbbox()) can be fiddly to work with You'll also need to be careful about alphablending and background colors to ensure that only your text pixels are copied.

Good luck!