lip9000 Posted May 1, 2009 Share Posted May 1, 2009 I have a script that generates a thumbnail in real time from a larger image, and it also compresses the larger image when that it showed as well. Images are called on the page like so: <img src="http://www.mylol.net/getphoto.php?mini&id=71925"> (id being the user ID) The problem is that because the source isn't the image directly, images aren't being cached, so when the page refreshes it has to reload every single image through the php script and resize and compress them all, and this is eating up a lot of server resources. Is there a way to edit this script so that it perhaps saves a cached version of that image into a folder on the webserver called 'cached' and looks for it next time the page loads? But also if there is a difference (say the user updates the photo) then it will reload the new image instead of the old cached version? Here is the script, MANY THANKS IN ADVANCE! <?php ob_start();session_start(); include_once 'inc/config.php'; include_once 'inc/func.general.php'; //INIT MYSQL $connection=mysql_connect(SQL_SERVER,SQL_USER,SQL_PASSWORD) or die('Cannot connect to mysql: '.mysql_error()); $result=mysql_select_db(SQL_DATABASE, $connection) or die('Cannot open mysql database: '.mysql_error()); if ((! isset($_GET['id']))&&(! isset($_GET['gid']))) {die('UNKNOWN PICTURE ID!');} if ((int)$_SESSION['uid']>0) { $query='SELECT id FROM lol_users WHERE (isadmin=1) AND (id='.(int)$_SESSION['uid'].')'; $qres=mysql_query($query) or die('MYSQL ERROR: '.mysql_error()); if (mysql_fetch_array($qres)) {define('GUEST_ISADMIN',true);} } define('GUEST_ISADMIN',false); if ($_GET['id']==-1) {define('USERBANNED',0);} elseif ($_GET['id']==-2) {define('USERBANNED',1);} if (isset($_GET['id'])&&(!defined('USERBANNED'))) { $query='SELECT fname,uid FROM lol_photos WHERE pid='.(int)$_GET['id']; $qres=mysql_query($query) or die('MYSQL ERROR: '.mysql_error()); $filename=mysql_fetch_array($qres); $ownerid=$filename['uid']; $filename=$filename['fname']; $query='SELECT validated,isex FROM lol_users WHERE id='.(int)$ownerid; $qres=mysql_query($query) or die('MYSQL ERROR: '.mysql_error()); $userinfo=mysql_fetch_array($qres); if (((int)$userinfo['validated']<1)&&(!GUEST_ISADMIN)) {define('USERBANNED',(int)$userinfo['isex']);} $ofilename='images/'.USER_PICT_DIR.'/'.$filename.'.jpg'; $mfilename='images/'.USER_PICT_DIR.'/mini/'.$filename.'.png'; } elseif (isset($_GET['gid'])) { $query='SELECT pname FROM lol_groups WHERE gid='.(int)$_GET['gid']; $qres=mysql_query($query) or die('MYSQL ERROR: '.mysql_error()); $filename=mysql_fetch_array($qres);$filename=$filename['pname']; $ofilename='images/'.GROUP_PICT_DIR.'/'.$filename.'.png'; } if (isset($_GET['mini'])&&(!defined('USERBANNED'))) { if (! file_exists($ofilename)) {die('PICTURE NOT FOUND!');} header('Content-type: image/png'); if (! file_exists($mfilename)) { list($width,$height)=getimagesize($ofilename); $new_width=100;$new_height=100; $new_width=$width;$new_height=$height; $ratio=$width/$height; if ($new_height>100) {$new_height=100;$new_width=$new_height*$ratio;} if ($new_width>100) {$new_width=100;$new_height=$new_width/$ratio;} $image2=imagecreatetruecolor($new_width,$new_height); $image =imagecreatefromjpeg($ofilename); imagecopyresampled($image2,$image,0,0,0,0,$new_width,$new_height,$width,$height); //imagepng($image2,$mfilename); imagejpeg($image2,null,65); //imagepng($image2); } else { $image=imagecreatefrompng($mfilename); imagejpeg($image,null,65); //imagepng($image); } } elseif (isset($_GET['id'])&&(!defined('USERBANNED'))) { if (! file_exists($ofilename)) {die('PICTURE NOT FOUND!');} header('Content-type: image/jpeg'); if (! sesflood_check((int)$_GET['pid'],'photov1')) { sesflood_check((int)$_GET['pid'],'photov1'); $query='UPDATE lol_photos SET views=views+1 WHERE pid='.(int)$_GET['id']; $qres=mysql_query($query) or die('MYSQL ERROR: '.mysql_error()); } $image=imagecreatefromjpeg($ofilename); imagejpeg($image,null,61); } elseif (isset($_GET['gid'])) { header('Content-type: image/png'); $image=imagecreatefrompng($ofilename); imagepng($image); } elseif (defined('USERBANNED')&&(USERBANNED==0)) { $image=imagecreatefromgif('images/'.USER_PICT_DIR.'/unknown_male.gif'); header('Content-type: image/gif'); imagegif($image);die(); } elseif (defined('USERBANNED')&&(USERBANNED==1)) { $image=imagecreatefromgif('images/'.USER_PICT_DIR.'/unknown_female.gif'); header('Content-type: image/gif'); imagegif($image);die(); } else {die('PICTURE NOT FOUND!');} ob_end_flush(); ?> The main part is about halfway down the script, the GET[mini] is for loading thumbnails. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted May 1, 2009 Share Posted May 1, 2009 You should store the thumbnail image as a file so that it does not need to be produced each time it is requested. If the thumbnail file exists, read and output the file. If it does not exist when it is requested, create it, store it as a file so that the next time it is requested it will exist, and also output it. Quote Link to comment Share on other sites More sharing options...
lip9000 Posted May 1, 2009 Author Share Posted May 1, 2009 thats exactly what i was thinking, but how would i go about doing so? what functions and where about in my script would i do this? I'm not familiar with the code needed to save files and such. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted May 1, 2009 Share Posted May 1, 2009 For any request for an image, use http://php.net/file_exists to test if it already exists. If it does, simply read it and output the correct content-type header followed by the image data (the readfile command would come in handy.) If the requested file does not exist, the second parameter in the imagexxx() functions allow you to create a file from the resulting image - http://php.net/imagegif 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.