GD Library Notes

Source: Internet
Author: User
Tags crop image imagecopy imagejpeg transparent watermark

^. GD Introduction
PHP is not limited to producing only HTML output, but it can also create and manipulate image files in many different formats.
PHP provides some built-in image information functions, or you can use the GD library to create new images or work with existing images.
Currently the GD2 library supports formats such as GIF, JPEG, PNG, and wbmp. In addition, some freetype, Type1 and other font libraries are supported.
JPEG is the name of a compression standard, usually used to store photos or to store images with rich color and color levels. This format uses lossy compression.
PNG is a portable network image that uses a lossless compression standard for images.
The original meaning of GIF is "image Interchange Format", which is a lossless compression format based on LZW algorithm for continuous tone.
Before use, first check whether to open the GD library this module
Phpinfo ();
No GD library, go to php.ini to find
Extension=php_gd2.dll

^. Using GD Foundation
Canvas Management
Imagecreate () Creating a palette-based canvas
Imagecreatetruecolor () Create a canvas based on true Color
Imagedestroy () resources for destroying images

Set color
Imagecolorallocate () assigning colors to images

Generate an image to output the image to a browser or save the image as
Header ("Content-type:image/jpeg")
Imagegif () to output or save the image as a GIF format
Imagepng () to output or save the image as a PNG format
Imagejpeg () to output or save the image as a JPEG format
If the two parameter is a saved
If a parameter is the output

^.4 a step
1. Create a canvas
$img = Imagecreatetruecolor (300,300);

2. Drawing images
assigning colors
$blue = Imagecolorallocate ($img, 0,0,255);
$red = Imagecolorallocate ($img, 255,0,0);

3. Output image
Header (' content-type:image/jpg ');
Imagejpeg ($IMG); Do not save As
Imagejpeg ($img, './image/'. Time (). JPG '); Save As

4. Destruction of resources
Imagedestroy ($IMG);

^. Drawing images
Fill Color
Imagefill ($img, 0,0, $blue);

Draw Line
for ($i =0; $i <10; $i + +) {
Imageline ($img, Mt_rand (0, $width), Mt_rand (0, $height), Mt_rand (0, $width), Mt_rand (0, $height), $white);
}

Portrait point
for ($i =0; $i <1000; $i + +) {
Randomly plot pixel points
Imagesetpixel ($img, Mt_rand (0,400), Mt_rand (0,400), $white);
}

Rectangular
Imagerectangle ();
Imagefilledrectangle ($img,,, $white);

Polygon
$gon = Array (
$width/2, 0,
$width, $height/2,
$width/2, $height,
0, $height/2,
$width/2, 0);
Imagepolygon ($img, $gon, 5, $white);
Imagefilledpolygon ($img, $gon, 5, $white);

Draw a Circle
Imageellipse ($img,,, $red);
Imagefilledellipse ($img, 150,150, N, $red);

Draw an arc
Imagearc ($img, 150,150, 100,100, 0,360, $red);
Imagefilledarc ($img, 150,150, 100,100, 0,-30, $red, 0);

Draw text
Imagestring ()//a string of characters x-axis
Imagestringup ()//a string of characters y-axis
Imagechar ()//one-character x-axis
Imagecharup ()//one-character y-axis

Resource, font size, angle, coordinate x, coordinate y, color, font file, output string
Imagettftext ($img, 30,-45, 80,150, $white, './msyh. TTF ', ' Fandesavan de Sa ');

^.php image processing (zoom, crop, watermark, rotate and flip)
Basis
Imagecreatefrompng ($filename); Open Resource
Imagecreatefromjpeg ()
Imagecreatefromgif ()
GetImageSize ($filename); Get picture information, return array
Imagesx ($IMG); Wide chart
Imagesy ($IMG); Thurgau

Picture scaling and cropping
Imagecopyresampled ()//Copy part of the image and resize it
Imagecopyresized ()

Add a picture watermark
Imagecopy ()
Imagecopymerge ()

Picture rotation and rollover
Imagerotate ()

^. Crop image
source file
$filename = './image/1.jpg ';

The width and height of the new graph
$width = 300;
$height = 300;

New map
$imgdes = Imagecreatetruecolor ($width, $height);

Cropping chart
$IMGSRC = Imagecreatefromjpeg ($filename);

to crop
New figure, clipping object, new figure X, new figure y, trim figure x, crop graph y, new figure W, new figure h, crop graph W, crop graph H
Imagecopyresampled ($imgdes, $imgsrc, 0, 0, $width, $height, $width, $height);

Output
Header (' Content-type:image/jpeg ');
Imagejpeg ($imgdes);

Destroying resources
Imagedestroy ($imgdes);
Imagedestroy ($IMGSRC);

^. Watermark Text
source file
$filename = './image/1.jpg ';
$img = Imagecreatefromjpeg ($filename);

Font Color
$color = Imagecolorallocate ($img, 255,255,0);

Add text
Imagettftext ($img, 0, $color, './sthupo. TTF ', ' Dream Lover ');

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

Imagedestroy ($IMG);

^. Distorted picture Scaling
$filename = './image/1.jpg ';
$img = Imagecreatefromjpeg ($filename);
$width = 300;
$height = 300;
$BG = Imagecreatetruecolor ($width, $height);

Perform distortion scaling
Canvas, beauty chart, canvas x, canvas y, beauty figure x, beauty picture y, canvas w, canvas h, beauty picture W, beauty figure h
Imagecopyresampled ($BG, $img, 0,0,0,0, $width, $height, Imagesx ($img), Imagesy ($img));

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

Imagedestroy ($BG);
Imagedestroy ($IMG);

^. No transparent watermark picture
$filename = './image/1.jpg ';
$bgpic = Imagecreatefromjpeg ($filename);

Introduce watermark image
$water = Imagecreatefrompng ('./image/logo.png ');

To overwrite the watermark
Beautiful figure, watermark map, watermark x, watermark y, beautiful figure x, beauty figure y, Watermark W, Watermark graph H
Imagecopy ($bgpic, $water, 300,100,0,0,imagesx ($water), Imagesy ($water));

Header (' Content-type:image/jpeg ');
Imagejpeg ($bgpic);
Imagedestroy ($bgpic);
Imagedestroy ($water);

^. Transparent watermark
$filename = './image/1.jpg ';
$bgpic = Imagecreatefromjpeg ($filename);

Introduce watermark image
$water = Imagecreatefromjpeg ('./image/taiji.jpg ');

Transparent watermark Overlay
Beautiful figure, watermark map, watermark x, watermark y, beauty figure x, beauty figure y, Watermark W, Watermark H, transparency
Imagecopymerge ($bgpic, $water, 200,400,0,0,imagesx ($water), Imagesy ($water), 50);

Header (' Content-type:image/jpeg ');
Imagejpeg ($bgpic);
Imagedestroy ($bgpic);
Imagedestroy ($water);

^. Rotate picture
$filename = './image/1.jpg ';
$img = Imagecreatefromjpeg ($filename);

$color = Imagecolorallocate ($img, 255,255,0);

Canvas, angle, color
$img = Imagerotate ($img, -30, $color); Back to resources

Header (' Content-type:image/jpeg ');
Imagejpeg ($IMG);
Imagedestroy ($IMG);

^. Equal scale Scaling
$filename = './image/1.jpg ';
$img = Imagecreatefromjpeg ($filename);

$width = Imagesx ($img);
$height = Imagesy ($img);
$size = 300;

if ($width > $height) {
$BW = $size;
$BH = ($height/$width) * $BW;
}else{
$BH = $size;
$BW = ($width/$height) * $BH;
}

$BG = Imagecreatetruecolor ($BW, $BH);

Imagecopyresampled ($BG, $img, 0,0,0,0, $BW, $BH, $width, $height);

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

Imagedestroy ($BG);
Imagedestroy ($IMG);

GD Library Notes

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.