Using PHP to automatically generate business cards with user information _php tips

Source: Internet
Author: User
Tags learn php

Objective

Whether you want to add character characters to your self-portrait on the meticulously p, or photography enthusiasts to add a watermark in the shot, or in the Web page or mobile applications in real-time generation of text and pictures of the combination, we need to find a suitable and reliable way to the picture and text perfectly together.

So, "the best programming Language" PHP and its GD library provide us with such a simple and efficient solution.

I know, this time there is an example is the best ~ below let me give a simple chestnut!

Goal

Combine the personal information entered in the Web application with the picture to generate a card with the user information printed.

Effect

1 preset a picture, as a business card background


Business Card Background

2 User Information input


Information input

3-Piece generation (after clicking Continue)


Business Card Generation

Steps

Before you begin, use phpinfo() command to confirm that the GD library is installed.

First of all, what we want to do is very simple, create a PHP file, call it main.php bar.

In main.php, in theory we need the following lines of code ( This code is based on Cake 3):

<?php//Create a form to provide user input function echo $this->form->create ();
Only the Value property is reserved, and the other properties have omitted the Echo $this->form->input (' Name ', [' => $firstname, ' ... ' => ' ... ', ...]);
echo $this->form->input (' Last Name ', [' Value ' => $lastname, ' ... ' => ' ... ', ...]);
echo $this->form->input (' City ', [' Value ' => $city, ' ... ' => ' ... ', ...]);

echo $this->form->input (' state ', [' Value ' => $state, ' ... ' => ' ... ', ...]); 
* * The IMG tag is directed to a PHP file called image.php.
* All user input information will be stored in the form of a URL in the src attribute of the img tag, * can be obtained by image.php after the form is submitted.
* All variables have been converted to uppercase with PHP's Strtoupper function. * * echo $this->html->image ("image.php?first=". Strtoupper ($firstname).
&last= ". Strtoupper ($lastname). "&location=". Strtoupper ($city). "
". Strtoupper ($state), [' Fullbase ' => true]); * * The above code is equivalent to: *  *//Form submit echo $this->form->butt 

On (' CONTINUE ', [' type ' => ' submit ']); Close the form $this->form->end ();

Next, it's the image.php that created where amazing happens. Include picture settings, get user information, add text information to the picture, and everything will be done in this step.

Let's go through the code to find out.

The <?php/header () function sends the original HTTP header to the client.

Header (' Content-type:image/jpeg '); 

Create a new image from a file or URL, where the text.jpg is the preset picture shown in effect 1 $RIMG = Imagecreatefromjpeg ("test.jpg");

Assign color to an image $cor = imagecolorallocate ($rImg, 0, 0, 0);

We can customize the text font combined with the picture $font = "./arial.ttf";
Sets the width of the resulting picture and the height of the text relative to the picture $imgWidthSetting = 676;

$textHeightOffset = 220;
Decode main.php the IMG tag encoded URL string, which includes the location $first = UrlDecode ($_get[' a ');
$last = UrlDecode ($_get[' last ']);

$location = UrlDecode ($_get[' location ')); * * Get the range of text you want to add to the picture.
Imagettfbbox () returns an array of 8 * units representing the Four corners of the text's outer frame.
* * $width _first = imagettfbbox (0, $font, $first);
$width _last = Imagettfbbox (0, $font, $last);

$width _location = Imagettfbbox (0, $font, $location);
The $width _ variable [2] represents the x-coordinate position of the lower right corner $x _first = ceil ($imgWidthSetting-$width _first[2]); 
$x _last = ceil ($imgWidthSetting-$width _last[2]);

$x _location = ceil ($imgWidthSetting-$width _location[2]); Use the Imagettftext () function to "write" the text to the image Imagettftext ($rImg, 0, $x_first, $textHeightOffset +120, $cor, $font, $first);
Imagettftext ($rImg, 0, $x _last, $textHeightOffset +210, $cor, $font, $last);

Imagettftext ($rImg, 0, $x _location, $textHeightOffset +290, $cor, $font, $location); 

Output image to browser or file, quality as optional, range from 0 (worst quality, file smaller) to 100 (best quality, File max) imagejpeg ($RIMG, null,100);
Free up Memory, Imagedestroy () frees the memory associated with the image. Imagedestroy ($RIMG);

Here we have completed all the steps to add text to the picture through PHP. When the user enters personal information and submits the form, the business card with effect 3 is automatically generated by image.php.

Summarize

Using PHP to combine text with pictures is simple, but can be derived from a number of different application methods. For example, we can hide the background of the business card and the generated business card, and only display the information input interface to the user. At the same time, we access the printer's API in code. In this way, when users enter their own information click to continue, the display of user information card will be directly printed out. This works well for users participating in the offline party activity check in.

In short, PHP as "the Best programming language", coding is not uniform, performance is criticized, grammar is not rigorous. Indeed, PHP slot too much, enough for us to black three days and three nights. But we have to admit that PHP and its vast built-in library of functions provide a convenient and powerful tool for Web developers. You see, a few simple functions, you can achieve the perfect combination of text and pictures, so PHP still has a lot of people support. I hope this article for you to learn PHP help.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.