Coding standards
Security in ResourceSpace
Developer reference
Database
Action functions
Admin functions
Ajax functions
Annotation functions
API functions
Collections functions
Comment functions
Config functions
CSV export functions
Dash functions
Debug functions
Encryption functions
Facial recognition functions
File functions
General functions
Language functions
Log functions
Login functions
Message functions
Migration functions
Node functions
PDF functions
Plugin functions
Render functions
Reporting functions
Request functions
Research functions
Slideshow functions
Theme permission functions
User functions
Video functions
Database functions
Metadata functions
Resource functions
Search functions
Map functions
Job functions
Tab functions
Test 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 51 to 82

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 4th December 2024 08:35 Europe/London time based on the source file dated 8th November 2024 11:45 Europe/London time.