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", "");
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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.