php - 如何在 PHP imagecolorallocate() 中使用背景图像而不是颜色

标签 php html css

我想创建一个自定义验证码。 所以我需要将文本和背景图像转换为单个图像;
现在我只能在imagecolorallocate()
中使用背景颜色,这是我的代码

<?php
$text = rand (1000, 9999);
$my_img = imagecreate( 200, 80 );
$background = imagecolorallocate( $my_img,255, 255, 255);
$text_colour = imagecolorallocate( $my_img, 0, 0, 255 );
$line_colour = imagecolorallocate( $my_img, 255, 255, 255 );
imagestring( $my_img, 4, 30, 25, $text, $text_colour );
imagesetthickness ( $my_img, 5 );
imageline( $my_img, 30, 45, 165, 45, $line_colour );
header( "Content-type: image/png" );
imagepng( $my_img );
imagecolordeallocate( $line_color );
imagecolordeallocate( $text_color );
imagecolordeallocate( $background );
imagedestroy( $my_img );
?>

最佳答案

 <?php
 function LoadJpeg($imgname)
 {
  if(!$im)
{
    /* Create a black image */
    $im  = imagecreatetruecolor(150, 30);
    $bgc = imagecolorallocate($im, 255, 255, 255);
    $tc  = imagecolorallocate($im, 0, 0, 0);

    imagefilledrectangle($im, 0, 0, 150, 30, $bgc);

    /* Output an error message */
    imagestring($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
}

return $im;
}

 header('Content-Type: image/jpeg');
 $img = LoadJpeg('bogus.image');
 imagejpeg($img);
 imagedestroy($img);
 ?>

关于php - 如何在 PHP imagecolorallocate() 中使用背景图像而不是颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21746766/

相关文章:

python - 使用 Cactus App for Mac 设置全局变量

jquery - 使平均菜单位置固定

css - 具有动态高度和页脚的模态窗口

php 和 mysql 到 xml

php - 使用 PHP 在 for 循环中使用 mysql 查询的困难

php - 在 RegEx 中接受国际名称字符

PHP、MySQL 结果中出现 HTML 错误

javascript - 使用 Javascript 使用 xPath 即时修改 HTML 源代码

html - <hr> 未显示

javascript - 如何制作脚注以在单击的位置显示其引用的文本?