【问题标题】:How to add a random background image while creating a text image using php?如何在使用 php 创建文本图像时添加随机背景图像?
【发布时间】:2015-04-11 12:06:11
【问题描述】:

我想做类似的事情http://www.appline.ro/pasti谢谢帮助

刷新时我想显示另一个图像,随机图像,但我希望能够在其上写入以获得最终图像http://2ol.us/scrie/lol.php。 $image_file ='image.png';

    <?php   
            $font_file     = 'franklin.ttf';

            $image_file    = 'http://i642.photobucket.com/albums/uu143/paloma666/nature.png';






            // customizable variables
            $font_size = 47 ; // font size in pts
            $font_color = '#FF0000' ;

                // clean up the input
                if(empty($_GET['text']))    fatal_error('Error: No text specified.') ;
                $text = html_entity_decode($_GET['text']);
                //$text = floatval($text);
                //$text = number_format($text, 1, '.', '') . '%';

                if(empty($text))
                    fatal_error('Error: Text not properly formatted.') ;






                // x and y for the bottom right of the text
                // so it expands like right aligned text
                $x_finalpos     = 247;
                $y_finalpos     = 137;

                // trust me for now...in PNG out PNG

                $mime_type          = 'image/png' ;
                $extension          = '.png' ;
                $s_end_buffer_size  = 4096 ;

                // check for GD support
                if(!function_exists('ImageCreate'))
                    fatal_error('Error: Server does not support PHP image generation') ;

                // check font availability;
                if(!is_readable($font_file)) {
                    fatal_error('Error: The server is missing the specified font.') ;
                }

                // create and measure the text
                $font_rgb = hex_to_rgb($font_color) ;
                $box = @ImageTTFBBox($font_size,0,$font_file,$text) ;

                $text_width = abs($box[2]-$box[0]);
                $text_height = abs($box[5]-$box[3]);

                $image =  imagecreatefrompng($image_file);
                if(!$image || !$box)
                {
                    fatal_error('Error: The server could not create this image.') ;
                }

                // allocate colors and measure final text position
                $font_color = ImageColorAllocate($image,$font_rgb['red'],$font_rgb['green'],$font_rgb['blue']) ;
                $image_width = imagesx($image);
                $put_text_x = $image_width - $text_width - ($image_width - $x_finalpos);
                $put_text_y = $y_finalpos;

                // Write the text
                imagettftext($image, $font_size, 0, $put_text_x,  $put_text_y, $font_color, $font_file, $text);

                header("Content-type: $mime_type");
                ImagePNG($image) ;
                ImageDestroy($image) ;
                exit ;

                /*
                    attempt to create an image containing the error message given. 
                    if this works, the image is sent to the browser. if not, an error
                    is logged, and passed back to the browser as a 500 code instead.
                */

                function fatal_error($message)
                {
                    // send an image
                    if(function_exists('ImageCreate'))
                    {
                        $width = ImageFontWidth(5) * strlen($message) + 10 ;
                        $height = ImageFontHeight(5) + 10 ;
                        if($image = ImageCreate($width,$height))
                        {
                            $background = ImageColorAllocate($image,255,255,255) ;
                            $text_color = ImageColorAllocate($image,0,0,0) ;
                            ImageString($image,5,5,5,$message,$text_color) ;    
                            header('Content-type: image/png') ;
                            ImagePNG($image) ;
                            ImageDestroy($image) ;
                            exit ;
                        }
                    }

                    // send 500 code
                    header("HTTP/1.0 500 Internal Server Error") ;
                    print($message) ;
                    exit ;
                }

                /* 
                    decode an HTML hex-code into an array of R,G, and B values.
                    accepts these formats: (case insensitive) #ffffff, ffffff, #fff, fff 
                */    

                function hex_to_rgb($hex) {
                    // remove '#'
                    if(substr($hex,0,1) == '#')
                        $hex = substr($hex,1) ;

                    // expand short form ('fff') color to long form ('ffffff')
                    if(strlen($hex) == 3) {
                        $hex = substr($hex,0,1) . substr($hex,0,1) .
                               substr($hex,1,1) . substr($hex,1,1) .
                               substr($hex,2,1) . substr($hex,2,1) ;
                    }

                    if(strlen($hex) != 6)
                        fatal_error('Error: Invalid color "'.$hex.'"') ;

                    // convert from hexidecimal number systems
                    $rgb['red'] = hexdec(substr($hex,0,2)) ;
                    $rgb['green'] = hexdec(substr($hex,2,2)) ;
                    $rgb['blue'] = hexdec(substr($hex,4,2)) ;

                    return $rgb ;
                }
            ?>





 <?php   
            $font_file     = 'franklin.ttf';

 $image_file    ='http://i642.photobucket.com/albums/uu143/paloma666/nature.png';

            // customizable variables
            $font_size = 47 ; // font size in pts
            $font_color = '#FF0000' ;

                // clean up the input
                if(empty($_GET['text']))    fatal_error('Error: No text specified.') ;
                $text = html_entity_decode($_GET['text']);
                //$text = floatval($text);
                //$text = number_format($text, 1, '.', '') . '%';

                if(empty($text))
                    fatal_error('Error: Text not properly formatted.') ;






                // x and y for the bottom right of the text
                // so it expands like right aligned text
                $x_finalpos     = 247;
                $y_finalpos     = 137;

                // trust me for now...in PNG out PNG

                $mime_type          = 'image/png' ;
                $extension          = '.png' ;
                $s_end_buffer_size  = 4096 ;

                // check for GD support
                if(!function_exists('ImageCreate'))
                    fatal_error('Error: Server does not support PHP image generation') ;

                // check font availability;
                if(!is_readable($font_file)) {
                    fatal_error('Error: The server is missing the specified font.') ;
                }

                // create and measure the text
                $font_rgb = hex_to_rgb($font_color) ;
                $box = @ImageTTFBBox($font_size,0,$font_file,$text) ;

                $text_width = abs($box[2]-$box[0]);
                $text_height = abs($box[5]-$box[3]);

                $image =  imagecreatefrompng($image_file);
                if(!$image || !$box)
                {
                    fatal_error('Error: The server could not create this image.') ;
                }

                // allocate colors and measure final text position
                $font_color = ImageColorAllocate($image,$font_rgb['red'],$font_rgb['green'],$font_rgb['blue']) ;
                $image_width = imagesx($image);
                $put_text_x = $image_width - $text_width - ($image_width - $x_finalpos);
                $put_text_y = $y_finalpos;

                // Write the text
                imagettftext($image, $font_size, 0, $put_text_x,  $put_text_y, $font_color, $font_file, $text);

                header("Content-type: $mime_type");
                ImagePNG($image) ;
                ImageDestroy($image) ;
                exit ;

                /*
                    attempt to create an image containing the error message given. 
                    if this works, the image is sent to the browser. if not, an error
                    is logged, and passed back to the browser as a 500 code instead.
                */

                function fatal_error($message)
                {
                    // send an image
                    if(function_exists('ImageCreate'))
                    {
                        $width = ImageFontWidth(5) * strlen($message) + 10 ;
                        $height = ImageFontHeight(5) + 10 ;
                        if($image = ImageCreate($width,$height))
                        {
                            $background = ImageColorAllocate($image,255,255,255) ;
                            $text_color = ImageColorAllocate($image,0,0,0) ;
                            ImageString($image,5,5,5,$message,$text_color) ;    
                            header('Content-type: image/png') ;
                            ImagePNG($image) ;
                            ImageDestroy($image) ;
                            exit ;
                        }
                    }

                    // send 500 code
                    header("HTTP/1.0 500 Internal Server Error") ;
                    print($message) ;
                    exit ;
                }

                /* 
                    decode an HTML hex-code into an array of R,G, and B values.
                    accepts these formats: (case insensitive) #ffffff, ffffff, #fff, fff 
                */    

                function hex_to_rgb($hex) {
                    // remove '#'
                    if(substr($hex,0,1) == '#')
                        $hex = substr($hex,1) ;

                    // expand short form ('fff') color to long form ('ffffff')
                    if(strlen($hex) == 3) {
                        $hex = substr($hex,0,1) . substr($hex,0,1) .
                               substr($hex,1,1) . substr($hex,1,1) .
                               substr($hex,2,1) . substr($hex,2,1) ;
                    }

                    if(strlen($hex) != 6)
                        fatal_error('Error: Invalid color "'.$hex.'"') ;

                    // convert from hexidecimal number systems
                    $rgb['red'] = hexdec(substr($hex,0,2)) ;
                    $rgb['green'] = hexdec(substr($hex,2,2)) ;
                    $rgb['blue'] = hexdec(substr($hex,4,2)) ;

                    return $rgb ;
                }
            ?>

【问题讨论】:

  • 到目前为止你尝试过什么?请重新格式化您的代码,删除与问题无关的任何内容,并将文件分离到它们自己的代码块中。

标签: php image random gd


【解决方案1】:

一种可能性是拥有一组可能的图像,然后生成一个随机索引。

可能是这样的:

$array = array("example.com/image1.png","example.com/image2.png","example.com/image3.png");
$index = rand(0,count($array)-1);
$image_file = $array[$index];

我还没有测试过代码,但它应该可以工作:)

【讨论】:

  • 似乎 Marco Bernardini 快了一点:D
【解决方案2】:

创建一个包含一些图像或指向它们的链接的数组,然后使用array_rand() 函数选择它们。

https://php.net/array_rand

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-13
    • 2015-02-04
    • 2013-07-29
    • 1970-01-01
    • 2018-08-01
    • 2012-02-08
    • 2023-04-02
    相关资源
    最近更新 更多