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 );
}
}