Collections functions
General functions
Node functions
Render functions
Theme permission functions
User functions
Resource functions

create_random_image()

Description

Generate a random image which can be used during testing (e.g to upload, or create previews for)

- text -> Image content text
- filename (default: random)
- width (default: 150)
- height (default: 50)
- bg[red|green|blue] -> Background colour (RGB), e.g $info['bg']['red'] = 234;

Parameters

ColumnTypeDefaultDescription
$info: array
$text_b; get_temp_dir . DIRECTORY_SEPARATOR . "{$filename}.jpg"; if imagejpeg$img
$bg_r
$bg_g
5
20
15
$info['text']
imagecolorallocate$img
$text_r
$text_g
$path
70
$info array Set image parameters:

Return

array Returns an "error" key if something went wrong, otherwise provides some useful info (e.g path)

Location

include/test_functions.php lines 35 to 66

Definition

 
function create_random_image(array $info): array
    {
    
$width $info['width'] ?? 150;
    
$height $info['height'] ?? 50;
    
$filename $info['filename'] ?? generateSecureKey(32);

    
// Background colour
    
$bg_r =  $info['bg']['red'] ?? mt_rand(0255);
    
$bg_g $info['bg']['green'] ?? mt_rand(0255);
    
$bg_b $info['bg']['blue'] ?? mt_rand(0255);

    
// Text colour
    
$text_r $bg_r 128 $bg_r 100 $bg_r 100;
    
$text_g $bg_g 128 $bg_g 100 $bg_g 100;
    
$text_b $bg_b 128 $bg_b 100 $bg_b 100;

    
// Generate image
    
$img imagecreate($width$height);
    
imagecolorallocate($img$bg_r$bg_g$bg_b);
    
imagestring($img52015$info['text'], imagecolorallocate($img$text_r$text_g$text_b));
    
$path get_temp_dir() . DIRECTORY_SEPARATOR "{$filename}.jpg";
    if (
imagejpeg($img$path70))
        {
        return [
            
'path' => $path,
        ];
        }

    return [
        
'error' => 'Failed to create image',
    ];
    }

This article was last updated 26th April 2024 21:35 Europe/London time based on the source file dated 4th April 2024 10:05 Europe/London time.