dongzanghong4379 2015-12-28 21:22
浏览 265
已采纳

PHP imagettftext()字体字母不透明

I am using the PHP GD library to allow users to generate their name on top of a background image that I supply. The current issue is that the letters in the outputted PNG are not transparent--as you can see below they overlap each other.

Text: Roger Ajax

Background Image:

Background Image

Result:

Non Transparent Letters

Could this perhaps be due to the TTF font? I have tried the following fonts with the same result:

- HomemadeApple.ttf
- IndieFlower.ttf 
- DancingScript.ttf 
- KaushanScript Regular.ttf
- PermanentMarker.ttf

Here is the full source of my function:

    $signature_text = "Roger Ajax";
    $font_name = "Homemade Apple";

    // Lowercase all letters then capitalize First Lettter of each word
    $signature_text = strtolower($signature_text);
    $signature_text = ucwords($signature_text);

    // Font & Text Settings
    $font_size = 32;
    $font = "/var/gosigner/fonts/HomemadeApple.ttf";
    $desired_width = strlen($signature_text) * 34;
    $start_position = 32;

    // Background Image
    $originalImage = $this->config->item('app_root') . "img/signature_field_blank.png";

    // Verify BG Image can be found
    if(!file_exists($originalImage)) {
        $this->shared->throw_error("Signature template file could not be found");
    }

    $im = imagecreatefrompng($originalImage); // Get original
    imagealphablending($im, false);           // Save Transparency
    imagesavealpha($im, true);                // Save Transparencyc

    $img_resized = imagecreatetruecolor($desired_width, 72);   // Create new PNG
    imagealphablending($img_resized, false);                   // Save Transparency
    imagesavealpha($img_resized, true);    

    $trans_colour = imagecolorallocatealpha($img_resized, 0, 0, 0, 127);
    imagefill($img_resized, 0, 0, $trans_colour);


    $x = imagesx($im); // Original X
    $y = imagesy($im); // Original Y
    imagecopyresampled($img_resized, $im, 0, 0, 0, 0, 214, 72, $x, $y);
    $black = imagecolorallocate($im, 0, 0, 0);
    imagettftext($img_resized, $font_size, 0, $start_position, 45, $black, $font, $signature_text);

    header('Content-Type: image/png');
    imagepng($img_resized);
    imagedestroy($im);
    imagedestroy($img_resized);
  • 写回答

1条回答 默认 最新

  • doushan6692 2015-12-29 00:21
    关注

    I'd add the text to $img_resized (here I'm using different font & color)

    Roger Ajax

    $img_resized = imagecreatetruecolor($desired_width, 72);
    imagesavealpha($img_resized, true);
    imagefill($img_resized, 0, 0, IMG_COLOR_TRANSPARENT);
    $black = imagecolorallocate($img_resized, 0, 0, 0);
    imagefttext($img_resized, $font_size, 0, $start_position, 45, $black, $font, $signature_text);
    

    before placing it on top of $im.

    imagecopyresampled($img_resized, $im, 0, 0, 0, 0, 214, 72, $x, $y);
    

    (in addition I'm not using the imagealphablending() part and using IMG_COLOR_TRANSPARENT instead of $trans_colour)

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

报告相同问题?

悬赏问题

  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?