Common GD library functions

Source: Internet
Author: User
Tags imagecopy imagejpeg

01. php

<? PHP


/*

Drawing with Windows graphic Board

1: Create a canvas and set the size.



2: Create a pigment. (red, r Green G blue B, composed of three primary colors. The primary colors can be 0-from weak to strong ).



3: draw lines, write, draw graphics, fill and so on.



4: Save As Image



5. Destroy the canvas.



Using the GD library to draw images is still the above five steps.

*/


// 1: Create a canvas and return it as a resource

$ Im = imagecreatetruecolor (300,200 );



// 2: Create paint

$ Gray = imagecolorallocate ($ im, 100,100,100 );


// 3: Fill the canvas

Imagefill ($ im, 0, 0, $ gray );



// 4: Save As Image

Imagepng ($ im, './01.png ');


// 5: destroy the canvas

Imagedestroy ($ IM );



Echo 'OK ';



-------------------

02. php

<? PHP

// The GD library is disabled by default. You need to enable related options.

// How do I check whether the GD library is enabled?

Print_r (gd_info ());

// You can see an array similar to the following to check whether the GD library is enabled.

Array

(

[Gd version] => bundled (2.0.34 compatible) // GD library version

[FreeType support] => 1

[FreeType linkage] => with FreeType

[T1lib support] =>

[GIF read support] => 1

[GIF create support] => 1

[JPEG support] => 1

[PNG support] => 1

[Wbmp support] => 1

[XPM support] =>

[Xbm support] => 1

[JIS-mapped Japanese font support] =>

)



-------------------

03. php

<? PHP

// Step 1: Create a canvas


// 1: Create a blank canvas and specify the width and height.

// 300*200 blank canvas. The returned value is a resource type.

// Create a canvas. The default background color is pure black.

/*

$ Im = imagecreatetruecolor (300,200 );

Imagepng ($ im, './02.png ');

*/


$ Im = imagecreatefromjpeg ('./he.jpg ');

$ Blue = imagecolorallocate ($ im, 255 );

Imageline ($ im, 0, 0, 800,600, $ blue );

If (imagejpeg ($ im, './03.jpg ')){

Echo 'OK ';

}


?>

-------------------

04. php

<? PHP

$ Im1 = imagecreatetruecolor (1, 300,200 );

$ IM2 = imagecreatetruecolor (600,400 );


$ Blue = imagecolorallocate ($ im1, 255 );

$ Red = imagecolorallocate ($ IM2, 255, 0, 0 );

?>

-------------------

05. php

<? PHP

$ Im = imagecreatetruecolor (600,400 );

$ Red = imagecolorallocate ($ im, 255, 0, 0 );

Imagefill ($ im, 0, 0, $ red );

Header ('content-type: image/JPEG ');

Imagejpeg ($ IM );


Imagedestroy ($ IM );

?>

-------------------

06. php

<? PHP

// Step 5: destroy the canvas


$ Im = imagecreatetruecolor (300,200 );


Imagedestroy ($ IM );


?>

-------------------

07. php

<? PHP

/*

Imageline: Draw line

Imagestring, imagettftext: Write

Imagefill-Region Filling

Imagefilledarc-draw an elliptical arc and fill it

Imagefilledellipse-draw an ellipse and fill it

Imagefilledpolygon-draw a polygon and fill it

Imagefilledrectangle-draw a rectangle and fill it


Imagerectangle: Draw a rectangle


Imagecopy: copy an image

Imagecopymerge: Copy and merge images (transparency can be set .)


Imagecopyresampled: Merge and resize

*/


?>


-------------------

08. php

<? PHP

// Draw Line Learning

/*

Bool imageline (resource $ image, int $ X1, int $ Y1, int $ X2, int $ Y2, int $ color)

*/


// 1: Create a canvas

$ Im = imagecreatefromjpeg ('./home.jpg ');



// 2: Pigment

$ Red = imagecolorallocate ($ im, 255, 0, 0 );

$ Blue = imagecolorallocate ($ im, 255 );


// 3: draw a line

Imageline ($ im, 0, 0, 670,503, $ red );

Imageline ($ im, 0,503,670, 0, $ blue );


// 4: Output

Header ('content-type: image/JPEG ');

Imagejpeg ($ IM );


// 5: Destroy

Imagedestroy ($ IM );

?>


-------------------

09. php

<? PHP


/*

Writing

Bool imagestring (resource $ image, int $ font, int $ X, int $ y, string $ S, int $ col)


Resource $ image: canvas Resource

Int $ Font: size (1-5). The font is default and cannot be changed.


Int $ X, int $ Y: represents the coordinates of the point in the upper left corner of the first character to be written.


String $ S: content to be written


Int $ color: Color


Q: Can I wrap a line?

A: No.


Q: Can I write Chinese?

A: No.


Q: Can I specify a font?

A: No.

*/




$ Im = imagecreatefromjpeg ('./home.jpg ');


$ Blue = imagecolorallocate ($ im, 255 );


Imagestring ($ im, 5,100,100, 'Welcome to shahe ', $ blue );


Header ('content-type: image/JPEG ');

Imagejpeg ($ IM );


-------------------

10. php

<? PHP


Function randname ($ n = 6 ){

If ($ n <= 0 ){

Return '';

}

$ STR = 'abcdefghijkmnpqrstuvwxyzabcdefghijkmnpqrstuvwxyz0123456789 ';

$ STR = substr (str_shuffle ($ Str), 0, $ N );


Return $ STR;

}


// Perform the verification code


$ Im = imagecreatetruecolor (80, 30 );


$ Gray = imagecolorallocate ($ im, 30, 30 );

$ Red = imagecolorallocate ($ im, 255, 0, 0 );


Imagefill ($ im, 0, 0, $ gray );


Imagestring ($ im, 5, 5, randname (4), $ red );


// Output

Header ('content-type: image/JPEG ');

Imagejpeg ($ IM );



-------------------

11. php

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 strict // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml" lang = "ZH-CN">

<Head>

<Title> create a webpage </title>

<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>

<Meta name = "Description" content = ""/>

<Meta name = "keywords" content = ""/>

<SCRIPT type = "text/JavaScript">

Function CHN (){

VaR img0 = Document. getelementsbytagname ('img ') [0];

Img0.src = '10. php? Id = '+ math. Random ();

}

</SCRIPT>


<Style type = "text/CSS">

</Style>

</Head>

<Body>

<Form>

Verification Code: <input type = "text" name = "code"/>

</Form>

</Body>

</Html>

-------------------

12. php

<? PHP

/*

Write Chinese

Imagettftext

You need to provide a TTF Library first.

Array imagettftext (resource $ image, float $ size, float $ angle, int $ X, int $ y, int $ color, string $ fontfile, string $ text)

*/

$ Im = imagecreatefromjpeg ('./home.jpg ');


$ Blue = imagecolorallocate ($ im, 255 );


Imagettftext ($ im, 100,100, $ blue, './msyh. ttf', 'changping Water', away from town noisy ');


Header ('content-type: image/JPEG ');

Imagejpeg ($ IM );

?>



-------------------

13. php

<? PHP

/*

// Draw an ellipse

Create an ellipse on a canvas and 4 data records are required.

Bool imageellipse (resource $ image, int $ CX, int $ cy, int $ W, int $ H, int $ color)

Center: Cx, Cy

Width: W

Height: H



// Draw a rectangle

Bool imagerectangle (resource $ image, int $ X1, int $ Y1, int $ X2, int $ Y2, int $ col)



*/



$ Im = imagecreatetruecolor (400,800 );


$ Gray = imagecolorallocate ($ im, 100,100,100 );

$ Blue = imagecolorallocate ($ im, 255 );



Imagefill ($ im, 0, 0, $ gray );


// Draw an ellipse

Imageellipse ($ im, 200,100, $ blue );


// Draw a rectangle

Imagerectangle ($ im, 100,100,300,200, $ blue );


// Draw a circle

Imageellipse ($ im, 200,300,200,200, $ blue );


// Output

Header ('content-type: image/JPEG ');

Imagejpeg ($ IM );


-------------------

14. php


<? PHP


// Copy an image


/*

Case study: effect of making a bottom Image

[X] is a small image.

[X]


Use a thumbnail to create a large image

{[X] [x]}

*/


/*

Obtain the image width.

Imagesx ()


Obtain the Image Height

Imagesy ()


*/



// Read the image as a canvas Resource

$ Small = imagecreatefrompng ('./feng.png ');


// Determine the size

$ SX = imagesx ($ small); // width of the thumbnail

$ Sy = imagesy ($ small); // width of the thumbnail



// Create a large image canvas

$ Big = imagecreatetruecolor (2 * $ SX + 20, $ Sy );

$ Gray = imagecolorallocate ($ big, 100,100,100); // gray



// Expand the image

Imagefill ($ big, 0, 0, $ gray );



// Copy the thumbnail to the larger image.

/*

Bool imagecopy (resource $ dst_im, resource $ src_im, int $ dst_x, int $ dst_y, int $ src_x, int $ src_y, int $ src_w, int $ src_h)

*/


Imagecopy ($ big, $ small, 0, 0, 0, $ Sx, $ Sy );

Imagecopy ($ big, $ small, $ SX + 20, 0, 0, $ Sx, $ Sy );




// Output

Header ('content-type: image/JPEG ');

Imagejpeg ($ big );



?>


-------------------

15. php

<? PHP

// Fill


$ Im = imagecreatetruecolor (400,400 );


$ Gray = imagecolorallocate ($ im, 100,100,100 );

$ Blue = imagecolorallocate ($ im, 255 );

$ Red = imagecolorallocate ($ im, 255, 0, 0 );

$ Orange = imagecolorallocate ($ im, 248,224,143 );


Imagefill ($ im, 100,100, $ gray );


// Draw an ellipse

Imageellipse ($ im, 200,200,200,200, $ blue );


// Fill in the ellipse

Imagefill ($ im, 200,200, $ red );



// Draw a rectangle and fill it directly.

Imagefilledrectangle ($ im, 0,100,200,200, $ blue );



// Draw an ellipse and fill it directly


Imagefilledellipse ($ im, 200,100, $ orange );



// Output

Header ('content-type: image/JPEG ');

Imagejpeg ($ IM );

?>



-------------------

16. php

<? PHP


// Draw and fill an arc

/*

Bool imagefilledarc (resource $ image, int $ CX, int $ cy, int $ W, int $ H, int $ S, int $ E, int $ color, int $ style)


Style description


Pie: pie

Chord: String


Nofill: do not fill

Edged: edge


0 img_arc_pie

1 img_arc_chord

2 img_arc_nofill

4 img_arc_edged



0000 (pie/arc)

0100 (fill)

-----

0100 = 4 --> slice + fill



0001 (string)

0010 (not filled)

--------

0011 = 3 (straight line)


0001 (string)

0100 (fill) ---> triangle + fill Effect

-----

0101 = 5



0000 (ARC)

0010 (not filled)

-----

0010-> 2-> an arc, but not filled.



*/



$ Im = imagecreatetruecolor (400,400 );


$ Gray = imagecolorallocate ($ im, 100,100,100 );

$ Blue = imagecolorallocate ($ im, 255 );


Imagefill ($ im, 0, 0, $ gray );

Imagefilledarc ($ im, 200,200,200,200, 45,135, $ blue, 2 );



Header ('content-type: image/JPEG ');

Imagejpeg ($ IM );



?>

-------------------

17. php

<? PHP


// Copy an image


/*

Case study: effect of making a bottom Image

[X] is a small image.

[X]


Use a thumbnail to create a large image

{[X] [x]}

*/


/*

Obtain the image width.

Imagesx ()


Obtain the Image Height

Imagesy ()


*/



// Read the image as a canvas Resource

$ Small = imagecreatefrompng ('./feng.png ');


// Determine the size

$ SX = imagesx ($ small); // width of the thumbnail

$ Sy = imagesy ($ small); // width of the thumbnail



// Create a large image canvas

$ Big = imagecreatetruecolor (2 * $ SX + 20, $ Sy );

$ Gray = imagecolorallocate ($ big, 100,100,100); // gray



// Expand the image

Imagefill ($ big, 0, 0, $ gray );



// Copy the thumbnail to the larger image.

/*

Bool imagecopy (resource $ dst_im, resource $ src_im, int $ dst_x, int $ dst_y, int $ src_x, int $ src_y, int $ src_w, int $ src_h)

*/


Imagecopy ($ big, $ small, 0, 0, 0, $ Sx, $ Sy );

Imagecopy ($ big, $ small, $ SX + 20, 0, 0, $ Sx, $ Sy );




// Output

Header ('content-type: image/JPEG ');

Imagejpeg ($ big );



?>

-------------------

18. php

<? PHP


// Watermark effect.

/*

Function parameters are the same as those of imagecopy,

Difference in the last parameter: $ PCT indicates transparency.


Bool imagecopymerge (resource $ dst_im, resource $ src_im, int $ dst_x, int $ dst_y, int $ src_x, int $ src_y, int $ src_w, int $ src_h, int $ PCT)


*/



$ Big = imagecreatefromjpeg ('./home.jpg ');

$ BX = imagesx ($ big );

$ By = imagesy ($ big );



$ Small = imagecreatefrompng ('./feng.png ');

$ SX = imagesx ($ small );

$ Sy = imagesy ($ small );



// Add text

$ Blue = imagecolorallocate ($ big, 255 );

Imagettftext ($ big, 16, 0, 100,100, $ blue, './msyh. ttf', 'changping water-gun, Fengjie calls you to buy a house ');



Imagecopymerge ($ big, $ small, $ Bx-$ Sx, 0, 0, $ Sx, $ Sy, 37 );


// Output

Header ('content-type: image/JPEG ');

Imagejpeg ($ big );




?>

-------------------

19. php

<? PHP


// Generate a thumbnail

/*

Bool imagecopyresampled (resource $ dst_image, resource $ src_image, int $ dst_x, int $ dst_y, int $ src_x, int $ src_y, int $ dst_w, int $ dst_h, int $ src_w, int $ src_h)

*/


$ Feng = imagecreatefrompng ('./feng.png ');


$ FX = imagesx ($ Feng); // obtain the width.

$ FY = imagesy ($ Feng); // obtain the height



// Create a small canvas

$ SX = $ FX/2;

$ Sy = $ Fy/2;

$ Small = imagecreatetruecolor ($ Sx, $ Sy );



// Stick the large canvas and scale it down.

Imagecopyresampled ($ small, $ Feng, 0, 0, 0, $ Sx, $ Sy, $ FX, $ FY );


If (imagepng ($ small, './xiaofeng.png ')){

Echo 'saved successfully ';

} Else {

Echo 'failed to save ';

}

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.