Forum Moderators: coopster

Message Too Old, No Replies

Dynamic images and browsers

Images show up on Netscape7 but not on IE

         

mkss

10:24 am on Jan 5, 2005 (gmt 0)

10+ Year Member



Hi,

I created a small PHP module which extracts JPEG data from a MSSQL blob database field (using ADODB), resizes it and then sends it to the browser, so that it can be used as argument for IMG SRC.
I'm opening a new window using javascript, create tables, output text and - the IMG SRC part. When calling from Netscape7, the window and all contained stuff shows up correctly, but not when calling from IE6. IE6 instead displays a red cross symbol (aka 'graphic not available'), but shows the image at its right place when clicking right -> view graphic.
It doesn't also matter whether I'm loading the JPEG from a blob field or from a file. Here's a little bit of code:

if ($_GET["pnr"]) {
$pnr = stripslashes($_GET["pnr"]);
$pnr = str_replace(chr(34),"",$pnr);
$pnr = str_replace(chr(39),"",$pnr);
$w = 128;
$h = 96;
$conn = &ADONewConnection("mssql");
$conn->PConnect($g_host, $g_user, $g_pass, $g_db);
$rs = &$conn->Execute("spi_GetPersPict '$pnr'");
$newp = imagecreatetruecolor($w,$h);

if (!$rs->EOF) {
$oldp = imagecreatefromstring($rs->fields[0]);
$w_src = imagesx($oldp);
$h_src = imagesy($oldp);
imagecopyresampled($newp,$oldp,4,4,0,0,$w,$h,$w_src,$h_src);
@imagedestroy($oldp);
unset($oldp);
}
else {
$col0 = imagecolorallocate($newp,0,0,0);
imagefill($newp,0,0,$col0);
}

$conn->Close(); unset($rs); unset($conn);

ob_start();
imagejpeg($newp,"",70);
$buffer = ob_get_contents();
ob_end_clean();

@imagedestroy($newp);
unset($newp);

header("Content-Type: image/jpeg");
header("Content-Disposition: inline; filename=FunnyFaces.jpg");
header("Cache-Control: no-store, no-cache, must-revalidate");

echo $buffer;
}

Regardless of whether I'm using echo or imagejpeg(), the result is the same and the problem's still there. The call for including the picture is:

(lt)IMG SRC="dispitem.php?pnr=1234" border=0(gt)

As you can see, I also tried the various cache control parameters, but it won't work with either of them. And the fact that Netscape7 is able to view the image shows me the image itself isn't corrupt in any way. But it appears on slow computers that Netscape7 first draws an empty placeholder for the image and then the image itself. I'm missing this 'final step' with IE6. I'm also aware that the header() function must not be preceded by another output and I also double-checked that there's no space in front of <?php. I don't use mixed PHP/HTML coding, so all modules are plain PHP modules starting with <?php and ending with?>.

I'm wondering if there's a simple solution for it. However, most code snippets I found regarding this theme work a similar way and I never heard anything about such problems.
Hope you excellent guys could shed some light onto me.

TIA,
Michael

mincklerstraat

12:02 pm on Jan 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to webmasterworld [webmasterworld.com], Michael.

I'm not real sure what your problem is exactly, but I'll give you my 2¢ here.

First off, I'm not sure your header 'Content-disposition: inline' is right for this purpose - if you're just sending the image to a browser, and not outputting it for being displayed inline in an e-mail message, this might be a source of problems. Note how most images you see on the web don't have this header. I've only come across this particular header and images for display in e-mail. So try leaving out the 'Content-disposition' header entirely.

Second, I don't know how much misery I've gone through trying to 'catch' the output of image functions with buffering, and then trying to output what I caught. You're already in the 'binary' mess of things using the BLOB field in your database - however, perhaps you could simplify your code, and maybe even remove whatever it is that's screwing up ie, by not buffering, and just outputting to the browser directly. This would entail removing the ob_start, ob_get_contents, and ob_end_clean lines, and putting your header lines above the imagejpeg line. This way imagejpeg just spits out its contents directly to the browser.

mkss

12:28 pm on Jan 5, 2005 (gmt 0)

10+ Year Member



Mincklerstraat,

thanks for your response. Both facts - the content-disposition header and output buffering - were inserted for testing when I saw that 'normal' or 'simplified' output didn't work. I first tried it omitting all headers except Content-Type: image/jpeg and just echoing the image or imagejpeg()'ing it. All that didn't work reliably with IE.
So my primary question better should read: What excatly does Netscape7 when displaying images? What exactly is missing with IE?
And, of course: What exactly is the way to produce a reliable output/display of dynamically created images?

Could it also be a matter of PHP version? Here, PHP 4.3.? is running on a W2K server as CGI, not as ISAPI. Don't have a server available at the moment which has Linux/Apache installed.

Additional:

Later on, I tried to change the operating scheme of PHP on this server from CGI to ISAPI, and voila! now it works at a glance :-)
So it seems to be something with CGI timing or with the fact CGI is called for every module while ISAPI is kept in memory during the connection's lifetime.

TIA,
Michael

mincklerstraat

4:21 pm on Jan 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Great to hear you got it solved. Also interesting that gd in this case seems to work differently as a cgi than as a module. I've never messed with a PHP cgi install, but that's a bit of a wake-up call for scripts that you're distributing - probably best to see if friends with cgi installs first try it out. The best of success with your script.