cancel
Showing results for 
Search instead for 
Did you mean: 

PHP script not always displaying image

Pablos
Newbie
Posts: 9
Registered: ‎24-12-2007

PHP script not always displaying image

I'm trying to write a script that resizes an image. I uploaded this image in my cgi space:
http://ccgi.pabs2003.plus.com/petimage/rosa.jpg
You should see an image of a the tarantula in your browser (i hope you're not aracnophobic). So far so good,  right, ....
Now, before I add the resizing code I've done a simple PHP script to display the image for now:
<?php
    header("Content-type: image/jpeg");
    $image = imagecreatefromjpeg('http://ccgi.pabs2003.plus.com/petimage/rosa.jpg');
    imagejpeg($image);
?>
Later on I will hopefully add resizing code, but for now my main concern is that this script does not always result in the image displaying on my  browser all the time.
Try it yourself:
http://ccgi.pabs2003.plus.com/cgi-bin/Pets/imagejpeg.php
To see what I mean click the 'Refresh' button on and wait. Do that a couple of times.
I find that I get the image around 50-60% of time, and the rest of  the time the browser just waits forever. 
Why doesn't it always work?Huh
Someone please take a look at this problem for me. I'd really appreciate it.
Thank you!
Merry Christmas,
Pablo.
14 REPLIES 14
MikeWhitehead
Grafter
Posts: 748
Registered: ‎19-08-2007

Re: PHP script not always displaying image

It seems to be working fine for me, but just as a thought, alter your code to read:

<?php
    header("Content-type: image/jpeg");
    $image = imagecreatefromjpeg('http://ccgi.pabs2003.plus.com/petimage/rosa.jpg');
    if(!$image){
        echo "Image creation failed";
    } else {
        imagejpeg($image);
        imagedestroy($image);
    }
?>

At least it'll give a bit more of an insight into if it's being created on those times that you don't see it working.
Merry Xmas Smiley
Pablos
Newbie
Posts: 9
Registered: ‎24-12-2007

Re: PHP script not always displaying image

Hi Mike,
ok I've tried altering the code to that, but i still get  the same result.
on my browser i just get 'Waiting for ccgi.pabs2003.plus.com' at the bottom and the spinning 'loading' icon goes on forever.  when i hit 'refresh' it might work or it might not, either way i get no error.
i might get lucky and it loads first time around, but then it will always fail at some point, quite soon, if i keep refreshing.
Just out of interest here is the link:-
http://ccgi.pabs2003.plus.com/cgi-bin/Pets/imagejpeg.php
although i think i already gave it to you.
Thanks a lot!!
Merry Christmas,
Pablo.
jelv
Seasoned Hero
Posts: 26,785
Thanks: 971
Fixes: 10
Registered: ‎10-04-2007

Re: PHP script not always displaying image

Just in case you thought it was you - I get the browser hang about half the time.
jelv (a.k.a Spoon Whittler)
   Why I have left Plusnet (warning: long post!)   
Broadband: Andrews & Arnold Home::1 (FTTC 80/20)
Line rental: Pulse 8 Home Line Rental (£14.40/month)
Mobile: iD mobile (£4/month)
paulh
Rising Star
Posts: 1,283
Thanks: 10
Registered: ‎30-07-2007

Re: PHP script not always displaying image

hangs a lot for me.
i tried it at work today with IE7 and at home with Opera 9.25 -- tbhe resize deosn't work at all in opera
MikeWhitehead
Grafter
Posts: 748
Registered: ‎19-08-2007

Re: PHP script not always displaying image

Hmmmm...sorry for not being too much help lately, but as you might know its Christmas Tongue lol. I'm shattered and stuffed, so I'm not at the top of my game right now.
Does it only happen with this one page, or multiple different pages (in particular pages that do not use the imagecreatefromjpeg function)?
SNIP: Just wrote a heap of code for you, but decided I should test it first and it turns out it's useless. I'll have a closer look at it tomorrow.
Pablos
Newbie
Posts: 9
Registered: ‎24-12-2007

Re: PHP script not always displaying image

Hi Mike,
Sorry didn't get back to you sooner.  Families and that, you know Smiley
I created a new file with the following code, as adviced:
<?php
    header("Content-type: image/jpeg");
    $image = imagecreatetruecolor(400,400);
    if(!$image){
        echo "Image creation failed";
    } else {
        imagejpeg($image);
        imagedestroy($image);
    }
?>
See it at:-
http://ccgi.pabs2003.plus.com/cgi-bin/Pets/imagejpeg2.php
And guess what? It works.
You were right!
Pablo.
MikeWhitehead
Grafter
Posts: 748
Registered: ‎19-08-2007

Re: PHP script not always displaying image

Great to see that that's working so far - seems then its imagecreatefromjpeg that's being problematic. Have you been able to reproduce this to work with your original sample image? If not, I'll have a mess around today and see what I can come up with (I don't tend to do much work with image handling so don't know of an easy fix just yet).
Pablos
Newbie
Posts: 9
Registered: ‎24-12-2007

Re: PHP script not always displaying image

Hi Mike,
Sorry didn't get back to you sooner. Went out skateboarding.
I don't know how i would do it without imagecreatefromjpeg().  I'm stuck at this point in time.
Thanks for all your help.
Take care,
Pablos.
Peter_Vaughan
Grafter
Posts: 14,469
Registered: ‎30-07-2007

Re: PHP script not always displaying image

You could just let the browser deal with the image resize by computing the height & width params to the img tag withou affecting the actual image size. You can get details about the image in php.
MikeWhitehead
Grafter
Posts: 748
Registered: ‎19-08-2007

Re: PHP script not always displaying image

That would lead to improper scaling though, which I am guessing why he wants to go down the imagecreatefromjpeg route - it provides much smoother scaling of an image with less image degradation - it will also reduce server load as the reduced image will weigh less.
I still can't really understand why it isn't working sometimes and working others. Writing up some code now which, if it works, will at least help debugging a bit. It's a hard one to diagnose though, since I've never came across it before.
Pablos
Newbie
Posts: 9
Registered: ‎24-12-2007

Re: PHP script not always displaying image

Thanks Peter.
That's true, i could let the browser resize the image, but then i would also be forcing the browser to download a potentially very large image - that's what I'm trying to avoid. I was thinking the only other possibility would be to let the PHP resize the image upon the user uploading a thumbnail the image, but then that still leaves the possibility of imagecreatefromjpeg() not working (*), and that would be very uncooperative thing.
Thanks!!
Pablos.
* it seems that is what stopped the image loading
Pablos
Newbie
Posts: 9
Registered: ‎24-12-2007

imagecreatefromjpeg() is times-out; was: PHP script not always displaying image

Dear Community Members,
I have found that PHP's processing of the document containing a call to the GD library function imagecreatefromjpeg() hangs-up on that line and simply goes no further (*).  I tried various strategies, including reducing the .jpg image to very-small image in case that was the problem, and trying other jpeg sources but the results were the same.
Having done some research on Google I found that there were a few people who were having this problem and the advice given to them seems to have been to report it to PHP.net as a bug.
Hypothetically if this *is* a bug then it seems to have been fixed in later versions of GD/PHP, but in the mean-time i'm stuck because the version of PlusNet's GD/PHP has a bug --assuming that to be the problem.
I don't think there is a work-around for this.  Is there any other option other than to change to an ISP that provides a better version of GD/PHP?
Please advice.
All the best,
Pablos.
*  sometimes it works fine, but more often than not it just hangs-up
Pablos
Newbie
Posts: 9
Registered: ‎24-12-2007

mystery solved: why wasn't imagecreatefromjpeg() working -- doh!!

Dear all,
Over Christmas I posted here to ask why my script using the imagecreatefromjpeg() function wasn't working.
Cut a long story short, I want to thank all of you who tried to help me. In particular thanks to Mr Mike Whitehead who inspite of his hangover very kindly volunteered to help me and nearly found the problem.  Thanks, Mike, for that.
Thing is I didn't know, and seems nobody else spotted either, was that you weren't supposed to call imagecreatefromjpeg() using a filename parameter as an HTTP request, so I had something like the following, from memory:-
imagecreatefromjpeg('http://ccgi.pabs2003.plus.com/petimage/rosa.jpg");
and no matter how much I tried to work it it kept failing now and again, but I always kept using 'http://.....' as a parameter, that was the reason.
Now thanks to Mr Richard Pearce in PlusNet Technical support I know that I am being a bad boy doing this. You are supposed to use the file-system for this call, so somthing like the following works no problem:0
imagecreatefromjpeg('rosa.jpg');
where my convoluted 'http://....' parameters where not working.
Don't know why I did it this way. I think i must have believed that jpeg files were not accessible on the cgi web space so I had to muddle-around using an 'http://....' call to the image instead, and it worked..... kind of.
Thanks a lot guys.
All the best,
Pablo.
prichardson
Grafter
Posts: 1,503
Thanks: 1
Registered: ‎05-04-2007

Re: PHP script not always displaying image

I had a good look at this and I will admit, I did not spot it. That said, I knew about the very problem you refer to, which is not a problem with your code, but more the nature of the load balancer that serves the CCGI system.
Back when I was a customer, so your talking about 3 years back, more so, back prior to the current incarnation of the CCGI server (when it was just called CGI, or criticalmass).
I spotted that if you wanted to load a URL from the CGI server, it would fail. Now normally this doesn't matter, as you can do it different ways, just as pipe the output from the shell, include it if it is PHP (or same lang as the calling code) or a few other tricks.
What you could not do however was use something like an XML or news feed from another site hosted on the CGI server, which could only be done by the URL.
When looking into it, the load balancer basically does not like loopback connections. ie, it cannot handle a connection from the CGI server to the CGI servers load balancer IP.
It could however handle a connection to the servers local IP address and if clever enough, fool the CGI server to serve content as if it was a normal web request.
I created some code so that PHP could handle a custom URI, just for use on the CGI server, such as cgi://cgi.username.plus.com/site.php
I no longer have a copy, though there may be somebody about with one. I am unsure if it works in the new CCGI setup, but it is essentially the same issues preventing your original code working.
If there is a copy about, I will clean it up and ensure it works on the servers.