Multi-Gaming Community
It is currently 27 Apr 2024, 19:40

All times are UTC+02:00




Post new topic  Reply to topic  [ 8 posts ] 
Author Message
ro 
PostPosted: 31 Mar 2010, 01:17 
Offline
The Necromancer (4970)
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?

_________________
War does not determine who is right - only who is left. - Bertrand Russell


Top
   
ro 
PostPosted: 31 Mar 2010, 04:16 
Offline
The Necromancer (4970)
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);
?>

_________________
War does not determine who is right - only who is left. - Bertrand Russell


Top
   
gb 
PostPosted: 31 Mar 2010, 21:03 
Offline
Has learned to write! (217)
[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?

_________________
Don't worry I got some silicon carrots - [SpA]Bucky O'Hare


Top
   
ro 
PostPosted: 31 Mar 2010, 21:10 
Offline
The Necromancer (4970)
Nah, .htaccess work just fine (except for some functions).

Anyways, it's sorted. ^^

_________________
War does not determine who is right - only who is left. - Bertrand Russell


Top
   
de 
PostPosted: 01 Apr 2010, 08:41 
Offline
Crap at posting (27)
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.


Top
   
ro 
PostPosted: 01 Apr 2010, 08:47 
Offline
The Necromancer (4970)
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?

_________________
War does not determine who is right - only who is left. - Bertrand Russell


Top
   
de 
PostPosted: 01 Apr 2010, 09:03 
Offline
Crap at posting (27)
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.


Top
   
ro 
PostPosted: 01 Apr 2010, 09:16 
Offline
The Necromancer (4970)
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

_________________
War does not determine who is right - only who is left. - Bertrand Russell


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 8 posts ] 

All times are UTC+02:00


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited