SpecialAttack.net
https://forum.specialattack.net/

Random Avatars via PHP (HALP!)
https://forum.specialattack.net/viewtopic.php?t=7150
Page 1 of 1

Author:  Lim-Dul [ 31 Mar 2010, 01:17 ]
Post subject:  Random Avatars via PHP (HALP!)

So, to all you PHP wizards out there, I have a question.

I've made a little script that will rotate avatars and disguise itself as a PNG (of course I have .htaccess with a rewrite rule for that as well) but for some reason it won't work on the forums. That is, it will correctly display the images if linked to as .php but not .png.

What's the problem?
Code:
<?php
header("Content-type: image/png"); 

$start = "1";
$end = "16";
$file_type = ".png";

$random = mt_rand($start, $end);
$image_name = $random . $file_type;

$newavatar = imagecreatefrompng($image_name);
imagepng($newavatar);
imagedestroy($newavatar); 
?>
Code:
[img]http://limdul.byethost11.com/Chibi_Bloodlines/random.php[/img]
Image
Code:
[img]http://limdul.byethost11.com/Chibi_Bloodlines/random.png[/img]
Image

:-(

P.S. It works as a link in the browser itself: http://limdul.byethost11.com/Chibi_Bloo ... random.png
P.P.S. Here's my .htaccess
Code:
RewriteEngine On
RewriteRule random.png random.php
P.P.P.S. A-ha! The script isn't redirected properly unless typed in manually in the browser. I guess I have to do something more with the rewrite engine?

Author:  Lim-Dul [ 31 Mar 2010, 04:16 ]
Post subject:  Re: Random Avatars via PHP (HALP!)

Mystery solved - the bad host didn't support image hotlinking. I wish I could have used mine but the server doesn't support URL rewriting for security reasons. :-P

Now I have a dynamically generated avatar which will be my MSPaint emblem 50% of the time and one of the Bloodline Champion Chibi avatars the other 50%. :-D
Code:
<?php
header('Content-type: image/png');

$start = "1";
$end = "16";
$ext = ".png";

$coinflip = mt_rand(0,1);

if ($coinflip == 0) {
	$random = mt_rand($start, $end);
	$image_name = $random . $ext;
	$newavatar = imagecreatefrompng($image_name);
} else {
	$newavatar = imagecreatefrompng('cod.png');
}

imagepng($newavatar);
imagedestroy($newavatar);
?>

Author:  [SpA]Frosty [ 31 Mar 2010, 21:03 ]
Post subject:  Re: Random Avatars via PHP (HALP!)

[SpA]Lim-Dul wrote:
I wish I could have used mine but the server doesn't support URL rewriting for security reasons. :-P
Never knew mod_rewrite was such a big security problem :P or you talking about them not allowing you .htaccess file?

Author:  Lim-Dul [ 31 Mar 2010, 21:10 ]
Post subject:  Re: Random Avatars via PHP (HALP!)

Nah, .htaccess work just fine (except for some functions).

Anyways, it's sorted. ^^

Author:  BigBadaBoom [ 01 Apr 2010, 08:41 ]
Post subject:  Re: Random Avatars via PHP (HALP!)

Do you plan on modifying the image?

If you're not then instead of creating an image in memory from png and then converting it to png and outputting it I'd suggest using
Code:
readfile ($image_name)
Reads a file and writes it to the output buffer.

Author:  Lim-Dul [ 01 Apr 2010, 08:47 ]
Post subject:  Re: Random Avatars via PHP (HALP!)

BigBadaBoom wrote:
Do you plan on modifying the image?

If you're not then instead of creating an image in memory from png and then converting it to png and outputting it I'd suggest using
Code:
readfile ($image_name)
Reads a file and writes it to the output buffer.
How would that affect the browser cache? I mean, wouldn't not recreating (and destroying) the image stop it from being updated on each request?

Author:  BigBadaBoom [ 01 Apr 2010, 09:03 ]
Post subject:  Re: Random Avatars via PHP (HALP!)

The browser doesn't see a difference. Its just how it works internally in the server.
Btw, you can effect the browser cache using the head:
Code:
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Some date in the past
This stops all browsers or any proxys (proxys like to cache to reduce traffic) from caching the image.

Author:  Lim-Dul [ 01 Apr 2010, 09:16 ]
Post subject:  Re: Random Avatars via PHP (HALP!)

BigBadaBoom wrote:
The browser doesn't see a difference. Its just how it works internally in the server.
Btw, you can effect the browser cache using the head:
Code:
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Some date in the past
This stops all browsers or any proxys (proxys like to cache to reduce traffic) from caching the image.
Thanks! That's the kind of expert advice I was looking for. I use PHP only on a need-to-know basis. =)

The updated code works like a charm even without cache-affecting header tags and while functionally speaking there's no difference it's certainly a more elegant way to do it - especially if I were to ever write a larger script and needed to optimize it for speed. :-D
Code:
<?php
header('Content-type: image/png');

$start = "1";
$end = "16";
$ext = ".png";

$coinflip = mt_rand(0,1);

if ($coinflip == 0) {
	$random = mt_rand($start, $end);
	$image_name = $random . $ext;
	readfile ($image_name);
} else {
	readfile ('cod.png');
}
?>
Image

Page 1 of 1 All times are UTC+01:00

Powered by phpBB® Forum Software © phpBB Limited