KingOfHeart Posted September 3, 2010 Share Posted September 3, 2010 <?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? Quote Link to comment Share on other sites More sharing options...
KingOfHeart Posted September 4, 2010 Author Share Posted September 4, 2010 Too complicated or did I lack some detail explaining what I wanted? Quote Link to comment Share on other sites More sharing options...
Crashthatch Posted September 4, 2010 Share Posted September 4, 2010 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?) Quote Link to comment Share on other sites More sharing options...
KingOfHeart Posted September 5, 2010 Author Share Posted September 5, 2010 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. Quote Link to comment Share on other sites More sharing options...
Crashthatch Posted September 5, 2010 Share Posted September 5, 2010 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). Quote Link to comment Share on other sites More sharing options...
KingOfHeart Posted September 6, 2010 Author Share Posted September 6, 2010 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.