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

I want to create a custom captcha. so i need to convert text and background image as single image;
Now i can use only background color in imagecolorallocate()
here is my code

<?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 );
?>

You can use the imagecreatefromjpg PHP function, or any of its associated functions based on the image type, like imagecreatefrompng.

This will create an image element in PHP from the selected file that you can use as a background file.

From the PHP manual.

Your next option is to use imagecopymerge() that will use an existing image element and merge it with another image. Info from the PHP manual

For adding a background to your new image you need to use imagecreatefrompng instead of imagecreate.

You can go through another stack overflow link

 <?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);
 ?>