Php uses the GD library to implement text image watermarks and thumbnails tutorial-PHP source code

Source: Internet
Author: User
Thumbnails can be implemented through the gd library. Let's take a look at a simple example of using the GD library in php to implement text image watermarks and thumbnails. We hope this example can help you effectively. Thumbnails can be implemented through the gd library. Let's take a look at a simple example of using the GD library in php to implement text image watermarks and thumbnails. We hope this example can help you effectively.

Script ec (2); script


To use the gd library, you must first open the gd library, as shown below:

Enable GD library support for PHP in Windows

Find php. ini, open the content, and find:

; Extension = php_gd2.dll

Remove the Semicolon ";" at the beginning and save it. If there is no semicolon, it is enabled.


Specific can refer to the following: http://www.111cn.net/phper/php/48352.htm


I. How to add a text watermark

Require 'image. class. php'
$ Src = "001.jpg ";
$ Content = "hello ";
$ Font_url = "my. ttf ";
$ Size = 20;
$ Image = new Image ($ src );
$ Color = array (
0 => 255,
1 => 255,
2 => 255,
2 => 20
);
$ Local = array (
'X' => 20,
'Y' => 30
);
$ Angle = 10;
$ Image-> fontMark ($ content, $ font_url, $ size, $ color, $ local, $ angle );
$ Image-> show ();

Ii. How to Use Image thumbnails:

Require 'image. class. php'
$ Src = "001.jpg ";
$ Image = new Image ($ src );
$ Image-> thumb (300,200 );
$ Image-> show ();

Iii. image. class. php

Class image {
Private $ info;
Private $ image;
Public function _ contruct ($ src ){
$ Info = getimagesize ($ src );
$ This-> info = array (
'Width' => $ info [0],
'Height' => $ info [1],
'Type' => image_type_to_extension ($ info [2], false ),
'Mime '=> $ info ['mime'],

);
$ Fun = "imagecreatefrom {$ this-> info ['type']}";

$ This-> image = $ fun ($ src );

}

// Thumbnail
Public function thumd ($ width, $ height ){
$ Image_thumb = imagecreatetruecolor ($ width, $ height );
Imagecopyresampled ($ image_thumb, $ this-> image, 0, 0, 0, $ width, $ height, $ this-> info ['width'], $ this-> info ['height']);
Imagedestroy ($ this-> image );
$ This-> image = $ image_thumb;

}
// Text watermark
Public function fontMark ($ content, $ font_url, $ size, $ color, $ local, $ angle ){

$ Col = imagecolorallocatealpha ($ this-> image, $ color [0], $ color [1], $ color [2], $ color [3]);
$ Text = imagettftext ($ this-> image, $ size, $ angle, $ local ['X'], $ local ['y'], $ col, $ font_url, $ content );
}
// Output image
Public function show ()
{
Header ("Content-type:", $ this-> info ['mime ']);

$ Func = "image {$ this-> info ['type']}";
$ Func ($ this-> image );

}
Public function save ($ nwename ){
$ Func = "image {$ this-> info ['type']}";
// Retrieve image display from memory
$ Func ($ this-> image );
// Save the image
$ Func ($ this-> image, $ nwename. $ this-> info ['type']);

}
Public function _ destruct (){

Imagedestroy ($ this-> image );
}

}

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.