Change what Image is saved at a certain URL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
goodtimeshaxor
Forum Newbie
Posts: 5
Joined: Wed Jun 06, 2007 11:36 pm

Change what Image is saved at a certain URL

Post by goodtimeshaxor »

The best way to explain what I need to do is to give an example and here it is:

Say you have a site URL www. website. com/images/banner.gif
and you want the image stored at that URL to be randomly selected from a pool of 6 images:

/banner1.gif
/banner2.gif
/banner3.gif
/banner4.gif
/banner5.gif
/banner6.gif

When a viewer goes to www. website. com/images/banner.gif, one of the 6 banner images would be displayed at random. I know there are scripts out there that allow you to change which image is shown on an html or php page but I need something that will change what image is actually stored at a specific location on my web server.

I'm a beginner in PHP and cannot code this myself -_-;; Wondering if anyone could help me out with this.

Thanks in advance
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Examples of this are fairly abundant. :?

Code: Select all

<?php

$image = 'example.jpg';

$im = imagecreatefromjpeg( $image );

ob_start();
imagejpeg($im, '', 100);
$len = ob_get_length();
$imagedata = ob_get_clean();

header( 'Content-type: image/jpeg' );
header( 'Content-length: ' . $len );

echo $imagedata;

?>
or

Code: Select all

<?php

$thursday = (date('w') == 4);

if($thursday or isset($_GET['pirate']))
{
	$file = 'avatar_sized_pirate.png';
}
else
{
	$file = 'avatar_sized.png';
}

header('Content-type: image/png');
header('Content-length: ' . filesize($file) );
readfile($file);

?>
aided by an .htaccess

Code: Select all

AddType application/x-httpd-php .gif .png .jpg .jpeg
yieldsImageorImage
goodtimeshaxor
Forum Newbie
Posts: 5
Joined: Wed Jun 06, 2007 11:36 pm

Post by goodtimeshaxor »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hmm. I can't seem to get it to work and I don't exactly understand how it works either.

I have my images saved to:
http://www.l2luna.com/banner/banner.gif
http://www.l2luna.com/banner/banner1.gif
http://www.l2luna.com/banner/banner2.gif

and I have the first php code you gave to:
http://www.l2luna.com/banner/banner.php

Code: Select all

<?php

$image = 'banner.gif';

$im = imagecreatefromgif( $image );

ob_start();
imagegif($im, '', 2);
$len = ob_get_length();
$imagedata = ob_get_clean();

header( 'Content-type: image/gif' );
header( 'Content-length: ' . $len );

echo $imagedata;

?>
Some info: The image stored in banner.gif can be random or changed by time (in minutes). Not sure if that will affect the script. Those 4 files are all I have in side directory on the site.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
goodtimeshaxor
Forum Newbie
Posts: 5
Joined: Wed Jun 06, 2007 11:36 pm

Post by goodtimeshaxor »

Still don't know whats wrong.. Anyone out there? =)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

imagegif() only accepts two parameters. Your call has three. What is the third one supposed to be?

Also, if you're not going to manipulate the images, you don't need to use the image functions.. you can simply pull in the file data using file_Get_contents() or one of its equivalents.
goodtimeshaxor
Forum Newbie
Posts: 5
Joined: Wed Jun 06, 2007 11:36 pm

Post by goodtimeshaxor »

Ah. I just copied the PHP code that you showed me and assumed that the 100 meant how many images it ran through. I guess I assumed wrong (I should look at the syntax first before doing that).

Take a look at this directory:
http://www.l2revengeserver.com/banner/

I'm looking to do something like this:
http://www.l2revengeserver.com/banner/banner.gif

It should change every couple minutes.

(Again, thank you for your help. I appreciate it)
Post Reply