Jump to content

Load PHP image faster or cache


KingOfHeart

Recommended Posts

<?php
$img = $_GET['img'];
$wid = $_GET['wid'];
$hei = $_GET['hei'];
$xoff = $_GET['xoff'];
$yoff = $_GET['yoff'];

if($img == 0)
return;

$image = array("HyruleTown","Bosses","ForestNPCs","RoyalFamily","Rings");


header("Content-type: image/png");
$im = imagecreatefrompng($image[$img - 1] . ".png");
$dest = imagecreatetruecolor($wid, $hei);
imagecopy($dest, $im, 0, 0, $xoff, $yoff, $wid, $hei);

imagepng($dest);
imagedestroy($dest);
imagedestroy($im);
?>

This script allows you to take an image and only show parts of it. This script works great, but there's one problem. As I add more and more to a page, it takes longer and longer to load.

 

http://openzelda.thegaminguniverse.com/wiki/index.php5?title=OZWiki:Alien_Invasion

 

There will be a lot more php images added soon. I used this method so I don't have to upload a ton of images at once.

So is there a way to speed things up? Is it possible to have it save cache so the images don't reload everytime you refresh the page?

Link to comment
Share on other sites

The imagepng() function can take a second argument- a filename to save the output image to, rather than outputting to the browser.

 

You could use this at the end to save the output image to a cache directory (eg. with a filename of the form "sheet-xoff-yoff-width-height.png", so "Hyruletown-20-70-100-100.png"), then next time that image is requested check to see if the image exists in your cache and if it does, then just output the saved image rather than doing all the opening / image manipulation / copying etc. that you're currently doing.

 

 

If I was doing this, I'd actually have an admin "createcache.php" script which would generate all of the possible cached images from the sprite-sheets, then in your page that users would see you could just have normal img tags to the cached images (eg. <img src="cached/Hyruletown-20-70-100-100.png">). This would mean that it's only one static image call for each image, which is much faster to serve than including PHP in any way.

 

In both cases, you'd have to remember to clear the cache directory (and run createcache.php again) after you uploaded new sprite sheets to stop the old cached versions being displayed.

 

Alternatively, just edit / upload the individual sprite images directly rather than using sprite-sheets (what benefit do you get from using these?)

Link to comment
Share on other sites

Did I make a mistake?

 

<?php
$img = $_GET['img'];
$wid = $_GET['wid'];
$hei = $_GET['hei'];
$xoff = $_GET['xoff'];
$yoff = $_GET['yoff'];

if($img == 0)
return;

$image = array("HyruleTown","Bosses","ForestNPCs","RoyalFamily","Rings");
$out = "Catched-Images/".$image[$img-1].$xoff.$yoff.$wid.$hei.".png";

if(file_exists($out))
{
readfile($out);
return;
}	


header("Content-type: image/png");
$im = imagecreatefrompng($image[$img - 1] . ".png");
$dest = imagecreatetruecolor($wid, $hei);
imagecopy($dest, $im, 0, 0, $xoff, $yoff, $wid, $hei);
imagepng($dest,$im);
imagepng($dest,$out);
imagedestroy($dest);
imagedestroy($im);
?>

 

So far it's creating the images but the fileexists isn't working. Guessing the two strings don't match up or something.

Link to comment
Share on other sites

I think maybe you still need the line:

header("Content-type: image/png");

before you do the readfile().

 

If that doesn't work, try to debug and see where it's failing by visiting the script in your browser and seeing what you get sent (ie. visit the page for that script, then right-click -> view source).

Link to comment
Share on other sites

I got it working.

 

function Recatch()
{
foreach (glob("Catched-Images/*.png") as $file) { //recatch
	unlink($file);
}	
}

$img = $_GET['img'];
$wid = $_GET['wid'];
$hei = $_GET['hei'];
$xoff = $_GET['xoff'];
$yoff = $_GET['yoff'];

if($img == 0)
return;	

$image = array("HyruleTown","Bosses","ForestNPCs","RoyalFamily","Rings");
$out = "Catched-Images/".$image[$img-1].$xoff.$yoff.$wid.$hei.".png";
header("Content-type: image/png");
if(file_exists($out))
{
readfile($out);
return;
}
$im = imagecreatefrompng($image[$img - 1] . ".png");
$dest = imagecreatetruecolor($wid, $hei);
imagecopy($dest, $im, 0, 0, $xoff, $yoff, $wid, $hei);
imagepng($dest,$im);
imagepng($dest,$out);
imagedestroy($dest);
?>

 

I threw in a function to delete all files in the directory, but it's only there if I ever need it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.