duanchuonong5370 2013-09-11 00:06
浏览 47
已采纳

使用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!

  • 写回答

1条回答 默认 最新

  • douxian7117 2013-09-11 00:19
    关注

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码