473,388 Members | 1,425 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

PHP-gen'd images won't display in HTML

CPD
I have an image-generating script myimage.php that's worked and that I've
used for years. All it does is increment a counter file and output a single
pixel in the color I provide. But on one particular web server I uploaded
to, the behavior is strange.

If I enter the URL http://www.domain.ext/myimage.php, the picture is
displayed as expected. But if I insert the script into HTML, like <img
src="myimage.php">, it fails to display and I get the broken image icon,
instead.

This has never happened before, but I do not know enough about PHP builds
and web server settings to figure this out.

Any suggestions? Thanks for any help!

Here's the code, if necessary:

$filename = trim($_GET['filename']);
$color = (trim($_GET['color']) == "") ? "000000" : trim($_GET['color']);

$count = '0';
$file_last_mod = 0;

// Set up filename whether or not provided

if(strlen($filename)) {
$filename .= ".count";
}
else {
$fname_end = strrpos($script_name, ".");
$fname_end = ($fname_end === false) ? strlen($script_name) :
$fname_end - 1;
$slash_pos = strrpos($script_name, "/");
$fname_start = ($slash_pos === false) ? 0 : $slash_pos + 1;
$script_name = (!strlen($filename)) ? $HTTP_SERVER_VARS['SCRIPT_NAME'] :
$filename;
$filename = substr($script_name, $fname_start, $fname_end - $slash_pos)
.. ".count";
}

// open and read file if it exists

if (file_exists($filename)) {
$file_last_mod = filemtime($filename);
$count = file_get_contents($filename);
}

// increment count

$count += 1;

// reopen file and write new count

$handle = fopen($filename, "w");
fwrite($handle, $count);
fclose($handle);

// Generate a 1 x 1 pixel image

$im = @imagecreate(1, 1)
or die("<!-- Cannot Initialize new GD image stream -->");

// Set up color if not provided

if (!strlen(trim($color))) $color = "000000";

// Set image color

$red = 0 + base_convert(substr($color, 0, 2), 16, 8);
$green = 0 + base_convert(substr($color, 2, 2), 16, 8);
$blue = 0 + base_convert(substr($color, 4, 2), 16, 8);
$img_color = imagecolorallocate($im, $red, $green, $blue);

// Output the created image according to available GD image support

if (function_exists("imagegif")) {
header("Content-type: image/gif");
imagegif($im);
} elseif (function_exists("imagejpeg")) {
header("Content-type: image/jpeg");
imagejpeg($im);
} elseif (function_exists("imagepng")) {
header("Content-type: image/png");
imagepng($im);
} elseif (function_exists("imagewbmp")) {
header("Content-type: image/vnd.wap.wbmp");
imagewbmp($im);
} else {
die("<!-- No image support on this PHP server -->");
}
Dec 28 '06 #1
9 1774
I tried your script and it works on my localhost. If you tried
http://www.domain.ext/myimage.php and it worked then we know the image
module is working.

So all i can think of is
1) The path to the image has a problem.
2) The file outputs some unwanted characters. Check that there is no
blank spaces at the top of the page or anywhere else that get output.
Try setting error_reporting(0) so that no warnings are output.

Dec 28 '06 #2
When i save image.php in a utf-8 encoding it doesn't work because utf-8
inserts an invisible character at the top of the page. In that case the
file needs to be saved with another encoding.

Dec 28 '06 #3
I also had this problem, I can;t remember exactly how i solved it, but i
remember that you CAN'T set the image-source to a .php file.

If i now more, i'l let you know.

"CPD" <no@spam.plsschreef in bericht
news:PO******************************@comcast.com. ..
>I have an image-generating script myimage.php that's worked and that I've
used for years. All it does is increment a counter file and output a single
pixel in the color I provide. But on one particular web server I uploaded
to, the behavior is strange.

If I enter the URL http://www.domain.ext/myimage.php, the picture is
displayed as expected. But if I insert the script into HTML, like <img
src="myimage.php">, it fails to display and I get the broken image icon,
instead.

This has never happened before, but I do not know enough about PHP builds
and web server settings to figure this out.

Any suggestions? Thanks for any help!

Here's the code, if necessary:

$filename = trim($_GET['filename']);
$color = (trim($_GET['color']) == "") ? "000000" : trim($_GET['color']);

$count = '0';
$file_last_mod = 0;

// Set up filename whether or not provided

if(strlen($filename)) {
$filename .= ".count";
}
else {
$fname_end = strrpos($script_name, ".");
$fname_end = ($fname_end === false) ? strlen($script_name) :
$fname_end - 1;
$slash_pos = strrpos($script_name, "/");
$fname_start = ($slash_pos === false) ? 0 : $slash_pos + 1;
$script_name = (!strlen($filename)) ? $HTTP_SERVER_VARS['SCRIPT_NAME']
: $filename;
$filename = substr($script_name, $fname_start, $fname_end - $slash_pos)
. ".count";
}

// open and read file if it exists

if (file_exists($filename)) {
$file_last_mod = filemtime($filename);
$count = file_get_contents($filename);
}

// increment count

$count += 1;

// reopen file and write new count

$handle = fopen($filename, "w");
fwrite($handle, $count);
fclose($handle);

// Generate a 1 x 1 pixel image

$im = @imagecreate(1, 1)
or die("<!-- Cannot Initialize new GD image stream -->");

// Set up color if not provided

if (!strlen(trim($color))) $color = "000000";

// Set image color

$red = 0 + base_convert(substr($color, 0, 2), 16, 8);
$green = 0 + base_convert(substr($color, 2, 2), 16, 8);
$blue = 0 + base_convert(substr($color, 4, 2), 16, 8);
$img_color = imagecolorallocate($im, $red, $green, $blue);

// Output the created image according to available GD image support

if (function_exists("imagegif")) {
header("Content-type: image/gif");
imagegif($im);
} elseif (function_exists("imagejpeg")) {
header("Content-type: image/jpeg");
imagejpeg($im);
} elseif (function_exists("imagepng")) {
header("Content-type: image/png");
imagepng($im);
} elseif (function_exists("imagewbmp")) {
header("Content-type: image/vnd.wap.wbmp");
imagewbmp($im);
} else {
die("<!-- No image support on this PHP server -->");
}

Dec 28 '06 #4

Webtechnics - Info wrote:
you CAN'T set the image-source to a .php file.
of course you can, he said it worked before and i also had it working

Dec 28 '06 #5
Might be the http header

CPD wrote:
I have an image-generating script myimage.php that's worked and that I've
used for years. All it does is increment a counter file and output a single
pixel in the color I provide. But on one particular web server I uploaded
to, the behavior is strange.

If I enter the URL http://www.domain.ext/myimage.php, the picture is
displayed as expected. But if I insert the script into HTML, like <img
src="myimage.php">, it fails to display and I get the broken image icon,
instead.

This has never happened before, but I do not know enough about PHP builds
and web server settings to figure this out.

Any suggestions? Thanks for any help!

Here's the code, if necessary:

$filename = trim($_GET['filename']);
$color = (trim($_GET['color']) == "") ? "000000" : trim($_GET['color']);

$count = '0';
$file_last_mod = 0;

// Set up filename whether or not provided

if(strlen($filename)) {
$filename .= ".count";
}
else {
$fname_end = strrpos($script_name, ".");
$fname_end = ($fname_end === false) ? strlen($script_name) :
$fname_end - 1;
$slash_pos = strrpos($script_name, "/");
$fname_start = ($slash_pos === false) ? 0 : $slash_pos + 1;
$script_name = (!strlen($filename)) ? $HTTP_SERVER_VARS['SCRIPT_NAME'] :
$filename;
$filename = substr($script_name, $fname_start, $fname_end - $slash_pos)
.. ".count";
}

// open and read file if it exists

if (file_exists($filename)) {
$file_last_mod = filemtime($filename);
$count = file_get_contents($filename);
}

// increment count

$count += 1;

// reopen file and write new count

$handle = fopen($filename, "w");
fwrite($handle, $count);
fclose($handle);

// Generate a 1 x 1 pixel image

$im = @imagecreate(1, 1)
or die("<!-- Cannot Initialize new GD image stream -->");

// Set up color if not provided

if (!strlen(trim($color))) $color = "000000";

// Set image color

$red = 0 + base_convert(substr($color, 0, 2), 16, 8);
$green = 0 + base_convert(substr($color, 2, 2), 16, 8);
$blue = 0 + base_convert(substr($color, 4, 2), 16, 8);
$img_color = imagecolorallocate($im, $red, $green, $blue);

// Output the created image according to available GD image support

if (function_exists("imagegif")) {
header("Content-type: image/gif");
imagegif($im);
} elseif (function_exists("imagejpeg")) {
header("Content-type: image/jpeg");
imagejpeg($im);
} elseif (function_exists("imagepng")) {
header("Content-type: image/png");
imagepng($im);
} elseif (function_exists("imagewbmp")) {
header("Content-type: image/vnd.wap.wbmp");
imagewbmp($im);
} else {
die("<!-- No image support on this PHP server -->");
}

Dec 28 '06 #6
..oO(Webtechnics - Info)
>I also had this problem, I can;t remember exactly how i solved it, but i
remember that you CAN'T set the image-source to a .php file.
That's wrong. The value of the 'src' attribute is a URL, and a URL just
points to a resource somewhere on a network. It doesn't matter if the
referenced resource is a static file and delivered as-is or if it is
generated by a script. A ".php" in a URL is absolutely meaningless.

Micha
Dec 28 '06 #7
Ric
CPD schrieb:
I have an image-generating script myimage.php that's worked and that I've
used for years. All it does is increment a counter file and output a single
pixel in the color I provide. But on one particular web server I uploaded
to, the behavior is strange.

If I enter the URL http://www.domain.ext/myimage.php, the picture is
displayed as expected. But if I insert the script into HTML, like <img
src="myimage.php">, it fails to display and I get the broken image icon,
instead.

This has never happened before, but I do not know enough about PHP builds
and web server settings to figure this out.

Any suggestions? Thanks for any help!

Here's the code, if necessary:

$filename = trim($_GET['filename']);
$color = (trim($_GET['color']) == "") ? "000000" : trim($_GET['color']);

$count = '0';
$file_last_mod = 0;

// Set up filename whether or not provided

if(strlen($filename)) {
$filename .= ".count";
}
else {
$fname_end = strrpos($script_name, ".");
$fname_end = ($fname_end === false) ? strlen($script_name) :
$fname_end - 1;
$slash_pos = strrpos($script_name, "/");
$fname_start = ($slash_pos === false) ? 0 : $slash_pos + 1;
$script_name = (!strlen($filename)) ? $HTTP_SERVER_VARS['SCRIPT_NAME'] :
$filename;
$filename = substr($script_name, $fname_start, $fname_end - $slash_pos)
. ".count";
}

// open and read file if it exists

if (file_exists($filename)) {
$file_last_mod = filemtime($filename);
$count = file_get_contents($filename);
}

// increment count

$count += 1;

// reopen file and write new count

$handle = fopen($filename, "w");
fwrite($handle, $count);
fclose($handle);

// Generate a 1 x 1 pixel image

$im = @imagecreate(1, 1)
or die("<!-- Cannot Initialize new GD image stream -->");

// Set up color if not provided

if (!strlen(trim($color))) $color = "000000";

// Set image color

$red = 0 + base_convert(substr($color, 0, 2), 16, 8);
$green = 0 + base_convert(substr($color, 2, 2), 16, 8);
$blue = 0 + base_convert(substr($color, 4, 2), 16, 8);
$img_color = imagecolorallocate($im, $red, $green, $blue);

// Output the created image according to available GD image support

if (function_exists("imagegif")) {
header("Content-type: image/gif");
imagegif($im);
} elseif (function_exists("imagejpeg")) {
header("Content-type: image/jpeg");
imagejpeg($im);
} elseif (function_exists("imagepng")) {
header("Content-type: image/png");
imagepng($im);
} elseif (function_exists("imagewbmp")) {
header("Content-type: image/vnd.wap.wbmp");
imagewbmp($im);
} else {
die("<!-- No image support on this PHP server -->");
}

Dec 29 '06 #8
CPD
Folks, thanks for all the good tips and discussion. It got my brain
cranking, and it turned out to be a permissions issue. Always took for
granted that perms 446 was sufficient, but somehow on this server, it had to
be 447. Not sure why because the counter file is only read from and written
to, but it works now!

Thanks again, and have a Happy New Year!

"CPD" <no@spam.plswrote in message
news:PO******************************@comcast.com. ..
>I have an image-generating script myimage.php that's worked and that I've
used for years. All it does is increment a counter file and output a single
pixel in the color I provide. But on one particular web server I uploaded
to, the behavior is strange.

If I enter the URL http://www.domain.ext/myimage.php, the picture is
displayed as expected. But if I insert the script into HTML, like <img
src="myimage.php">, it fails to display and I get the broken image icon,
instead.

This has never happened before, but I do not know enough about PHP builds
and web server settings to figure this out.

Any suggestions? Thanks for any help!

Here's the code, if necessary:

$filename = trim($_GET['filename']);
$color = (trim($_GET['color']) == "") ? "000000" : trim($_GET['color']);

$count = '0';
$file_last_mod = 0;

// Set up filename whether or not provided

if(strlen($filename)) {
$filename .= ".count";
}
else {
$fname_end = strrpos($script_name, ".");
$fname_end = ($fname_end === false) ? strlen($script_name) :
$fname_end - 1;
$slash_pos = strrpos($script_name, "/");
$fname_start = ($slash_pos === false) ? 0 : $slash_pos + 1;
$script_name = (!strlen($filename)) ? $HTTP_SERVER_VARS['SCRIPT_NAME']
: $filename;
$filename = substr($script_name, $fname_start, $fname_end - $slash_pos)
. ".count";
}

// open and read file if it exists

if (file_exists($filename)) {
$file_last_mod = filemtime($filename);
$count = file_get_contents($filename);
}

// increment count

$count += 1;

// reopen file and write new count

$handle = fopen($filename, "w");
fwrite($handle, $count);
fclose($handle);

// Generate a 1 x 1 pixel image

$im = @imagecreate(1, 1)
or die("<!-- Cannot Initialize new GD image stream -->");

// Set up color if not provided

if (!strlen(trim($color))) $color = "000000";

// Set image color

$red = 0 + base_convert(substr($color, 0, 2), 16, 8);
$green = 0 + base_convert(substr($color, 2, 2), 16, 8);
$blue = 0 + base_convert(substr($color, 4, 2), 16, 8);
$img_color = imagecolorallocate($im, $red, $green, $blue);

// Output the created image according to available GD image support

if (function_exists("imagegif")) {
header("Content-type: image/gif");
imagegif($im);
} elseif (function_exists("imagejpeg")) {
header("Content-type: image/jpeg");
imagejpeg($im);
} elseif (function_exists("imagepng")) {
header("Content-type: image/png");
imagepng($im);
} elseif (function_exists("imagewbmp")) {
header("Content-type: image/vnd.wap.wbmp");
imagewbmp($im);
} else {
die("<!-- No image support on this PHP server -->");
}

Dec 29 '06 #9
Webtechnics - Info wrote:
I also had this problem, I can;t remember exactly how i solved it, but i
remember that you CAN'T set the image-source to a .php file.

If i now more, i'l let you know.
Yea, that simply isn't correct. I use .php files to send images with no problem.
The PHP script simply needs to generate the correct header before it writes any
data. For a JPEG, you'd include a line like this in your script:

header ('Content-Type: image/jpeg');
If your script is called foo.php, you can load the image like this:

<img src="foo.php" alt="A dynamic image." />

The browser will use the header rather than the extension to determine the file
type. PHP can pass any type of data to the browser using this method. One
application I wrote passes Word, Excel, PDF, HTML, JPEG, GIF, and PNG files
through the same wrapper script, and could pass anything that has a mime type by
placing the correct mime type into the database. PHP can output just about
anything to a web browser.
Jan 4 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: lawrence | last post by:
I haven't been able to reach www.php.net for days. Most of the rest of the web is working for me, though I've bad trouble reaching any English sites. Anyone else having trouble?
60
by: English Teacher | last post by:
Which would be more useful to learn, PHP or COLDFUSION? I know Coldfusion is popular in the work force. Is PHP? Thanks!
14
by: saayan | last post by:
Hi, I am using PHP 5.0.1 with Apache 2 on Win XP (SP2). My index.php file has require_once contents.php and also for functions.php. My contents.php file also has a require_once for...
0
by: tsivaraman | last post by:
I am trying to build php-5.2.1 in RedHat Linux 9. I have installed libxml2-2.6.11,mysql-5.0.33,httpd-2.2.4(apache) successfully.When i do 'make' from the php directory,i get the following...
0
by: Benjamin Grieshaber | last post by:
Hi, I´m on SuSE 9.3 with xmlrpc-c and xmlrpc-c-devel installed (ver. 0.9.10) I tried to compile php with xmlrpc support and got the following errors: ...
9
by: mekalai82 | last post by:
i have information.php file that file contain following coding <?php echo phpinfo(); ?> while i calling the URL ("http://localhost/information.php"). i am getting the coding <?php echo...
0
by: Patriot89 | last post by:
I have a quick question in reference to php file extenstions... I have code for example like this... This is all located on this site www.ixalliance.com/BHS/Default (This is my nav.php file) ...
4
by: mechphisto | last post by:
I'm working on a friend's box, Fedora Core 6. It has PHP 5.1.6. I need to install mcrypt into it, and the only way I can find to do it is from source then recompile PHP. So I did all that, and got...
5
Chrisjc
by: Chrisjc | last post by:
Good afternoon, I am seeking some php configuration help. Here is the run down I am running Windows server 2003 and IIS V6.0 I have never had issues before until now. I have Symantec Antivirus 11.0...
1
by: gnawz | last post by:
Hi guys, I have a couple of php files that perform various tasks. I will use fields in my system and provide code as well I need help as follows: My database contains the fields Category...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.