33 users online (1 members and 32 guests)  


  Results 1 to 3 of 3

Related

  1. Replies: 8
  2. Loading problem using phpThumb    Forum: PHP Forum
    Replies: 0
  3. Replies: 0
  4. PHPthumb image generation trouble    Forum: PHP Forum
    Replies: 3
  1. #1
    James Hawkle's Avatar
    New User

    Status
    Offline
    Join Date
    Mar 2002
    Posts
    5

    PHP Help- Modified phpThumb script

    Ugg... I don't get it. I'm brand new to PHP, but from what I understand this code should work!

    Code:
    $tempfile = str_replace(array('/',' ','#','&',',',"\'"),array('_','%20','%23','%26','%2C','%27'), $_REQUEST['src']);
    $CachedFilename = $_SERVER['DOCUMENT_ROOT'].'/thumb_img/'.basename($tempfile);
    
    //if copying of this URL to the webserver directory fails...
    //												   spc  #   &   ,   \'
    if (!copy("http://waypoint-zero".str_replace(array(' ','#','&',',',"\'"),array('%20','%23','%26','%2C','%27'), $_REQUEST['src']), $_SERVER['DOCUMENT_ROOT'].'/thumb_img/cache/'.basename($tempfile)))
    {
    	ErrorImage('Failed Copy --'."\n".'  Escape Error'."\n"."\n".'Debug:'."\n".str_replace(array(' ','#','&',',',"\'"),array('%20','%23','%26','%2C','%27'), $_REQUEST['src']), 92, 92);
    	exit;
    }
    
    if (is_readable($CachedFilename))
    {
    	header('Content-type: image/jpeg');
    	header('Content-Length: '.filesize($CachedFilename));
    	if (!readfile($CachedFilename))
    	{
    		ErrorImage('Cache Error --'."\n".'  Cache Failed'."\n"."\n".'Debug:'."\n".$CachedFilename, 92, 92);
    	}
    	unlink ($_SERVER['DOCUMENT_ROOT'].'/thumb_img/cache/'.$tempfile);
    	exit;
    }
    I'm sorry its so messy. Here, this section of code works, but is super ineffiecient because it doesn't fully use the thumbnail caching system.

    The first 'if' statement checks to see if a file copy, "downloaded" from my webserver to a cache folder that is locally detectable by this script, is successful. If it fails, it displays an error image in place of where the thumbnail should be.

    The second 'if' statement checks for the cached thumbnail of the image that should be thumbnailed.

    Now, what I want to do is move the second 'if' above the first 'if'. This will keep it from wasting the time of copying the image to the cache when it won't even be used. Here's what I think should work without copying the file:

    Code:
    $tempfile = str_replace(array('/',' ','#','&',',',"\'"),array('_','%20','%23','%26','%2C','%27'), $_REQUEST['src']);
    $CachedFilename = $_SERVER['DOCUMENT_ROOT'].'/thumb_img/'.basename($tempfile);
    
    if (is_readable($CachedFilename))
    {
    	header('Content-type: image/jpeg');
    	header('Content-Length: '.filesize($CachedFilename));
    	if (!readfile($CachedFilename))
    	{
    		ErrorImage('Cache Error --'."\n".'  Cache Failed'."\n"."\n".'Debug:'."\n".$CachedFilename, 92, 92);
    	}
    	exit;
    }
    
    //if copying of this URL to the webserver directory fails...
    //												   spc  #   &   ,   \'
    if (!copy("http://waypoint-zero".str_replace(array(' ','#','&',',',"\'"),array('%20','%23','%26','%2C','%27'), $_REQUEST['src']), $_SERVER['DOCUMENT_ROOT'].'/thumb_img/cache/'.basename($tempfile)))
    {
    	ErrorImage('Failed Copy --'."\n".'  Escape Error'."\n"."\n".'Debug:'."\n".str_replace(array(' ','#','&',',',"\'"),array('%20','%23','%26','%2C','%27'), $_REQUEST['src']), 92, 92);
    	exit;
    }
    It works fine all the way up to 'if (!readfile($CachedFilename))'. It causes the PHP script to hang or something, making my server send out a 502 error (server overloaded). Why does it work in the first example, despite how the temporary file isn't used at all, and not in this example. Remember, this all assumes there is a thumbnail in /thumb_img/.

    This PHP script is in use on my site. You can examine it here:
    http://hawkleyfox.kicks-ass.net/phptest/
    As you will see, the thumbnails are used in my directory listing. The PHP source for each example (and the orginal code before I butchered it) is in this directory, if you need more to go on.

    I'm burnt out on it right now. Please, any help is appreciated. Forgive my noobness.

  2. #2
    James Hawkle's Avatar
    New User

    Status
    Offline
    Join Date
    Mar 2002
    Posts
    5
    Yay! I fixed it AND Sonic Heroes is out! *wets pants*

    Umm... Anyway, I just took it in a different direction. Instead of getting the PHP file to output the cached thumbnail, I modifed the directory listing GSL script to check if a thumbnail is cached and pointing directly to that instead of letting the PHP script handle it.

    So, before a thumbnail is cached:
    HTML Code:
    <IMG src="/../phpThumb.php?src=/image/somefile.png&w=92&h=92&type=$(type:)" border="0">
    And if it sees there is a cached thumbnail:
    HTML Code:
    <IMG src="/thumb_img/somefile.png.jpg" border="0">
    Well, I butchered the hell out of this script, but it turned out even more effiecent then I hoped for. I still need to clean up the code though.

    Bleh. Must... get... Sonic Heroes... NOW!

  3. #3
    QuietDean's Avatar
    Administrator

    Status
    Offline
    Join Date
    Oct 2000
    Location
    Bournemouth, UK
    Posts
    2,662
    Hi, welcome to the forums./

    Glad you got it sorted
    If one of our members helps you, please click the icon to add to their reputation!
    No support via email or private message - use the forums!
    Before you ask, have you Searched?



Tags for this Thread