Jump to content


This topic is now archived and is closed to further replies.


php image creation in another page.

Recommended Posts

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, [code]header("Content-Type: image/png");[/code]

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

Share this post

Link to post
Share on other sites
Is it something like this that you want?


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.

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">
else {
<img src="image.php?name=<?php echo $_POST['name'] ?>">
<br><br><a href="test.php">Try again</a>

Here's the code for image.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);

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


Share this post

Link to post
Share on other sites


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.