This article uses a large number of examples to explain the php Tutorial code for image operations. Next let's take a look at the example tutorials.
<? Php
$ Height = 300;
$ Width = 300;
// Create a background image
$ Im = imagecreatetruecolor ($ width, $ height );
// Assign color
$ White = imagecolorallocate ($ im, 255,255,255 );
$ Blue = imagecolorallocate ($ im, 0, 0, 64 );
// Draw the color to the image
Imagefill ($ im, 0, 0, $ blue );
// Draw the string: hello, php
Imagestring ($ im, 10,100,120, 'Hello, php', $ white );
// Output the image and define the header
Header ('content-type: image/png ');
// Send the image to the browser
Imagepng ($ im );
// Clear resources
Imagedestroy ($ im );
?>
Create a thumbnail for instance 2
<? Php
Header ("content-type: image/jpeg ");
// Load the image
$ Imagen1 = imagecreatefromjpeg ("imagen1.jpg ");
$ Imagen2 = imagecreatefromjpeg ("imagen2.jpg ");
// Copy the image
Imagecopy ($ imagen1, $ imagen2, 200,150, 0 );
// Output jpeg image
Imagejpeg ($ imagen1 );
// Release the memory
Imagedestroy ($ imagen2 );
Imagedestroy ($ imagen1 );
?>
Obtain image size information
<? Php
$ Info = getimagesize ("imagen2.jpg ");
Print_r ($ info );
?>
Create a png image
<? Php
// Png format image processing function
Function loadpng ($ imgname ){
$ Im = @ imagecreatefrompng ($ imgname );
If (! $ Im) {// image loading failed
$ Im = imagecreate (400, 30 );
$ Bgc = imagecolorallocate ($ im, 255,255,255 );
$ Tc = imagecolorallocate ($ im, 0, 0, 0 );
Imagefilledrectangle ($ im, 0, 0,150, 30, $ bgc );
Imagestring ($ im, 4, 5, 5, "error loading: $ imgname", $ tc );
}
Return $ im;
}
$ Imgpng = loadpng ("./karte.png ");
/* Output the image to the browser */
Header ("content-type: image/png ");
Imagepng ($ imgpng );
?>
Add text to images
<? Php
// Create a 100*30 image
$ Im = imagecreate (100, 30 );
// White background and blue text
$ Bg = imagecolorallocate ($ im, 200,200,200 );
$ Textcolor = imagecolorallocate ($ im, 0, 0,255 );
// Write the string at the top left
Imagestring ($ im, 5, 0, 0, "hello world! ", $ Textcolor );
// Output the image
Header ("content-type: image/jpeg ");
Imagejpeg ($ im );
Imagedestroy ($ im );
?>