Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-16-2012, 03:01 AM   PM User | #1
mase857
New to the CF scene

 
Join Date: Dec 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
mase857 is an unknown quantity at this point
imagejpeg() function won't output the image

Ok , so I'm learning how to use the GD library of PHP to generate polls , images , etc .. The thing is that when it comes to the image output using the imagejpeg function, nothing shows on the screen .. I'd really appreciate your help . For this exercise , I am typing text on a HTML forum , which needs to be put on an image using the GD library , basically putting text on an image . It's pretty straight-forward, I just don't understand why my image won't show .. Below is both the HTML form code and the PHP code .

HTML form.
Code:
<html>
<head> <title> Create Buttons </title> </head>
<body>
<form action ="button.php" method ="post">
<p> Type button text here </p>
<input type="text" name="button_text" size ="20" />
<p> Choose button color: </p>
<input type ="radio" name="color" value="red"> Red <br>
<input type ="radio" name="color" value="green"> Green <br>
<input type ="radio" name="color" value="blue"> Blue <br>
<input type ="submit" value ="Create Button" />
</form>

</body>
</html>
PHP Code:
<?php
Header
('Content-type: image/jpeg');
$DOCUMENT_ROOT $_SERVER['DOCUMENT_ROOT'];
$button_text $_POST['button_text']; var_dump($button_text);
$color $_POST['color']; var_dump($color);
 if(empty(
$color) || empty($button_text))
   {
      echo 
" Could not create image - form not filled correctly";
      exit;
      }
      
$path $DOCUMENT_ROOT."/uploads/$color-button.jpg";
 
$im imagecreatefromjpeg ($path);
 
$width_image imagesx($im);
 
$height_image imagesy($im);

 
$width_image_wo_margins $width_image-(2*18);
 
$height_image_wo_margins $width_image-(2*18);

 
$font_size 33;
 
putenv('GDFONTPATH=C:\Windows\Fonts');

 
$fontname getenv('GDFONTPATH') . '\comic.ttf';
 if(!
is_file($fontname))
{
    die( 
"Missing Font");
}

 do
  {
    
$font_size--;
    
$bbox imagettfbbox($font_size,0,$fontname,$button_text);
    
$right_text $bbox[2];
    
$left_text $bbox[0];
    
$width_text  $right_text -$left_text;
    
$height_text abs($bbox[7] - $bbox[1]);
    }

    while(
$font_size && ( $height_text $height_image_wo_margins || $width_text $width_image_wo_margins));

    if ( 
$height_text $height_image_wo_margins || $width_text $width_image_wo_margins)
       {

      echo 
"Text given wil not fit on button.<br />";
        }
    else 
    {
       
$text_y $width_image/2.0 $width_text/2.0;
       
$text_x $height_image/2.0 $height_text/2.0;

    if(
$left_text 0)
      {
      
$text_x += abs($left_text); //add factor for left overhang.  
      
}
      
$above_line_text abs($bbox[7]);
      
$text_y += $above_line_text;
      
$text_y -=2;
      
$white imagecolorallocate($im,255,255,255);
      
imagettftext($im,$font_size,0,$text_x,$text_y,$white,$fontname,$button_text);

      
imagejpeg($imNULL,75);

     }
  
imagedestroy($im);
?>

Last edited by Inigoesdr; 12-16-2012 at 05:48 AM..
mase857 is offline   Reply With Quote
Old 12-16-2012, 03:24 AM   PM User | #2
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Quote:
nothing shows on the screen..
Do you mean you have an entirely blank screen or just that the image doesn't display? Does View Source for the page reveal anything?

Please edit your code: the closing tag is [/ CODE] (without the space) and there is a separate button for PHP code-wrapping.
PHP Code:
putenv('GDFONTPATH=C:\Windows\Fonts');

$fontname getenv('GDFONTPATH') . '\comic.ttf'
You might need to double-escape \\ these, and access to this C-drive location may be an issue (even if just testing it locally). Added: I think you might need to save a font (.ttf) within your domain(?).
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS

Last edited by AndrewGSW; 12-16-2012 at 03:40 AM..
AndrewGSW is offline   Reply With Quote
Old 12-16-2012, 03:47 AM   PM User | #3
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Incorrect post deleted.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Reply

Bookmarks

Tags
php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:35 AM.


Advertisement
Log in to turn off these ads.