• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 457
  • Last Modified:

GD + .htaccess + Dynamic filename help


I created an image using GD and the file name is test.php I renamed it to test.png and with .htaccess help its working just fine.

Now I want instead of test.png just the test # .. so when I request 345.png for example it will use the file name "345" to get the test info from my MySQL database.

I attached my current code below.

Again, the using the test # to get the info from MySQL is easy all what I need is when I call any .png it will use the file name as the test # .. So if I used 4553.png it should use 4553 as the test # to get the info from my database and etc..

The hard part for me is how I can get it to use any # entered as the file name even if I called 3333333333333.png ?

PS: The current test file is named test.php (which I attached below) but if I renamed to "test.png" the image works just fine.

Thank you so much for your time and efforts.
header ("Content-type: image/png");
$date	=	("13 Sep 2010");
$speed = 	("223");
$handle = ImageCreate (250, 125) or die ("Cannot Create image");
$bg_color = ImageColorAllocate ($handle, 0, 0, 0);
$txt_color = ImageColorAllocate ($handle, 127, 255, 0);
$line_color = ImageColorAllocate ($handle, 255, 255, 255);
ImageLine($handle, 0, 27, 250, 27, $line_color); 
ImageTTFText ($handle, 13, 0, 3, 18, $txt_color, "/Fonts/BI.ttf", "MyWebsite.com");
ImageTTFText ($handle, 10, 0, 150, 18, $txt_color, "/Fonts/N.ttf", $date);
ImageTTFText ($handle, 11, 0, 6, 50, $txt_color, "/Fonts/B.ttf", "Typing Speed:");
ImageTTFText ($handle, 11, 0, 100, 50, $txt_color, "/Fonts/N.ttf", $speed);
ImagePng ($handle);

Open in new window

1 Solution
there is two part of solution

1. in .htaccess you have to create a rewrite role which convert #.png into test.php?image=# example given below
2. in test.php use store value of image into some variable and use it accordingly.
# in .htaccess add following code

RewriteEngine On
RewriteRule   ^([0-9]+).png$  test.php?image=$1

// and in test.php

$image_no = $_GET['image'];

// now use can use $image_no in database query etc...

Open in new window

You have to also make sure that $_GET['image'] is safe. For instance check if it's numeric:

if (isset($_GET['image']) && is_numeric($_GET['image'])) {
   $image_no = $_GET['image'];

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now