Jump to content

Create image via php - help


N1kol4N1k

Recommended Posts

Hi guys, i have problem with php. I want to make website which create baner for user when they fill out the form. This is banner (without text and image): http://misterijeforum.com/nik/images/medium.png

 

I made form also: http://misterijeforum.com/nik/small.html

 

But i got problems with php script. I'm not php programmer, but i created "something" :D

 

<?php
header('Content-Type: image/png');

$im = ImageCreateFromPng("images/medium.png"); 

$black = imagecolorallocate($im, 0, 0, 0);
$ime = "$fullname";
$interests = "%interests";
$school = "%school";
$fb = "%facebook";
$msn = "%msn";
$mail = "%mail";
$start_x = 1;
$start_y = 1;
Imagettftext($im, 12, 0, $start_x, $start_y, $black, 'verdana.ttf', $ime);

Imagepng($im);

ImageDestroy($im);


?>

I made just for full name, but i don't know how to storage that new image in folder "images" and show image to user. And is this code good ?

 

Link to comment
Share on other sites

I know that code is bad I just found it on internet and modified it. Can you explain me how to do that or better to give me code. So i want to, when user fill out the form and click on "submit", it generate benner with informations which user write in form and that picture/banner storage in folder "images". Backgroud of banner will be www.misterijeforum.com/nik/images/medium.png . Thank you so much

Edited by N1kol4N1k
Link to comment
Share on other sites

OK, I was in a good mood. A quick google search directed me to a tutorial on this very site (http://www.phpfreaks.com/tutorial/php-add-text-to-image) which I used to create the following:

 

 

<?php

error_reporting(E_ALL);

//Set values based upon form POST data
$fullname = "Ricardo Montobaln";
$location = "Denver, Colorado";
$school = "School of Hard Knocks";
$facebook = "www.facebook.com/myprofile";
$email = "myname@domain.com";

//Path to image
$imageName = 'images/medium.png';

//Font face and size
$font  = 'verdana.ttf';
$fontSize = 10;
$spacing = 22; //horizontal spacing between lines

//Create image object
$image = ImageCreateFromPng($imageName );

//Pick color for the text
$fontColor = imagecolorallocate($image, 255, 255, 255);

//Set start x & y positions
$x = 75;
$y = 16;

//Add name text
imagettftext($image, $fontSize, 0, $x, $y, $fontColor, $font, $fullname);
//Move y position down and add location text
imagettftext($image, $fontSize, 0, $x, $y+=$spacing, $fontColor, $font, $location);
//Move y position down and add school text
imagettftext($image, $fontSize, 0, $x, $y+=$spacing, $fontColor, $font, $school);
//Move y position down and add facebook text
imagettftext($image, $fontSize, 0, $x, $y+=$spacing, $fontColor, $font, $facebook);
//Move y position down and add email text
imagettftext($image, $fontSize, 0, $x, $y+=$spacing, $fontColor, $font, $email);

header('Content-Type: image/png');

//Output image to the browser
imagepng($image);

//Delete the image resource
imagedestroy($image);

?>

 

Note: You set the position of the first line using the values for $x and $y. Then the  $spacing variable is used to move down to the next line after you place a line of text. That is done by using "$y+=$spacing" inside the function call for imagettftext(). That will increment $y by the value of $spacing.

 

EDIT: you may want to think abut reducing the font size. Otherwise values that are a little long will go past the right edge of the black area. Or you could make the image wider.

Edited by Psycho
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.