Page 1 of 1

A little changeable picture uses php guide

Posted: Wed May 15, 2019 11:55 am
by Echo Dino
probably you're using sig.grumpybumpers.com for having random images at every refresh. if you're hosting php supporter site you're able to do similar to this :D.


i'm using this three images(1,2,3): (probably gave you broken. visit the site to fix :) )

Image,Image
Image



and lets change them with every refresh! use that:

Code: Select all

<?php 

//variable

$id=mt_rand(0,2);
//Tell the browser that we are sending it a PNG image to display 
header("Content-Type: image/png"); 

//Echo with variables!
if ($id==0){
//Echo the contents of the image file called "dogbaby.png" to send it to the browser 
readfile("1.png"); 
}
elseif ($id==1){
//Echo the contents of the image file called "dogbaby.png" to send it to the browser 
readfile("2.png"); 
}
elseif ($id==2){
//Echo the contents of the image file called "dogbaby.png" to send it to the browser 
readfile("3.png"); 
}
?>
name that file to refreshimage.php
and our output is : (refresh to see another)
Image

Code: Select all

(make sure to put the link betwwen [img] and [/img]0



the "mt_rand(0,yournumber)" allows you to have random value between "0" and "yournumber" (if you choose 0 and 12 or anything. that will give one more (13)

Re: A little changeable picture uses php guide

Posted: Wed May 15, 2019 12:01 pm
by KevC
Other than looking like an advert, what is this for?

Re: A little changeable picture uses php guide

Posted: Wed May 15, 2019 12:44 pm
by Echo Dino
KevC wrote:
Wed May 15, 2019 12:01 pm
Other than looking like an advert, what is this for?


it's a little thing for making pictures change with every refresh. i'm trying to make it with another pics without my hoster site :) .

Re: A little changeable picture uses php guide

Posted: Wed May 15, 2019 12:51 pm
by KevC
What pictures where?
Your post makes no sense at all.

Re: A little changeable picture uses php guide

Posted: Wed May 15, 2019 12:55 pm
by Echo Dino
KevC wrote:
Wed May 15, 2019 12:51 pm
What pictures where?
Your post makes no sense at all.
that should be solved right now?

Re: A little changeable picture uses php guide

Posted: Wed May 15, 2019 2:21 pm
by Mannix_
KevC wrote:
Wed May 15, 2019 12:51 pm
What pictures where?
Your post makes no sense at all.
Every time your refresh this topic the image at the bottom of the first post changes

Re: A little changeable picture uses php guide

Posted: Thu May 16, 2019 6:56 am
by AmigoJack
Echo Dino wrote:
Wed May 15, 2019 11:55 am
use that:
This tiny code alone oozes of redunancy. One principle when coding is DRY. One could easily make your code more efficient and flexible this way:

Code: Select all

<?php 

header( 'Content-Type: image/png' ); 

$id= mt_rand( 0, 2 );
readfile( $id. '.png' );
However, this whole topic is not phpBB related, it's only PHP related.

Re: A little changeable picture uses php guide

Posted: Thu May 16, 2019 7:04 am
by Echo Dino

Code: Select all

<?php 

header( 'Content-Type: image/png' ); 

$id= mt_rand( 0, 2 );
readfile( $id. '.png' );


actually that doesn't work if file name not be number. but it's well code you've suggested :)

Re: A little changeable picture uses php guide

Posted: Thu May 16, 2019 4:31 pm
by Sniper_E
There is a better random image script you could use. It handles all images.

viewtopic.php?p=15253906#p15253236

Re: A little changeable picture uses php guide

Posted: Fri May 17, 2019 6:52 am
by AmigoJack
Sniper_E wrote:
Thu May 16, 2019 4:31 pm
better
handles all images
No and no: it sends no MIME header, is very inefficient (loops of regexes, will also match directories with that extension). And you can't synonym "all" for just 3 types.

Re: A little changeable picture uses php guide

Posted: Fri May 17, 2019 7:46 am
by Echo Dino
Sniper_E wrote:
Thu May 16, 2019 4:31 pm
There is a better random image script you could use. It handles all images.

viewtopic.php?p=15253906#p15253236


Isn't that very long? I got confused XD