dongtuoao7987 2014-07-04 08:19
浏览 45
已采纳

GD Lib - 文本与背景图像

This one is quite difficult to explain clearly. So I think pictures might help.

I currently have a script that adds some text over an image in a custom font. I now want make it so that the text has a rectangular background to sit on.

The code I currently have is:

        $rImg = ImageCreateFromJPEG($_REQUEST['imageurl']);
        $cor = imagecolorallocate($rImg, 255, 255, 255);
        $font = 'font.TTF';
        imagettftext($rImg, 34, 0, 0, 100, $cor, $font, urldecode('Test Text'));
        header('Content-type: image/jpeg');
        imagejpeg($rImg,NULL,100);

It creates an image like this:

enter image description here

I need the image to look something like this:

enter image description here

Does anyone know a simple way of achieving this look? The red rectangle needs to end within range of the image. I would just use CSS, but it's for email!

Appreciate any advice!

  • 写回答

1条回答 默认 最新

  • duanao2688 2014-07-04 08:26
    关注

    First you should calculate the boundaries of the text, so you can know how big red box you need. You could achive this with imagettfbbox function. Once you know the dimensions you can draw rectangle using imagefilledrectangle function. Also you should draw rectangle before you draw text.

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

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办