Jump to content


Photo

php image creation in another page.


  • Please log in to reply
1 reply to this topic

#1 Crazydog115

Crazydog115
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 21 August 2006 - 01:33 AM

How would I include a form processor that creates an image in another webpage?

I can't just put the php in, since I need to reset the header to png,
header("Content-Type: image/png");

So how would I include it so I can change the headers?

#2 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 21 August 2006 - 09:05 AM

Is it something like this that you want?

http://www.dizzie.co.uk/php/test.php

This page (test.php) check's to see if a form value (in this instance 'name') is set.  If it's set then it calls image.php and displays the result on test.php, if not it displays the form on test.php.

Here's the code for the two pages, with correct content header types.

test.php
<?php
header("Content-Type: text/html");
if (!isset($_POST['name'])){
?>
<h4>Enter your firstname</h4>
<form action="test.php" method="post">
<input type="text" name="name">
<input type="submit" name="submit" value="Create Image">
<?php
}
else {
?>
<img src="image.php?name=<?php echo $_POST['name'] ?>">
<br><br><a href="test.php">Try again</a>
<?php
}
?>

Here's the code for image.php

<?php
   header("Content-type: image/png"); 
   $string = $_GET['name'];
   $im = imagecreatefrompng("../logos/pubutton.png");
   $height = imagesy($im);
   $halfheight = $height / 2;
   $width = imagesx($im); 
   $halfwidth = $width / 2;
   $fontsize = 48; 
   $angle = 0; 
   $font = "../logos/smartie.ttf";
   $bg = imagecolorallocate($im, 255,255,255); 
   $white = imagecolorallocate($im,255,255,255); 
   $black = imagecolorallocate($im,0,0,0); 
   $array  = imagettfbbox($fontsize,$angle,$font,$string);
   $textwidth = abs($array[2] - $array[0]);
   $halftextwidth = $textwidth / 2;
   $textheight = abs($array[5] - $array[3]);
   $halftextheight = $textheight / 2;
   $xpos = $halfwidth - $halftextwidth;
   $ypos = $halfheight + $halftextheight;
   imagettftext($im, $fontsize, $angle, $xpos, $ypos, $black, $font, $string);
   imagepng($im); 
   imagedestroy($im); 
?>

Obviously your image.php file would probably look a lot different from mine.

Regards
Rich
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users