ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  Broken/corrupted image
Posted by Matthew on Aug-28-2014 04:02
Hi Peter,

I am using windows 7 64-bit os, php-5.3.27-nts-Win32-VC9-x86, and mySQL db.

The chart images I am trying to render are broken (red X/broken img) in IE, Mozilla, and
Chrome; however when I copy image URL it downloads an image which won't show in
windows because it is broken.  So it seems the chart object exists but the image is
corrupted somehow through encoding or something.

In phpchartdir.php the function partialUpdateChart has the line header("Content-type:
text/html; charset=utf-8");, but in Mozilla I was able to view the image info: content-type:
text/html; charset=windows-1252.

If you have any troubleshooting advice that would be greatly appreciated.

Thanks,

Matthew

  Re: Broken/corrupted image
Posted by Matthew on Aug-28-2014 04:58
One thing I forgot to mention is that when I open the corrupted image in a text editor I do
see a bunch of garbage characters however there is no legible text/error message within.

  Re: Broken/corrupted image
Posted by Peter Kwan on Aug-28-2014 06:57
Hi Matthew,

Are you testing using the exact original unmodified sample code? For example, have you
tested the simplebar.php sample code and does it have this problem? May be you can
attached the file you obtain. Even if it only contains garbage characters, we can often
determine the possible cause based on what garbage characters are there.

(A) If the issue only occurs in your own code, but not in the unmodified sample code, it is
likely cause by your code outputting HTML or text. The chart is not HTML or text. It is
PNG or JPG. If HTML or text is inserted in the PNG or JPG binary, it will correct the image.
causing the issue.

In PHP, anything that is not inside the <?php ... ?> is output as HTML or text. If you
have an empty space or empty line, it can be output as text, and this can corrupt the
image. So it is important the first line in your code is "<?php", with no leading space.

Some text editors may insert invisible UTF8 BOM header if the PHP script file is saved as
UTF8 text. That invisible header will be before the first visible text (which should be "<?
php"), and so is outside the <?php ... ?> block, and is considered as text by the PHP
interpreter. This may corrupt the image. If you are not sure if this can be the case, you
can attach a copy of the PHP script for us to analyze.

If your PHP script includes other PHP scripts (using include or require or require_once,
etc), and these other scripts has empty space or lines outside of the <?php .... ?> block,
it can also cause issues. If your PHP script includes "echo" or "print" statements, or emit
"notice" or "warning" messages, then it outputs text and can corrupt the image.

(B) If the issue occurs even with the unmodified sample code, one possibility is that there
are "filters" in the system that modifies the output. For example, I have seen systems
that will automatically modify all PHP output to make sure they are UTF8 encoded (or
conform to some encoding), or inject <META> tags or attempt to compress the output.
All these features work for HTML or text output, but will corrupt images. Normally, these
"filters" would check the MIME type of the output, and would not modify output of type
"image/png" or "image/jpeg" to avoid corrupting them, but I have seen filters that
modifies everything regardless of its MIME type. If we can have a copy of the corrupted
image file, we may be able to determine if it is being systematically modified.

Regards
Peter Kwan

  Re: Broken/corrupted image
Posted by Matthew on Aug-28-2014 22:04
Attachments:
Hi Peter,

I appreciate the timely and thorough response.  I tested the simplebar.php and was able to
view it, indicating that the issue falls under case (A), only occurring in my code rather than
a result of a filter.

Attached is the file I obtain when trying to view the chart.

Thanks,

Matthew
5486145275830.png

  Re: Broken/corrupted image
Posted by Peter Kwan on Aug-29-2014 00:57
Hi Matthew,

The error is caused an extra linefeed at the start of the image. Once I remove the linefeed,
I can get the correct image, which is a stacked bar chart with 5 bars and 2 segments each.

It is possible that your code or the files included by your code (using include or require or
require_once, etc) output the linefeed character (that is, an empty line). In PHP, anything
that is not in the <?php ... ?> block is output as text. So if your code has an empty line as
the first line, or the files included by your code has an empty line outside of <?php ... ?>
(either below or after the <?php ... ?> block), it would cause the issue.

To solve the problem, the ideal method is to find out where is the extra line and remove it.
If it is too tedious or if the included files cannot be modified, another method is to call the
PHP "ob_clean" function before outputting the chart. See:

http://php.net/manual/en/function.ob-clean.php

Hope this can help.

Regards
Peter Kwan

  Re: Broken/corrupted image
Posted by Matthew on Aug-29-2014 02:28
Hi Peter,

Thank you so much for your time and careful attention to my issue.  After digging through
all the php files I was calling through require, I found one with a whitespace  line after the
php close tag.  Thanks again, you are awesome!

Best,

Matthew