MyBB Community Forums

Full Version: A PHP Image question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I have a dynamic sig setup for my site, i used a tutorial to write the script.

You currently write:
[img]www.gfx-core.co.cc/statsig.php[/img]
To get the image.


This points to the file statsig.png in the website root.

How can i get it so that i can choose from multiple images with a code something like:
www.gfx-core.cc/statsig.php?imageid=1
or
www.gfx-core.cc/statsig.php?imageid=2
whilst only writing 1 script, so the imageID determines which image was used. ID 1 points to one image and ID2 points to a different image (statsig2.png for example)


Thanks in advanced Smile
You'd need to put something like this in the file:

$imageid = $_GET['imageid']

And then use that variable in the script to show a different images based on that.
I probably would end up using something like the following:
// Checking if the variable exists is good practice...
if(isset($_GET['imageid']))
{
  // Remove the intval() if you want the user to be able
  // to use something other than a number as the id
  $imageid = intval($_GET['imageid']);
}

// $imageid is not set if the user didn't pass ?imageid=something
// or 0 if the intval() encountered a word instead of a number
if(!isset($imageid) or $imageid == 0)
{
  // Change this if you want something else as the default image id
  $imageid = 1;
}

$filename = "statsig" . $imageid . ".png";
// Use $filename from here on
That's a better way of doing it.

What I can't figure out how to do (sorry to hijack the thread Toungue) is get the URL of the page the image is being loaded on... so if it's in a signature and you view the profile, you can record www.example.com/member.php?action=profile&uid=1... all I can do is get the URL of the image script itself which isn't what I want.
(2009-09-10, 08:51 PM)Firestryke31 Wrote: [ -> ]I probably would end up using something like the following:
// Checking if the variable exists is good practice...
if(isset($_GET['imageid']))
{
  // Remove the intval() if you want the user to be able
  // to use something other than a number as the id
  $imageid = intval($_GET['imageid']);
}

// $imageid is not set if the user didn't pass ?imageid=something
// or 0 if the intval() encountered a word instead of a number
if(!isset($imageid) or $imageid == 0)
{
  // Change this if you want something else as the default image id
  $imageid = 1;
}

$filename = "statsig" . $imageid . ".png";
// Use $filename from here on
How does it know which ID is which image?

I have this so far:
$stats = $cache->read("stats");

Header ("Content-type: image/png");
$img_handle = imageCreateFromPNG("./images/statsig.png");
$imgcolor = ImageColorAllocate ($img_handle, 153, 153, 153);
Where would i insert this code?
Can't you use $_SERVER['HTTP_REFERER']?
(2009-09-10, 09:24 PM)Martin M. Wrote: [ -> ]Can't you use $_SERVER['HTTP_REFERER']?

Shy I need to look at those variables more.
(2009-09-10, 09:24 PM)tommykent1210 Wrote: [ -> ]How does it know which ID is which image?

I have this so far:
$stats = $cache->read("stats");

Header ("Content-type: image/png");
$img_handle = imageCreateFromPNG("./images/statsig.png");
$imgcolor = ImageColorAllocate ($img_handle, 153, 153, 153);
Where would i insert this code?

It generates the file name from the imageid, so for example statsig.php?imageid=1 would use the file "statsig1.png". It basically tags the ID passed by the user on to the end of the file name.

You would place the code anywhere before the line
$img_handle = imageCreateFromPNG("./images/statsig.png");
and change that line to this:
$img_handle = imageCreateFromPNG("./images/" . $filename);

The only thing that you would have to change is the name of the current statsig.png to statsig1.png.
require "./inc/init.php";

 $stats = $cache->read("stats");

Header ("Content-type: image/png");
if(isset($_GET['imageid']))
{
  $imageid = intval($_GET['imageid']);
}

if(!isset($imageid) or $imageid == 0)
{
  $imageid = 1;
}

$filename = "statsig" . $imageid . ".png";
$img_handle = imageCreateFromPNG("./images/" . $filename); 
$imgcolor = ImageColorAllocate ($img_handle, 153, 153, 153);
$imgcolorhead = ImageColorAllocate ($img_handle, 255, 255, 255);
ImageString ($img_handle, 3, 10, 8,  $mybb->settings['homename']." : ", $imgcolorhead);
ImageString ($img_handle, 3, 10, 21,  $mybb->settings['homeurl'], $imgcolorhead);
ImageString ($img_handle, 2, 10, 33,  "Threads: ". my_number_format($stats['numthreads']), $imgcolor);
ImageString ($img_handle, 2, 10, 47,  "Posts: ". my_number_format($stats['numposts']), $imgcolor);
ImageString ($img_handle, 2, 10, 60,  "Members: ". my_number_format($stats['numusers']), $imgcolor);
ImagePng ($img_handle);
ImageDestroy ($img_handle);

?>

Like this? It just keeps giving me an error
"The image “http://www.gfx-core.co.cc/statsig2.php?imageid=1” cannot be displayed, because it contains errors."
You don't seem to have a file called "images/statsig1.png"

Name one of your images this (probably currently statsig.png) and it should work. I noticed that "http://www.gfx-core.co.cc/statsig2.php?imageid=2" does work, so the problem is the file name.

After "imageCreateFromPNG("./images/" . $filename);" you should also add this:
if($img_handle == false)
  $img_handle = imageCreateFromPNG("./images/statsig1.png");

This will make it so that if a user tries to access an image that doesn't exist, they'll get images/statsig1.png by default instead of an error.
Pages: 1 2