douji0073 2018-09-13 20:56
浏览 39
已采纳

当我尝试在PHP + GD库中添加图像时,文本正在消失

I'm trying to create a PNG with some text and a scaled picture in it. Here is the code for just the text, it works fine:

<?php
session_start();
error_reporting(E_ALL);

$label = imagecreate(500, 500);
imagecolorallocate($label, 0, 0, 0);

// up text
$color = imagecolorallocate($label, 255, 255, 255);
imagettftext($label, 50, 0, 0, 150, $color, "arial.ttf", "UP UP UP");

// down text
$color = imagecolorallocate($label, 255, 255, 255);
imagettftext($label, 50, 0, 0, 350, $color, "assets/fonts/arial.ttf", "DOWN DOWN DOWN");

header('Content-type: image/png');
imagepng($label);
imagedestroy($label);
die();
?>

With the code above you get the following picture, which is correct:

enter image description here

Now I'm trying to have a small picture in it, So I'm loading the picture from a JPEG file (adidas.jpg). Here's the code

<?php
session_start();
error_reporting(E_ALL);


$label = imagecreate(500, 500);
imagecolorallocate($label, 0, 0, 0);


// up text
$color = imagecolorallocate($label, 255, 255, 255);
imagettftext($label, 50, 0, 0, 150, $color, "arial.ttf", "UP UP UP");

// image
$src = imagecreatefromjpeg("adidas.jpg");
$pic = imagecreatetruecolor(500, 500);
imagecopyresampled($label, $src, 0, 0, 0, 0, 150, 150, imagesx($src), imagesy($src));
$white = imagecolorallocate($pic, 255, 255, 255);
imagefill($label,0,0,$white);
imagedestroy($pic);


// down text
$color = imagecolorallocate($label, 255, 255, 255);
imagettftext($label, 50, 0, 0, 350, $color, "arial.ttf", "DOWN DOWN DOWN");

header('Content-type: image/png');
imagepng($label);
imagedestroy($label);
die();
?>

And this is what I get:

enter image description here

To my surprise the "down" text disappeared. Why is that? The text added before the picture is fine, the text added after it turns to black for some reason

  • 写回答

1条回答 默认 最新

  • duanlu0559 2018-09-13 22:00
    关注

    Your code is bit messy, "DOWN.." text will appear if you remove second:

    $color = imagecolorallocate($label, 255, 255, 255);
    

    You dont fill the original image, you try it later but with wrong color ($white is from $pic, not $label). I cleaned it up:

    <?php
    session_start();
    error_reporting(E_ALL);
    
    $label = imagecreate(500, 500);
    $black = imagecolorallocate($label, 0, 0, 0);
    $white = imagecolorallocate($label, 255, 255, 255);
    imagefill($label, 0, 0, $black);
    
    imagettftext($label, 50, 0, 0, 150, $white, "arial.ttf", "UP UP UP");
    
    $src = imagecreatefromjpeg("adidas.jpg");
    $pic = imagecreatetruecolor(500, 500);
    imagecopyresampled($label, $src, 0, 0, 0, 0, 150, 150, imagesx($src), imagesy($src));
    $white2 = imagecolorallocate($pic, 255, 255, 255);
    
    imagettftext($label, 50, 0, 0, 350, $white, "arial.ttf", "DOWN DOWN DOWN");
    
    ob_end_clean();
    header('Content-type: image/png');
    imagepng($label);
    
    imagedestroy($src);
    imagedestroy($pic);
    imagedestroy($label);
    die();
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。