ImageJPG only working a couple of times

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
megamanic
Forum Newbie
Posts: 5
Joined: Wed Aug 13, 2003 7:40 am

ImageJPG only working a couple of times

Post by megamanic »

This one's making me tear my hair out.

What I'm doing is pretty traditional stuff.

Graphic designer uploads jpg into products/master directory
I've written a 404 handler to look for .jpgs in the master directory resize them & output them to the browser - saving the resized image into products so I don't need to keep creating the images on the fly every time.

code follows - the problem I have is the following. If I don't save the image the 404 handler just keeps working beautifully. If I get the script to save the image down to the products directory it works - twice. Then every subsequent request fails UNTIL I upload the script to the server which resets the process so I can create another 2 images. What am I missing?

Code: Select all

/*  $last  is the name of the jpg to be created e.g. test.jpg with no directory information */

  header("Content-type: image/jpg");
  $img_orig = imagecreatefromjpeg('../products/master/'.$last); 
  $new_x = imagesx($img_orig) / 2;
  $new_y = imagesy($img_orig) / 2;
  $img_dest = imagecreatetruecolor($new_x,$new_y);
  imagecopyresampled(	$img_dest, 
    $img_orig,
    0,0,0,0,
    $new_x,
    $new_y,
    imagesx($img_orig), 
    imagesy($img_orig));
/* This line only works two or three times through - and I've used different ways of defining the filename */
//    imageJPEG($img_dest,'../products/'.$last,88); 
// this line works at least 8 -10 times
   imageJPEG($img_dest,'',88); 
   ImageDestroy($img_orig);
  ImageDestroy($img_dest);
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post by pootergeist »

lose the header() line basically

actually you want to if/else it a bit

if(browser output)
header (......)
imagejpeg($ref);

else
imagejpeg($ref, $savename);

you'll notice a snag there - they have to be the other way around and running both a local save and browser output isn't advised (from memory I had snags - try it and see though)

I'd suggest just creating the local copy when the artist uploads for two good reasons
1. uploading takes time - the additional .NN of a second to process a thumbnail is not going to be noticed (even when uploading 10 concurrently)
2. delaying a page by that amount for joe-surfer public might be noticed.

something like that anyway.
megamanic
Forum Newbie
Posts: 5
Joined: Wed Aug 13, 2003 7:40 am

Post by megamanic »

Thanks for that. I need to code around a bit to get this working because the idea was to create and save the images inside a 404 handler so the first person to request the image (probably the graphic designer checking his work) takes the slight hit & everyone else gets the image served normally.

I suppose what I'll have to do is change the workflow so that the graphic designer uses http to upload the files & I'll create the resized copies then.

Ideally I'd like this technique to work the way I described - especially as I'm just doing a variation on a technique that Rasmus himself suggested at a seminar I attended a few months ago & I was dying to try out ever since :wink:

Does anyone know what the nature of the error I'm getting is & how likely it is to get fixed in the next release (presumably of GD rather than PHP)

Regards

Mike
megamanic
Forum Newbie
Posts: 5
Joined: Wed Aug 13, 2003 7:40 am

Post by megamanic »

Just thinking...

would this work??

Code: Select all

imagejpeg($ref, $savename);  // Save the image to a file
header (......);
imagejpeg($ref);  // now output it to the browser...
Or would it just make things a lot worse?
megamanic
Forum Newbie
Posts: 5
Joined: Wed Aug 13, 2003 7:40 am

Post by megamanic »

Just tried it & the answer is "sort of" - it improves the fail point to 8 graphics...
Post Reply