Php generate text code on the image _ PHP Tutorial

Source: Internet
Author: User
Tags base64 encode
Php generates text code on the image. In the php Tutorial, if you want to use its image processing function, you must go to php. enable the gd Library in ini, * send the header file header (content-type: imagepng); create an image. if it fails to be output to the php Tutorial, you must use the image processing function in php. enable the gd Library in ini,
*/
// Send the header file
Header ("content-type: image/png ");
// Create an image. if the output fails
$ Im = @ imagecreate (150,50) or die ("cannot initialize new gd image stream ");
// Define the background color
$ Background_color = imagecolorallocate ($ instant, 255,255,255 );
// Define the text color
$ Text_color = imagecolorallocate ($ im, 233,14, 91 );
// Draw a file on the image
Imagestring ($ im, 3,5, 5, "hello world", $ text_color );
// Output image file
Imagepng ($ im );
// Destroy the image
Imagedestroy ($ im );
/*
The code execution result 22.5 is as follows:
*/

// 2. the image proportion is reduced.

// Define a file
Using filename='1.jpg ';
// Define the zoom percentage
$ Percent = 0.5;
// Output header file
Header ('content-type: image/jpeg ');
// Obtain the new size
List ($ width, $ height) = getimagesize ($ filename );
$ Newwidth = $ width * $ percent;
$ Newheight = $ height * $ percent;
// Create a graphic area and load the image
$ Thumb = imagecreatetruecolor ($ newwidth, $ newheight );
$ Source = imagecreatefromjpeg ($ filename );
// Adjust the size again
Imagecopyresized ($ thumb, $ source, 0, 0, 0, 0, $ newwidth, $ newheight, $ width, $ height );
// Output image
Imagejpeg ($ thumb );
/*
Run the code to scale the original image by 50% and output the new image.
*/

// Write text on the image

// Define content
$ Data = 'ivborw0kggoaaaansuheugaaabwaaaascamaaab/2u7waaaabl '.
'Bmveuaaad // + l2z/daaaasuleqvr4xqwquqoaiaxc2/0vxzdr '.
'Ex4ijtrkb7lobnustxsb0jixiamssqnwlsv + wulf4avk9flq2r '.
'8a5hse35q3eo2xp1a1wqkzsgetvdtkdqaaaabjru5erkjggg = ';
// Base64 encode the content
$ Data = base64_decode ($ data );
// Create an image based on the string
$ Im = imagecreatefromstring ($ data );
If ($ im! = False)
{
// If the image is created successfully, the image is output.
Header ('content-type: image/png ');
Imagepng ($ im );
}
Else
{
// If creation fails, the output content
Echo 'a error occured .';
}
/*
The code execution result 22.4 is as follows:
*/

// Write text on the image

Header ("content-type: image/png ");
// Create an image. if the output fails
$ Im = @ imagecreate (100,50) or die ("cannot initialize new gd image stream ");
// Define the background color
$ Background_color = imagecolorallocate ($ instant, 255,255,255 );
// Define the text color
$ Text_color = imagecolorallocate ($ im, 233,14, 91 );
// Draw a file on the image
Imagestring ($ im, 1, 5, 5, "a simple text string", $ text_color );
// Output image file
Imagepng ($ im );
// Destroy the image
Imagedestroy ($ im );
/*
Execute this code to generate a jpeg image.
And output the specified string
*/

Response, * /// send the header file header (content-type: image/png); // create an image, if the output fails...

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.