Jump to content

[SOLVED] Using PHP to generate dynamic image for browser


Recommended Posts

I'm new to PHP, but I am a C++ programmer.

I'm trying to implement an image verification similar to that used to register on this site.

I got some code that I like in how it is supposed to work, and everything seems to work correctly except I don't see the image ... I get a Red X in IE like the image is not found.

 

I tried creating the most basic test pages I could to see it I could get anything to appear.

 

testpage.htm:

<html>
<HEAD>

	<TITLE>Test Page</TITLE>

</HEAD>

<BODY>
	<hr/>
	<img src="testpage.php" />
	<hr/>
</BODY>
</html>

 

testpage.php:

<?php
$width = 120;
$height = 40;
$image = imagecreatetruecolor($width, $height);

$bgColor = imagecolorallocate ($image, 0, 128, 0);
imagefill($image, 0, 0, $bgColor)

header('Content-type: image/jpeg');
imagejpeg($image);

imagedestroy($image);
?>

 

I asked my hosting service if PHP was GD enabled and got this response:

We do have GD on the server.

 

[root@gator177 ~]# php -i  | grep gd

Configure Command =>  './configure' '--with-apxs=/usr/local/apache/bin/apxs' '--prefix=/usr/local' '--with-xml' '--enable-bcmath' '--enable-calendar' '--with-curl' '--with-dom' '--with-dom-xslt' '--with-dom-exslt' '--enable-exif' '--enable-ftp' '--with-gd' '--with-jpeg-dir=/usr/local' '--with-png-dir=/usr' '--with-xpm-dir=/usr/X11R6' '--with-gettext' '--with-imap=/usr/local/imap-2004g' '--enable-mbstring' '--enable-mbstr-enc-trans' '--enable-mbregex' '--with-mcrypt' '--enable-memory-limit' '--with-mhash' '--enable-magic-quotes' '--with-mysqli' '--with-mysql=/usr' '--with-openssl' '--enable-discard-path' '--with-pear' '--enable-xslt' '--with-xslt-sablot' '--enable-sockets' '--enable-track-vars' '--with-ttf' '--with-freetype-dir=/usr' '--enable-gd-native-ttf' '--enable-versioning' '--enable-wddx' '--with-xmlrpc' '--with-zlib'

gd

 

Can anyone tell me how to determine where/why this is failing?

Dang, I missed that!

I wonder how I did that since I copy and pasted most of the lines from the real code to this test code.

I fixed that and my test worked, so I guess GD is working on the server.

 

I went through the actual code and it has all the semicolons on the lines so that isn't the problem.

 

Guess I'll go back to my test code and slowly keep adding parts back in from the original code to see if I can figure out what isn't working.

 

It there a way to test if the calls fail and display a message in the page?

I tried that, and I don't get any messages displayed in the browser page.

I just get the red X instead of the image.

I also just check the "error log" that I can see in cPanel (on the hosting service) and it's most recent errors are just "File not found".

 

Can I add code to the script to test each function and output a specific message that will show up in the page?

I didn't think so, because the call is being made from an IMG tag so anything other than an image wouldn't display right?

But as, I'm new to PHP and really to everything beyond plain HTML for websites, I figure that there is probably a way even if it involves writing to a log file myself.

 

Oh, and THANKS for you help!

In my original post I include the entire code for my test page (testpage.htm & testpage.php).

There is only 1 header() call, and it is in testpage.php.

 

Also, I added a little more code to the testpage.php and it looked good at first, but when I started refreshing the page, sometimes it worked and other times it failed, gave me the red X and put this in the error log:

[Thu Nov  1 08:03:31 2007] [error] [client ...] Premature end of script headers: .../public_html/testpage.php

 

Here is the current code for testpage.php:

<?php
$width = 120;
$height = 40;
$image = imagecreate($width, $height);
$bgColor = imagecolorallocate ($image, 255, 255, 255);
$textColor = imagecolorallocate ($image, 0, 0, 0);
imagefill($image, 0, 0, $bgColor);
// Add Random noise
for ($i = 0; $i < 250; $i++) {
        $rx1 = rand(0,$width);
        $rx2 = rand(0,$width);
        $ry1 = rand(0,$height);
        $ry2 = rand(0,$height);
        $rcVal = rand(0,255);
        $rc1 = imagecolorallocate($image, rand(0,255), rand(0,255), rand(100,255));
        imageline ($image, $rx1, $ry1, $rx2, $ry2, $rc1);
}

// send the content type header so the image is displayed properly
header('Content-type: image/jpeg');

imagejpeg($image);
imagedestroy($image);
?>

After a lot of pain I finally figured out that there were some strange characters (that couldn't be seen in my editor) at the ends of some lines of code.

I determined that because sometime hitting the End key put me at the beginning of the next line.

 

I cpied and pasted the contents of each file to MS-Word, then fixed the line breaks, then pasted into an empty editor and saved over each file.

 

Then it all magically worked.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.