Skip to content

Instantly share code, notes, and snippets.

@researcx
Created July 24, 2023 08:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save researcx/5fda54c4406b8cdd0328a9bc8e1fbab1 to your computer and use it in GitHub Desktop.
Save researcx/5fda54c4406b8cdd0328a9bc8e1fbab1 to your computer and use it in GitHub Desktop.

Embeddable dynamic images:

PHP:

NGINX line:

location /example.png {
	include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}

PHP code to generate an image:

<?php
$image = imagecreate(500, 300);
$background_color = imagecolorallocate($image, 0, 153, 0);
$text_color = imagecolorallocate($image, 255, 255, 255);
imagestring($image, 5, 180, 100, $_SERVER['REMOTE_ADDR'], $text_color);
imagestring($image, 3, 160, 120, $_SERVER['HTTP_USER_AGENT'], $text_color);
  
header("Content-Type: image/png");
  
imagepng($image);
imagedestroy($image);
?>

(save as example.png to your webroot)

Python+Flask:

NGINX line:

location /example.png {
	proxy_pass http://127.0.0.1:5000/;
}

Python+Flask route to generate an image:

from flask import Flask, Response

app = Flask(__name__)

@app.route('/example.png')
def generate_image():
	image = 'stuff' # stuff
	return Response(image, mimetype='image/png')

if __name__ == '__main__':
    app.run(debug=True)

The code above will dynamically render an image that displays the viewer's IP address and user-agent, whilst still providing an embeddable image type that doesn't get picked up by image type validation on website or forums. It also provides the correct mime-type to be a png.

Image redirects:

NGINX lines:

	location /funny_picture.jpg {
		# make them run gmod
		rewrite /funny_picture.jpg steam://launch/4000 permanent;
	}
	location /funnier_picture.jpg {
		# make them join my gmod server
		rewrite /funnier_picture.jpg steam://connect/42.51.12.172:27015 permanent;
	location /send_mail.jpg {
		# send an email to the google admin
		rewrite /send_mail.jpg mailto:admin@google.com?subject=Hi&body=Hello! permanent;
	}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment