Jump to content

php image creation in another page.


Recommended Posts

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

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.