A PHP-based tool for generating customized text images on-the-fly. Create dynamic social media graphics, placeholders, or text visuals with custom text, font size, colors, and dimensions via URL parameters.
- Dynamic text placement with automatic centering
- Customizable background and font colors
- Adjustable image and font sizes
- Automatic text slugification and capitalization
- SEO-friendly URLs with Apache mod_rewrite
- PHP 7.0 or higher
- GD Library
- Apache web server with mod_rewrite enabled
- A TrueType font file (Arial Bold used in this example)
- Clone this repository to your web server:
git clone https://github.com/hlotiim/Dynamic-Placeholder-Image-Generator.git - Ensure your web server is configured to run PHP scripts.
- Update the font file path in the
script.phpscript:$fontFile = __DIR__ . 'arial.ttf';
- Create or update your
.htaccessfile in the root directory of your website with the following rewrite rule:RewriteEngine On RewriteRule ^images/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)\.png$ script.php?size=$1&bg=$2&fontcolor=$3&sizeandname=$4&text=$5 [L]
Access the script with the following URL format:
/images/{size}/{bg}/{fontcolor}/{sizeandname}/{text}.png
-
size: Font size (integer)- Example:
120 - This determines the base font size before scaling
- Example:
-
bg: Background color (6-digit hex code without #)- Example:
f67280for a light pink background - You can use any valid hex color code
- Example:
-
fontcolor: Text color (6-digit hex code without #)- Example:
fffffffor white text - You can use any valid hex color code
- Example:
-
sizeandname: Image dimensions in format WidthxHeight- Example:
1200x900 - This sets the overall image size in pixels
- Example:
-
text: The text to display on the image- Use hyphens instead of spaces
- The script will automatically capitalize words and wrap long text
https://yourdomain.com/images/120/f67280/ffffff/1200x900/hello-world.png
This will generate a 1200x900 image with a pink background (#f67280), white text (#ffffff), a base font size of 120, and the text "Hello World" centered on the image.
You can customize the script.php script to modify various aspects of the image generation:
- Font: Change the
$fontFilevariable to use a different TrueType font. - Text Wrapping: Modify the
wordwrap()function call to change how long text is wrapped. - Scaling: Adjust the
$scalingFactorcalculation to change how the font size scales with image size.
You can extend the script to add new features, such as:
- Additional text styles (bold, italic, etc.)
- Multiple text lines
- Image overlays or watermarks
- Different text alignments (left, right, etc.)
- If you see a 404 error, ensure your Apache mod_rewrite is enabled and your .htaccess file is correctly placed and readable.
- If the image doesn't generate, check your PHP error logs and ensure the GD library is installed and enabled.
- If the font doesn't render correctly, verify the path to your font file is correct and the file is readable by the web server.
- This script doesn't include input sanitization. In a production environment, you should validate and sanitize all input parameters.
- Consider adding rate limiting to prevent abuse of the image generation service.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is open source and available under the MIT License.