AllsWeb Blog
No Result
View All Result
  • Home
  • Main Home
  • PHP and MySQL
  • JavaScript
    • jQuery & AJAX
  • WordPress
  • SEO
  • Web Hosting
  • Comparison
Support
Knowledgebase
  • Home
  • Main Home
  • PHP and MySQL
  • JavaScript
    • jQuery & AJAX
  • WordPress
  • SEO
  • Web Hosting
  • Comparison
No Result
View All Result
AllsWeb White Logo
No Result
View All Result
Home PHP and MySQL

Dynamic Image Creation with PHP

Create a Dynamic Image with PHP

Lakshika Mathur by Lakshika Mathur
December 21, 2019
Reading Time: 3 mins read
0
Create a Dynamic Image with PHP

The image functions of the GD library are the easiest way to generate a persuasive image with text in PHP. In some circumstances, it is essential to make an image on the fly and write dynamic text to the image. Therefore, if you want to generate a dynamic casual image with PHP. So, you have to install the GD library. In this tutorial, we will explain to you how to create a dynamic image with PHP.

RELATED POSTS

What is Application Programming Interface (APIs)?

Like Dislike Rating System with jQuery, Ajax, and PHP

Star Rating System with jQuery, Ajax, PHP, and MySQL

Our instance code will make a dynamic image with PHP grounded on the specified configuration. You can modify the image width, height, background color, text color, and text.

Create a Dynamic Image with PHP

The following code creates a random dynamic image using PHP. To create a dynamic image with PHP, we have to use the GD image functions.

  • imagecreate() – Creates a new image resource of specified size (width and hwight).
  • imagecolorallocate() – Allot a color for an image resource.
  • imagettfbbox() – Calculate the bounding box in pixels for TrueType text.
  • imagettftext() – Use TrueType fonts to write the text to the image.
  • imagepng() – Creates a PNG image.
  • imagedestroy() – Destroy an image resource.
// Specify font path
$font = 'fonts/verdana.ttf';

// Text font size
$font_size = 10;

// Get settings from URL
$setting = isset($_GET['s']) ? $_GET['s'] : "000_FFF_350_350";
$setting = explode("_", $setting);

$img = array();

// Define image width, height, and color
switch($n = count($setting)){
    case $n > 4 :
    case 3:
        $setting[3] = $setting[2];
    case 4:
        $img['width'] = (int) $setting[2];
        $img['height'] = (int) $setting[3];
    case 2:
        $img['background'] = $setting[0];
        $img['color'] = $setting[1];
        break;
    default:
        list($img['background'], $img['color'], $img['width'], $img['height']) = array('F', '0', 100, 100);
        break;
}
$background = explode(",",hex2rgb($img['background']));
$textColorRgb = explode(",",hex2rgb($img['color']));
$width = empty($img['width']) ? 100 : $img['width'];
$height = empty($img['height']) ? 100 : $img['height'];

// Get text from URL
$text = (string) isset($_GET['t']) ? urldecode($_GET['t']) : $width ." x ". $height;

// Create the image resource 
$image = @imagecreate($width, $height) or die("Cannot Initialize new GD image stream");

// Create image background
$background_color = imagecolorallocate($image, $background[0], $background[1], $background[2]);

// Grab the width & height of the text box
$bounding_box_size = imagettfbbox($font_size, 0, $font, $text);
$text_width = $bounding_box_size[2] - $bounding_box_size[0];
$text_height = $bounding_box_size[7]-$bounding_box_size[1];

// Text x&y coordinates
$x = ceil(($width - $text_width) / 2);
$y = ceil(($height - $text_height) / 2);

// Define text color
$text_color = imagecolorallocate($image, $textColorRgb[0], $textColorRgb[1], $textColorRgb[2]);

// Write text to image
imagettftext($image, $font_size, 0, $x, $y, $text_color, $font, $text);

// Set the content type header - in this case image/png
header('Content-Type: image/png');

// Output the image
imagepng($image);

// Free up memory
imagedestroy($image);

// Convert color code to rgb
function hex2rgb($hex) {
    $hex = str_replace("#", "", $hex);

    switch(strlen($hex)){
        case 1:
            $hex = $hex.$hex;
        case 2:
            $r = hexdec($hex);
            $g = hexdec($hex);
            $b = hexdec($hex);
            break;
        case 3:
            $r = hexdec(substr($hex,0,1).substr($hex,0,1));
            $g = hexdec(substr($hex,1,1).substr($hex,1,1));
            $b = hexdec(substr($hex,2,1).substr($hex,2,1));
            break;
        default:
            $r = hexdec(substr($hex,0,2));
            $g = hexdec(substr($hex,2,2));
            $b = hexdec(substr($hex,4,2));
            break;
    }

    $rgb = array($r, $g, $b);
    return implode(",", $rgb); 
}

SEE ALSO: Upload and Add Watermark to Image using PHP

Configure Options

  • s – Specify the image background color, width, foreground color, and height in the query string. And separate the setting by an underscore (_). (For example, 000_FFF_350_350)
  • t – Specify the text that you want to write on the image. By default, Write the image width and height over the image.

Usage

Stipulate the URL of the dynamic image generation script with the configuration option in the src tag.

<img src="create_image.php?s=000_FFF_350_350&t=Dynamic Image" >

SEE ALSO: Preview and Rotate Image Before Upload using jQuery and PHP

Conclusion

Firstly, the dynamic image creation functionality is beneficial when you want to create a random photo on the fly for the Captcha code. Secondly, if you’re going to make the image transparent, don’t use the background colour. And at last, you can quickly spread our dynamic image generation code functionality as per your requirements.

Also, read our previous blog- Upload Multiple Images and Store in Database using PHP and MySQL

Tags: Dynamic Image CreationPHP
ShareTweetSendShareSharePinScan
Lakshika Mathur

Lakshika Mathur

Related Posts

What is Application Programming Interface (APIs), Types, and Importance.
PHP and MySQL

What is Application Programming Interface (APIs)?

January 29, 2022
61
Like Dislike Rating System with jQuery, Ajax, and PHP
jQuery & AJAX

Like Dislike Rating System with jQuery, Ajax, and PHP

January 6, 2020
739
Star Rating System with jQuery, Ajax, PHP, and MySQL
jQuery & AJAX

Star Rating System with jQuery, Ajax, PHP, and MySQL

January 6, 2020
162
How to Force Download File in PHP
PHP and MySQL

How to Force Download File in PHP

January 2, 2020
82
How to Connect to the Remote MySQL Database using PHP
PHP and MySQL

How to Connect to the Remote MySQL Database using PHP

January 1, 2020
28
How to Generate QR Code with PHP using Google Chart API
PHP and MySQL

How to Generate QR Code with PHP using Google Chart API

January 1, 2020
83
Next Post
Preview and Rotate Image Before Upload using jQuery and PHP

Preview and Rotate Image Before Upload using jQuery and PHP

Upload and Add Watermark to Image using PHP

Upload and Add Watermark to Image using PHP

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • Comparison (3)
  • HTML & CSS (9)
  • Interesting Facts (1)
  • JavaScript (27)
    • jQuery & AJAX (18)
  • PHP and MySQL (48)
  • Security (10)
  • SEO (2)
  • Trademark (2)
  • Tutorials (5)
  • Uncategorized (1)
  • Web Hosting (19)
    • VPS Server (5)
  • WordPress (8)

Recent Posts

  • Is the Trademark valuable to your Brand or domain?
  • Ideas For Ten Fantastic Online Business From Home
  • Some best free WordPress Themes for Affiliate Marketing Websites
  • Home
  • Posts
  • Privacy Policy
  • Terms and Conditions

Built and Maintained With ♥ by AllsWeb Team

No Result
View All Result
  • Home
  • Main Home
  • PHP and MySQL
  • JavaScript
    • jQuery & AJAX
  • WordPress
  • SEO
  • Web Hosting
  • Comparison

Built and Maintained With ♥ by AllsWeb Team

Go to mobile version