Skip to content
This repository has been archived by the owner on Jul 18, 2023. It is now read-only.

Dynamic Dummy Image Generator — as seen on DummyImage.com

License

MIT, Apache-2.0 licenses found

Licenses found

MIT
LICENSE
Apache-2.0
LICENSE-RobotoMono
Notifications You must be signed in to change notification settings

jgauthi/PHP-Dummy-Image-Generator

 
 

Repository files navigation

Dynamic Dummy Image Generator
— as seen on DummyImage.com

This script enables you to create placeholder images in a breeze.

Usage

Just link to the index.php file via the HTML <img> element. For example, would create a PNG image with red (#f00) background, white (#ffffff) text, the word “Dummy” written on it and a size of 500px width, 250px height.

Classic usage:
<img src="folder-image/?size=500x250&type=png&bg=f00&color=ffffff&text=Dummy" alt="Dummy Image">

Image with specific default value: 
<img src="folder-image/?cfg=folder_thumb" alt="Dummy Image">

This script handles the following parameters, where basically all of them are optional.

  • size *(default: 640x480) — Examples:
    500x250 (= 500px width, 250px height)
    500 (= 500px square) *
  • type (default: png) — Examples:
    png (= PNG image)
    gif (= GIF image)
    jpeg or jpg (= JPEG image)
  • bg (default: 0099ff) — Examples:
    f00 (= #FF0000 as background color)
    FF0855 (= #FF0855 as background color)
  • color (default: FFFFFF) — Examples:
    000 (= #000000 as font color)
    FFFFFF (= #FFFFFF as font color)
  • text (default: {WidthOfTheImage}×{HightOfTheImage}) — Examples:
    Lore Ipsum (= Image has Lore Ipsum written on it)

All default value is set on config.ini, you can edit this file for your project, and add several parameters for different folders (you must create new section on ini file).

Htaccess Url Rules (apache)

Several options:

  1. Edition .htaccess project
  2. Symbolic link (ln -s /var/.../PHP-Dummy-Image-Generator/ project/images_folder)
  3. You can edit the apache site file for configure the relative url:
<VirtualHost *:8081>
	# ...

	Alias /images "/var/www/project_name/tools/PHP-Dummy-Image-Generator/index.php"
    <Directory "/var/www/project_name/tools/PHP-Dummy-Image-Generator">
		Options Indexes FollowSymLinks
		Require all granted
        FallbackResource /index.php
    </Directory>
</VirtualHost>

Auto-Image with custom name / size

If you use apache, a .htaccess is available with these rules:

IMG name files:
[text].[type]
[width]x[height].[type]
[text]-[width]x[height].[type]

Examples:

Classic usage on root folder:
<img src="folder-image/Hello_World.jpg" alt="Hello World"><br>
<img src="folder-image/300x800.jpg" alt="Custom size: 300x800"><br>
<img src="folder-image/Hello_World-200x150.jpg" alt="Hello World with custom size: 200x150">

Virtual support folder and sub folder with specific rules (optional)

You can use the .htaccess rules for support virtual folder and sub folder (IMG use precedents name convention):

[folder-cfg OR folder name]/[img]
[folder name]/[folder-cfg OR folder name]/[img]
[folder name]/[folder name]/[folder-cfg OR folder name]/[img]
[folder name]/[folder name]/[folder name]/[folder-cfg OR folder name]/[img]
Folder and Sub folder support (ini cfg support on last folder name):
<img src="folder-image/thumb/Lorem_Ipsu.jpg" alt="Lorem_Ipsu in thumb folder">
<img src="folder-image/any/virtual/folder/thumb/150x100.jpg" alt="150x100 in any/virtual/folder/thumb folder">

You can add specific parameters (size, bgcolor, etc) for a folder with a CFG configuration in config.ini file. Example (cfg rule "thumb"):

; Setting by default
[default]
;...

; Setting for folder with name "thumb"
[thumb]
size=150x100
bg_color=3322ff
text_color=ffffff
text_value=[WIDTH]×[HEIGHT]
type=jpg
font=RobotoMono-Regular.ttf

In Text parameter, you can use these variables:

  • [WIDTH], [HEIGHT]: Image values
  • [TYPE]: Image type (png, jpg)
  • [FILENAME]: Filename image

Url Rules and virtual folder with Local php-server

If you use local php-server, you can create a file router.php on your project on public/web folder (this file use your index.php), and edit this file for use it.

// Edit this const
const ROUTER_EXPREG = '#^(/images/)(film)?/?#i';
const FULLPATH_DUMMY_IMG_GENERATOR = '/path/to/PHP-Dummy-Image-Generator/index.php';

if (preg_match(ROUTER_EXPREG, $_SERVER['REQUEST_URI'], $extract)) {
    $_GET['cfg'] = $extract[2];
    // var_dump($extract);return true; // debug
    require_once FULLPATH_DUMMY_IMG_GENERATOR;
    return true;

} //...

You can add several condition for each url rules, add CFG folder on config.ini and edit the router template (for more details, look at Virtual support folder and sub folder with specific rules).
For use this router.php, you must launch for start the php-server:

cd root_project/public
# Or web folder, depending on your project
php -S localhost:8000 router.php

Url Rules and virtual folder with NGINX

On your project, you can add a new site configuration, with a specific domain (or specific virtual folder) who support image generator and virtual folder. On this file, you must edit the values: server_name (url domain), log location, root path to Dummy Image Generator, php-fpm usage.

If you use Docker-compose, docker-compose.yml example modification:

services:
  web:
    image: nginx:latest
    ports:
      - 80
    volumes:
      - ./local/path/to/Dummy-Image-Generator:/path/in/docker/to/Dummy-Image-Generator
      - ./local/path/to/nginx/sites:/etc/nginx/sites-enabled
      # ...
    links:
      - php
    labels:
      traefik.enable: 1
      traefik.http.port: 80
      traefik.http.frontend.rule: "HostRegexp:your-site.localhost.tv,static.localhost.tv"
      # ...

  php:
    image: php:8-fpm
    volumes:
      - ./local/path/to/Dummy-Image-Generator:/path/in/docker/to/Dummy-Image-Generator
      # ...

License & Credits

Please see the license file for more information.

Original idea by Russel Heimlich. When I first published this script, DummyImage.com was not Open Source, so I had to write a small script to replace the function on my own server.