Jump to content

PHP GD issue


bullbreed

Recommended Posts

I am trying to create an image using PHP and GD library.

What I want to do is;

 

1. User inputs required text

2. User selects a font from list

3. PHP and GD creates the image then saves it to the database in uploads folder

4. The generated image appears on screen after being called from the database

 

The following code I did does most of this but the user form for this is on a page called productdisplay.php and the php script is on create text.php so when the image is created it doesn't yet save it but displays it on screen on create-text.php

 

When the user runs the script I want to stay in the productdisplay.php page, save the image to the server then call it to the page.

 

Heres my code

<?php

//Report any errors
ini_set("display_errors", "1");
error_reporting(E_ALL);

// Create a 165x45 image
$im = imagecreatetruecolor(165, 45);
$white = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
$black = imagecolorallocate($im, 0x00, 0x00, 0x00);

// Get the Text String from the Text Field
$string = $_POST['input'];

// Get the Font Type from the Menu Field
$font = $_POST['fontselect'];

//Set the Path to the Font Directory
$font_file = 'fonts/';

// Make the background black
imagefilledrectangle($im, 0, 0, 164, 44, $black);

// Draw the Text from Text String using font size 14
imagefttext($im, 14, 0, 30, 30, $white, $font, $string);

// Output the image to the browser
header('Content-Type: image/png');

imagepng($im);

// Clear the image from memory
imagedestroy($im);
?>

 

What do I change to save the image, display on screen and stay on same page?

 

Thanks

 

B

Link to comment
Share on other sites

Thanks for that. I did a little tinkering around and got it working but I have a few questions that probably need an advanced user.

 

First of all here is the code

<?php

ini_set("display_errors", "1");
error_reporting(E_ALL);

//Report any errors

// Create a 165x45 image
$im = imagecreatetruecolor(165, 45);
$white = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
$black = imagecolorallocate($im, 0x00, 0x00, 0x00);

// Get the Text String from the Text Field
$string = $_POST['input'];

// Get the Font Type from the Menu Field
$font = $_POST['fontselect'];

//Set the Path to the Font Directory
$font_file = 'fonts/';

//Set the Path to the uploads folder
$location = 'uploads/test.png';

// Make the background black
imagefilledrectangle($im, 0, 0, 164, 44, $black);

// Draw the Text from Text String using font size 14
imagefttext($im, 14, 0, 30, 30, $white, $font, $string);

// Output the image to the browser
header('Content-Type: image/png');

//Save the image to the uploads folder
imagepng($im,$location);

// Clear the image from memory
imagedestroy($im);
?>

 

Question 1

I get a few notices and errors as below. How do I sort these

 

Warning: imagefttext() [function.imagefttext]: Could not find/open font in C:\xampp\htdocs\site\includes\prodtype.php on line 27

 

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\site\productdesign.php:11) in C:\xampp\htdocs\site\includes\prodtype.php on line 30

 

Question 2

I store the image that is created as 'test.png' in the 'uploads' folder but I tink I need to store it as something else because if 2 people are using the site at once the image will change to the latest users text so how would I store it related to the session? And would a user have to be logged in to start the session?

 

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.

Guest
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.