ScottS Posted October 31, 2007 Share Posted October 31, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/75560-solved-using-php-to-generate-dynamic-image-for-browser/ Share on other sites More sharing options...
Barand Posted October 31, 2007 Share Posted October 31, 2007 Missing ";" at end of imagefill() line. If you comment out the header() line and run the script you should get error messages Quote Link to comment https://forums.phpfreaks.com/topic/75560-solved-using-php-to-generate-dynamic-image-for-browser/#findComment-382277 Share on other sites More sharing options...
ScottS Posted November 1, 2007 Author Share Posted November 1, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/75560-solved-using-php-to-generate-dynamic-image-for-browser/#findComment-382704 Share on other sites More sharing options...
Barand Posted November 1, 2007 Share Posted November 1, 2007 Yes. See my last post Quote Link to comment https://forums.phpfreaks.com/topic/75560-solved-using-php-to-generate-dynamic-image-for-browser/#findComment-382707 Share on other sites More sharing options...
ScottS Posted November 1, 2007 Author Share Posted November 1, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/75560-solved-using-php-to-generate-dynamic-image-for-browser/#findComment-382715 Share on other sites More sharing options...
Barand Posted November 1, 2007 Share Posted November 1, 2007 Are you running testpage.php or testpage.htm after commenting out the header function? Quote Link to comment https://forums.phpfreaks.com/topic/75560-solved-using-php-to-generate-dynamic-image-for-browser/#findComment-382719 Share on other sites More sharing options...
ScottS Posted November 1, 2007 Author Share Posted November 1, 2007 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/75560-solved-using-php-to-generate-dynamic-image-for-browser/#findComment-382723 Share on other sites More sharing options...
Barand Posted November 1, 2007 Share Posted November 1, 2007 That code ^ worked fine Quote Link to comment https://forums.phpfreaks.com/topic/75560-solved-using-php-to-generate-dynamic-image-for-browser/#findComment-382893 Share on other sites More sharing options...
ScottS Posted November 1, 2007 Author Share Posted November 1, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/75560-solved-using-php-to-generate-dynamic-image-for-browser/#findComment-382986 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.