Help - IE7 doesn't print GD image - Solved thx

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

User avatar
robshanks
Forum Commoner
Posts: 32
Joined: Sun Aug 05, 2007 9:27 pm
Location: Hull, UK

Help - IE7 doesn't print GD image - Solved thx

Post by robshanks »

Hi
I am creating an image on the fly with PHP:

Code: Select all

<img src='scripts/sig.php?q=".$vehicleid."|".$vcdate."&sid=".rand()."' height='80px' width='240px' />
The website is https only and the created image displays perfectly in Firefox and MSIE 7. The image is also printed as expected by Firefox, however, MSIE will not print the image, just the red cross place holder.

Any ideas would be greatly appreciated.

Thanks in advance

Rob
Last edited by robshanks on Mon Aug 06, 2007 4:26 pm, edited 2 times in total.
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post by iknownothing »

"|" is an illegal Windows filename character, which most likely is causing the issue with IE
User avatar
robshanks
Forum Commoner
Posts: 32
Joined: Sun Aug 05, 2007 9:27 pm
Location: Hull, UK

No still no joy :(

Post by robshanks »

Hi iknownothing

Thanks for the input. I have changed the code to use an exclamation mark instead of the pipe character but the symptoms are exactly the same:

Code: Select all

<img src='scripts/sig.php?q=".$vehicleid."!".$vcdate."&sid=".rand()."' height='80px' width='240px' />
I am testing it on IE7 and it simply will not print the image which is displayed fine within the browser.

I am wondering it is a cahce vs https problem?

Any suggestions would be greatly appreciated.

Cheers

Rob
Last edited by robshanks on Mon Aug 06, 2007 3:55 pm, edited 1 time in total.
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post by iknownothing »

is this in PHP, or within HTML?

Code: Select all

<img src='scripts/sig.php?q=".$vehicleid."!".$vcdate."&sid=".rand()."' height='80px' width='240px' />
if PHP:

Code: Select all

echo "<img src='scripts/sig.php?q=" . $vehicleid . "!" . $vcdate. "&sid=" . rand() . "' height='80' width='240' />";
if HTML:

Code: Select all

<img src="scripts/sig.php?q=<?php echo $vehicleid; ?>!<?php echo $vcdate; ?>&sid=<?php echo rand() ?>" height="80" width="240" /> 
Also, I'm not sure if IE reads HTML (not CSS) coded sizes such as 240px, as 240px, but rather just 240. I'm not certain of this however.


And, is it given a filetype? .JPG, .GIF etc? If not, I'd say that is probably your problem.
User avatar
robshanks
Forum Commoner
Posts: 32
Joined: Sun Aug 05, 2007 9:27 pm
Location: Hull, UK

Still not got to the cause :(

Post by robshanks »

Thanks again for your comments:

Again the images display fine in Firefox and IE7. They also print fine from Firefox, however, IE7 will not print them, nor do they show up with Print Preview.

Therefore, I am thinking that perhaps IE7 doesn't treat the image in the same way as Firefox - perhaps something to do with image caching on a https server? I am unsure as although I am an experienced programmer I am not experienced with the intricacies of web development for IE7.

It is in PHP so full line displaying the image is:

Code: Select all

echo "<td colspan='5'><img src='scripts/sig.php?q=".$vehicleid."!".$vcdate."&sid=".rand()."' height='80' width='240' /></td></td>";
The PHP script sig.php returns a jpeg of a signature captured as coordinate data:

Code: Select all

<?php
  $q=$_GET["q"];
  $qa=explode("!",$q);
  //Get Signature Data
  $sql="SELECT vcdata FROM vehiclecheck WHERE vehicleid='".$qa[0]."' AND vcdate='".$qa[1]."'";
  $res=mysql_query($sql);
  $row=mysql_fetch_array($res);
  $vcdata=explode("|",$row['vcdata']);
  $plot=$vcdata[16];
  //Draw Signature
  $im = @imagecreatetruecolor(240, 80)
    or die("Cannot Initialize new GD image stream");
  $black=imagecolorallocate($im, 1, 1, 1);
  $bg=imagecolorallocate($im, 255, 255, 255);
  imagefill($im, 0, 0, $bg);
  $sigpos=0;
  while($sigpos<strlen($plot))
  {
    $plotX=intval(hexdec(substr($plot,$sigpos,2)));
    $plotY=intval(hexdec(substr($plot,$sigpos+2,2)));
    if($plotY>127)
    {
      $plotXold=$plotX;
      $plotYold=$plotY-128;
    }
    else
    {
      imageline($im,$plotXold,$plotYold,$plotX,$plotY,$black);
      $plotXold=$plotX;
      $plotYold=$plotY;
    }
    $sigpos=$sigpos+4;
  }
  // Turn on output buffering
  ob_start();
  imagejpeg($im);
  header("Content-type: image/jpeg");
  // Tell the browser the number of bytes in buffer
  header("Content-Length: " . ob_get_length());
  // Send the buffer to the browser
  ob_end_flush();
  // Free the memory
  imagedestroy($im);
  exit;
?>
Any ideas gratefully received.

Thanks in advance

Rob
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post by iknownothing »

I'm no Pro with GD, I actually have no idea what that code does apart from what your telling me, but just for a bit of Trial and Error, try this, just for fun:

Notice, I added .jpg (as I can't see anywhere where it is being used in your code) (change .jpg to whatever image type you are using if not .jpg)

Code: Select all

echo "<td colspan='5'><img src='scripts/sig.php?q=".$vehicleid."!".$vcdate."&sid=".rand().".jpg' height='80' width='240' /></td>";
I'm thinking IE as an issue with MIME type or something, but disregard this post if you feel I'm way off the mark.
Last edited by iknownothing on Mon Aug 06, 2007 9:51 am, edited 1 time in total.
User avatar
robshanks
Forum Commoner
Posts: 32
Joined: Sun Aug 05, 2007 9:27 pm
Location: Hull, UK

Nope :(

Post by robshanks »

:( No difference - oh well will keep searching for inspiration.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Doh. Need to learn to read.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Why not just get rid of the explode and make it another $_GET variable?

Code: Select all

echo "<img src='scripts/sig.php?id=".$vehicleid."&date=".$vcdate."&sid=".rand()."' height='80px' width='240px' />"; 
User avatar
robshanks
Forum Commoner
Posts: 32
Joined: Sun Aug 05, 2007 9:27 pm
Location: Hull, UK

Post by robshanks »

Thanks Zoxive

But the problem isn't producing the image, that is produced fine.

The only issue is IE7 will not print the image. IE7 displays it correctly, Firefox both displays it and prints it correctly.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

robshanks wrote:Thanks Zoxive

But the problem isn't producing the image, that is produced fine.

The only issue is IE7 will not print the image. IE7 displays it correctly, Firefox both displays it and prints it correctly.
Simplifying the script makes it easier to determine what is wrong.
Did you debug and echo all the variables you want your php code to see in IE7?

It could also be, because you are setting your headers after you send the image data.

Code: Select all

header("Content-type: image/jpeg");
imagejpeg($im); // Should be after Header
User avatar
robshanks
Forum Commoner
Posts: 32
Joined: Sun Aug 05, 2007 9:27 pm
Location: Hull, UK

Post by robshanks »

Thank you all for your assistance.

Zoxive put me on the right track: Simplifying it all down to basics worked:

Code: Select all

<?php
  header("Content-type: image/jpeg");
  $im = @imagecreatetruecolor(240, 80)
    or die("Cannot Initialize new GD image stream");
    $black=imagecolorallocate($im, 1, 1, 1);
  $bg=imagecolorallocate($im, 255, 255, 255);
  imagefill($im, 0, 0, $bg);
  imagestring($im,3,1,1,"Help! Won't print in IE7",$black);
  imagejpeg($im);
  imagedestroy($im);
  exit;
?>
So in my original code I pass the coordinate data directly rather than carrying out the mysql_query and it has worked and IE7 will now print the image :D

Thanks again.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Make sure to also specify the content-length header....
User avatar
robshanks
Forum Commoner
Posts: 32
Joined: Sun Aug 05, 2007 9:27 pm
Location: Hull, UK

Post by robshanks »

Hehe

This is where it all started for me . . .

To specify the content-length header you have to buffer the output (as I am not saving the image as a file) which was the first thing I tried to get IE7 to print the image.

However, it is there in my production code:

Code: Select all

<?php
  $plot=$_GET["q"];
  $im = @imagecreatetruecolor(240, 80)
    or die("Cannot Initialize new GD image stream");
  $black=imagecolorallocate($im, 1, 1, 1);
  $bg=imagecolorallocate($im, 255, 255, 255);
  imagefill($im, 0, 0, $bg);
  $sigpos=0;
  while($sigpos<strlen($plot))
  {
    $plotX=intval(hexdec(substr($plot,$sigpos,2)));
    $plotY=intval(hexdec(substr($plot,$sigpos+2,2)));
    if($plotY>127)
    {
      $plotXold=$plotX;
      $plotYold=$plotY-128;
    }
    else
    {
      imageline($im,$plotXold,$plotYold,$plotX,$plotY,$black);
      $plotXold=$plotX;
      $plotYold=$plotY;
    }
    $sigpos=$sigpos+4;
  }
  // Turn on output buffering
  ob_start();
  imagejpeg($im);
  header("Content-type: image/jpeg");
  // Tell the browser the number of bytes in buffer
  header("Content-Length: " . ob_get_length());
  // Send the buffer to the browser
  ob_end_flush();
  // Free the memory
  imagedestroy($im);
  exit;
?>
Zoxive wrote:
It could also be, because you are setting your headers after you send the image data.
PHP:
header("Content-type: image/jpeg");
imagejpeg($im); // Should be after Header
The image and the header are sent to the buffer and then the buffer is sent to the browser.

In the end it was something to do with the extra code I had in the script producing the image which was being used to access the database to retreive the coordinate data which was casuing the problem.

When i passed the coordinate data directly and only produce the image from it in the script it displays and prints correctly in Firefox and IE7.

Now I just need to check the max amount of data I can pass as a string via $_GET . . . but that'll wait for another day.
Last edited by robshanks on Mon Aug 06, 2007 3:28 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It's typically a few kilobytes of data for most browsers, but does vary between operating systems and browsers.
Post Reply