By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,442 Members | 1,279 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,442 IT Pros & Developers. It's quick & easy.

GD Library Problem

P: 18
ok let met tell u what is the problem , i have downloaded the GD library code from interent and when i am using it the picture is not appearing on the page. i don't know why ?

i am using PHP 4.3.9 on Appache server under linux operating system ( fedora 3).
though the same code it is working on the localhost (WAMP) with php 5.2 and apche 2.2
so plz if someone have an idea about the problem plz answer me.





Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. session_start();
  3.  
  4. if( isset($_POST['submit'])) {
  5. if( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) {
  6. // Insert you code for processing the form here, e.g emailing the submission, entering it into a database. 
  7. echo 'Thank you. Your message said "'.$_POST['message'].'"';
  8. unset($_SESSION['security_code']);
  9. } else {
  10. // Insert your code for showing an error message here
  11. echo 'Sorry, you have provided an invalid security code';
  12. }
  13. } else {
  14. ?>
  15.  
  16. <form action="form.php" method="post">
  17. <label for="name">Name: </label><input type="text" name="name" id="name" /><br />
  18. <label for="email">Email: </label><input type="text" name="email" id="email" /><br />
  19. <label for="message">Message: </label><textarea rows="5" cols="30" name="message" id="message"></textarea><br />
  20. <img src="CaptchaSecurityImages.php width=100&height=40&characters=5" /><br />
  21. <label for="security_code">Security Code: </label><input id="security_code" name="security_code" type="text" /><br />
  22. <input type="submit" name="submit" value="Submit" />
  23. </form>
  24.  
  25. <?php
  26. }
  27. ?>
------------the second code----------------------
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3.  
  4. /*
  5. * File: CaptchaSecurityImages.php
  6. * Author: Simon Jarvis
  7. * Copyright: 2006 Simon Jarvis
  8. * Date: 03/08/06
  9. * Updated: 07/02/07
  10. * Requirements: PHP 4/5 with GD and FreeType libraries
  11. * Link: http://www.white-hat-web-design.co....php-captcha.php
  12. * This program is free software; you can redistribute it and/or 
  13. * modify it under the terms of the GNU General Public License 
  14. * as published by the Free Software Foundation; either version 2 
  15. * of the License, or (at your option) any later version.
  16. * This program is distributed in the hope that it will be useful, 
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of 
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
  19. * GNU General Public License for more details: 
  20. * http://www.gnu.org/licenses/gpl.html
  21. *
  22. */
  23.  
  24. class CaptchaSecurityImages {
  25.  
  26. var $font = 'monofont.ttf';
  27.  
  28. function generateCode($characters) {
  29. /* list all possible characters, similar looking characters and vowels have been removed */
  30. $possible = '23456789bcdfghjkmnpqrstvwxyz';
  31. $code = '';
  32. $i = 0;
  33. while ($i < $characters) { 
  34. $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
  35. $i++;
  36. }
  37. return $code;
  38. }
  39.  
  40. function CaptchaSecurityImages($width='120',$height='40',$c haracters='6') {
  41. $code = $this->generateCode($characters);
  42. /* font size will be 75% of the image height */
  43. $font_size = $height * 0.75;
  44. $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');
  45. /* set the colours */
  46. $background_color = imagecolorallocate($image, 255, 255, 255);
  47. $text_color = imagecolorallocate($image, 20, 40, 100);
  48. $noise_color = imagecolorallocate($image, 100, 120, 180);
  49. /* generate random dots in background */
  50. for( $i=0; $i<($width*$height)/3; $i++ ) {
  51. imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
  52. }
  53. /* generate random lines in background */
  54. for( $i=0; $i<($width*$height)/150; $i++ ) {
  55. imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
  56. }
  57. /* create textbox and add text */
  58. $textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
  59. $x = ($width - $textbox[4])/2;
  60. $y = ($height - $textbox[5])/2;
  61. imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function');
  62. /* output captcha image to browser */
  63. header('Content-Type: image/jpeg');
  64. imagejpeg($image);
  65. imagedestroy($image);
  66. $_SESSION['security_code'] = $code;
  67. }
  68.  
  69. }
  70.  
  71. $width = isset($_GET['width']) ? $_GET['width'] : '120';
  72. $height = isset($_GET['height']) ? $_GET['height'] : '40';
  73. $characters = isset($_GET['characters']) && $_GET['characters'] > 1 ? $_GET['characters'] : '6';
  74.  
  75. $captcha = new CaptchaSecurityImages($width,$height,$characters); 
  76.  
  77. ?>
Jul 21 '07 #1
Share this Question
Share on Google+
5 Replies


pbmods
Expert 5K+
P: 5,821
Heya, Akkad.

Is your script generating any errors? See this thread.
Jul 21 '07 #2

kovik
Expert 100+
P: 1,044
Your image src doesn't make sense. Did you forget to put a question mark at the start of your query string?
Jul 21 '07 #3

nathj
Expert 100+
P: 938
Hi,

I have recently deployed this code and I had to make s lightchange to it. The line that defines $_SESSION['secutiy_code'] works a lot better and more reliably if you move it from CaptchSecurityImages() to generateCode(), immediatly before the return.

This was the only way I could get the $_SESSION to validate later on.

I just thought I would pass this on as a quick tip.

As for the image not displaying it is also important that the CaptchaSecurityImages.php file is in the same directory as the php page that is using it.

Finally, double check whether the GD libraries are installed correctly and are available. This caused me a bit of bother at first but now it works just fine.

These are all just in case adding '?' into you img src doesn't work. That should definitley be there

Cheers
nathj
Jul 23 '07 #4

P: 18
Hi everyboy , thanx for the replies:

1. the quesiton mark is there when i am calling the CaptchaSecurityImages.php:
< img src="CaptchaSecurityImages.php?width=100&height=40 &characters=5" />
2.i have made the changes related to the $_SESSION['security_code'] ;
3. also i added the lines:
error_reporting(E_ALL);
ini_set('display_errors', True);
but there is no error in the form page , or in the from processing page.
4.issues related to GD library installation checking , if some one can just inform me about the installation of GD library on Fedora and what are the libraries needed for proper working of this library and how to enable it from php.ini , in detailed way plz
Jul 23 '07 #5

kovik
Expert 100+
P: 1,044
If you aren't getting any errors, GD is likely working fine.
Jul 23 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.