Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
PHP GD thumbnail problem
Old 08-13-2005, 09:33 AM PHP GD thumbnail problem
paulshutt's Avatar
Novice Talker

Posts: 5
Trades: 0
Hi All

This is my first posting so be gentle!!

I am having trouble with using PHP and the GD lib to create (some) thumbnail images.

The background....

I have a site that admins upload images over a PHP interface which updates the MYSQL database.
The first person to request the image then makes the PHP script create a thumbnail for use on the site. This then gets saved to a thumbnail image folder.
The next time someone requests the image the script checks to see if the file /photo/thumbnail/whatever.jpg file exists if it does the script returns the filename to be included as the source for the image, if not then it goes through the create image bit.

The problem ...

Now this is working fine apart from the odd image here and there which get the thumbnails created and saved fine, however when the script goes back to serve the image then a PHP error occurs (not always the same one!). and just the placeholder appears.

If I delete the file and let the script create the image again the same happens.
If I open the image in the browser

http://mysite.com/photo/thumbnail/whatver.jpg

up pops the image thumbnail file.

If I copy another thumbnail to that file name then the thumbnail appears fine on the site(obviously not the picture I want!).

I have tried to think of everything causing this behaviour and I am now lost.
I don't think the code is at fault and I am coming round to the posibility that there was a problem with the original image as copying another known good original to the other original filename and deleting the thumbnails also results in success (obviously not the picture I want yet again).

However the original images all serve in a browser ok and if I output directly to the screen instead of saving the file to disk this appears ok.

Here is the basic code for the image create inlude file.


PHP Code:
<? include "common.php";                                   // Database connection etc.....
$pilot_id=$HTTP_GET_VARS['id'];                            // Get the pilot id for this image


$sql2="SELECT photo FROM tbl_pilots WHERE id=".$pilot_id;

$result2 mysql_query($sql2);                            

if (
mysql_num_rows($result2)>0) {
    
$row2 mysql_fetch_object($result2);

    
$image $row2->photo;
    
    
$cachefile = ("photos/pilots/thumbnails/".$image);   // set the name of the cahcefile in the thumbnails directory
    
    
if (file_exists($cachefile)) {         // if the page has been cached from an earlier request 
         
include($cachefile);              // output the contents of the cache file 
          
exit;                            // exit the script, so that the rest is not executed 
            
}
            
    
// If no cache file is found then get the image, resample, resize and output to the file in $cachefile
    // This should only run once for each image unless the thumbnail is deleted.
    
    
if ($image == NULL)
    {
$image "noimage.jpg";}


    
$imf imagecreatefromjpeg ("photos/pilots/".$image); 

    
$ximagesx ($imf);

    
$yimagesy ($imf); 
    
    
$xyratio $x/$y;
    
            if (
$x $y)
            {
$sx 75;
            
$sy 75 $xyratio;}
            else
            {
$sy 75;
            
$sx 75 $xyratio;}



            
$imimagecreatetruecolor($sx$sy);

            
imagecopyresampled ($im$imf0000$sx$sy$x$y);    

            
Header("Content-type: image/jpeg");

            
imagejpeg($im,$cachefile,100);   // output the thumbnail at 100% quality

}

 
?>
If you have not fallen asleep already ;-)

If I change the above code to output to the browser and not to a file

imagejpeg($im,'',100); <<<< 2 single quotes not one double!!

Then the image appears fine however the CPU load starts to rise as the site is busy and without the caching then the site slows down during busy times.
So it is only when the routine reads back the saved image thumbnail that the error occurs.


Has anyone come accross this sort of behaviour before ?

If anyone wants to help in tracking this problem down then feel free.

I can point you to the live urls and play with the code to try and sort things out.


Thanks

Paul.

Last edited by 0beron; 08-13-2005 at 06:04 PM..
paulshutt is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-13-2005, 02:07 PM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
What types of PHP error does it cause?
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 08-13-2005, 04:33 PM
paulshutt's Avatar
Novice Talker

Posts: 5
Trades: 0
well thats the strange thing (another one!) the errors are all PHP parse errors but are all different for the different image which dont work. Here are a few...


Sat Aug 13 21:25:34 2005] [error] PHP Parse error: parse error, unexpected T_STRING in /home/sites/site4/web/photos/pilots/thumbnails/68 on line 12

Sat Aug 13 21:25:25 2005] [error] PHP Parse error: parse error, unexpected '`' in /home/sites/site4/web/photos/thumbnails/0308-32.jpg on line 20

Sat Aug 13 21:25:24 2005] [error] PHP Parse error: parse error, unexpected '{' in /home/sites/site4/web/photos/thumbnails/0308-15.jpg on line 24

[Sat Aug 13 21:25:23 2005] [error] PHP Parse error: parse error, unexpected ':' in /home/sites/site4/web/photos/thumbnails/0308-4.jpg on line 22

[Sat Aug 13 21:25:03 2005] [error] PHP Parse error: parse error, unexpected '~' in /home/sites/site4/web/photos/thumbnails/8.8.05 002.jpg on line 15

The strange thing is the error log shows the errors in the actual thumbnail files and not the files that create or call the thumbnails!

In the mean time I have set up another script which does the same thing but makes bigger picturers than the thumbnails (using the same base code) and one of the files that will not make a thumbnail will make a medium sized image just fine!

I am obviously missing something but I just don't know what.

Paul.
__________________
There are 10 types of people in the world....
Those who understand binary.. and those who don't.
paulshutt is offline
Reply With Quote
View Public Profile
 
Old 08-13-2005, 06:12 PM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
Ah, I think I've got it. You are using that script as an include file right? The GD function imagejpeg, combined with a jpeg header will actually output an image file, as binary data to the browser. When you include the file, the raw image gets dumped into your main script and parsed as PHP.

The solution to this is not to use include, but instead to simply echo out an img tag with the php script that generates the thumbnail as the src attribute:

PHP Code:
//Your main script
//...

//$pilot_id is set somewhere here.
//...
echo "<img src=\"createthumbnail.php?pilot_id=$pilot_id\" alt=\"Pilot Image\" />"
That way the main script generates a complete page with an image tag in it. This gets sent to the browser and the browser makes a second HTTP request to get the image. It does this by going to the createthumbnail.php script, passing along the pilot_id. The script generates the thumbnail and sends back the binary image which the browser interprets as a jpg due to the header information, and sticks it in your image tag as it should.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 08-14-2005, 03:24 AM
paulshutt's Avatar
Novice Talker

Posts: 5
Trades: 0
Thanks Oberon

I will try and get my head round it and have a play a little later.
I will let you know the results.

Thanks for the interest.

Paul.
__________________
There are 10 types of people in the world....
Those who understand binary.. and those who don't.
paulshutt is offline
Reply With Quote
View Public Profile
 
Old 08-14-2005, 07:04 PM
paulshutt's Avatar
Novice Talker

Posts: 5
Trades: 0
Hi Oberon

I am calling the above code as a separate php file (wotdimg.php from the main page (wotd.php)

The code is here

PHP Code:

    <td align="center">

          <a href="pilot.php?id=<? echo $crow->id?>&link=index.php"><img src="wotdimg.php?id=<? echo $crow->id?>"  border="0" alt="Todays Winners ... Click for Pilot Details."></a>

              </td></tr>
So when wotd.php tries to get the img src from wotdimg.php?id=<a value from the database> then the script wotdimg.php will make the file <pilot_id>.jpg and save it to disk in the directory.
Now the inclde statement in the wotdimg.php supplies the value <pilot_id>.jpg to the img src tag in the main page wotd.php.

So this seems to be working like you say it should (only the other way around) if I have read it correctly.
This still does not answer why it works fine 95% of the time.

Feel free to point things out if I have got the wrong end of the stick.

Paul.
__________________
There are 10 types of people in the world....
Those who understand binary.. and those who don't.
paulshutt is offline
Reply With Quote
View Public Profile
 
Old 08-15-2005, 10:06 AM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
Maybe we are still at crossed purposes...

The way I described it, the thumbnail image never goes on disk on the server - the wotdimage.php scipt just gets the original from disk, and hands back a thumbnail image directly to the browser. Maybe this isn't what you want?
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 08-15-2005, 11:50 AM
paulshutt's Avatar
Novice Talker

Posts: 5
Trades: 0
Thanks Oberon

After thinking about what you said about the parse errors I reworked the script and it works fine now.

Apart from a few minor modifiactions, like moving the cache file checking before the database include file is called not after (should save a few connections!!) and destroying the image once we have created it to free resources. The thing that I changed was to use

readfile($cahefile) instead of include($cachefile) as I suspect it had soemthing to do with the image being parsed and sometimes causing errors.

Now all is fine once again.

If you want to see what all this was about go to http://www.worldgliding2005.com

It is used in the Daily Winners pane top left and on the Photos section.

Using the image caching instead of creating them on the fly reduced CPU load by a factor of 10 - 20 times!!!

Thanks again for the help in making me think about where the problem was.

Paul.


PHP Code:
<? 
$pilot_id
=$HTTP_GET_VARS['id'];                                // Get the pilot id for this image

$cachefile = ("photos/pilots/thumbnails/".$pilot_id.".jpg");   // set the name of the cahcefile in the thumbnails directory
    
    
if (file_exists($cachefile)) {                             // if the page has been cached from an earlier request 
        // include($cachefile);                                  // output the contents of the cache file 
    
readfile ($cachefile);
          exit;                                                
// exit the script, so that the rest is not executed 
            
}
            
// If no cache file is found then connect to database, get the image, resample, resize and output to the file in $cachefile
// This should only run once for each image unless the thumbnail is deleted.

include "common.php";                                          // Database connection etc.....

$sql2="SELECT photo FROM tbl_pilots WHERE id=".$pilot_id;

$result2 mysql_query($sql2);                            

if (
mysql_num_rows($result2)>0) {
    
$row2 mysql_fetch_object($result2);

    
$image $row2->photo;
        
    if (
$image == NULL)
    {
$image "noimage.jpg";}

    
$imf imagecreatefromjpeg ("photos/pilots/".$image); 

    
$ximagesx ($imf);

    
$yimagesy ($imf); 
    
    
$xyratio $x/$y;
    
            if (
$x $y)
            {
            
$sx 75;
            
$sy 75 $xyratio;
            } else {
            
$sy 75;
            
$sx 75 $xyratio;
            }

            
$imimagecreatetruecolor($sx$sy);

            
imagecopyresampled ($im$imf0000$sx$sy$x$y);    

            
Header("Content-type: image/jpeg");

            
imagejpeg($im,'',100);                                // output the thumbnail at 100% quality to browser
            
imagejpeg($im,$cachefile,100);                        // save the thumbnail at 100% quality to disk
            
imagedestroy($im);
}

 
?>
__________________
There are 10 types of people in the world....
Those who understand binary.. and those who don't.
paulshutt is offline
Reply With Quote
View Public Profile
 
Old 08-15-2005, 04:10 PM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
Aaah, I see now. We both had the same idea but slightly different. I think I picked up on what the problem was sort of, but in a different part of your script, sorry for the confusion.

You are right to change it to readfile() instead of include(). include works as if the file you are including is right there in your code, line for line. If it's a php file, great, it will work, but if it's an image file for example then you have a problem.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Reply     « Reply to PHP GD thumbnail problem
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.30737 seconds with 12 queries