PHP uses the GD or GD2 library to use images. Only. For advanced graphic shapes, You can also download the jpgraph class library, which is completely written in PHP. Of course, it is also based on the GD/GD2 library.
1. Create a simple image
php /* before image output, HTML element output is not allowed */ header (" Content-Type: image/JPEG "); /// set the header information and display the page. Otherwise, the output image will be garbled. /* --------------- example 12.1: create a simple normal image ---------------- */ $ im = imagecreate (, 60); // Create a canvas $ white = imagecolorallocate (< SPAN style = "color: #800080;" >$ im , 159, 000000 ); imagegif ( $ im ); ?>
View code2. Add text (Text watermark) to the photo)
<? PHP /* ----------------- Add text to the photo in example 12.2. It can be a watermark ------------------- */ Header ("Content-Type: image/JPEG "); // Here we need to set the header information and tell the page how to render it. Otherwise, directly enter $ Im = Imagecreatefromjpeg ("images/123.jpg "); // Load Image $ Textcolor = Imagecolorallocate ( $ Im , 136 ); // Set the font color to blue and the RGB color value. $ Fnt = "C:/Windows/fonts/simhei. TTF "; // Define font /* I have not yet understood the principle of this issue. However, there is a way to judge: charset = UTF-8. If the HTML page is encoded with UTF-8, The iconv conversion is not required for the Chinese text. If it is the type of gb2312 encoding, it must be converted to UTF-8. In PHP, The GD2 Graphic Library only recognizes UTF-8 encoding for Chinese characters. */ // $ Mov = iconv ("gb2312", "UTF-8", "Changbai Mountain Tianchi"); // defines the output font string $ Motto = "Changbai Mountain Tianchi" ; Imagettftext ( $ Im , 50, 0, 20,150, $ Textcolor , $ Fnt , $ Motto ); // Write TTF text to the graph Imagejpeg ( $ Im ); // Create a JPEG Image Imagedestroy ( $ Im ); // Stops graphics and releases memory space ?>
View code3. Image Watermark
<?PHP /* ---------------- Image watermark ----------- */ Header ("Content-Type: image/JPEG "); // Here we need to set the header information and tell the page how to render it. Otherwise, directly enter $ Im = Imagecreatefromjpeg ("images/123.jpg "); // Load background image $ IML = Imagecreatefromjpeg ("images/321.jpg "); // Load background image $ Imarr =Getimagesize ("Images/321.jpg "); // Obtain the image size and type. For details, refer to the PHP help manual. Imagecopy ( $ Im , $ IML , 20,150, 0, 0, $ Imarr [0], $ Imarr [1]); // Copy an image to an image. For more information, see the PHP manual. Imagejpeg ( $ Im ); Imagedestroy ( $ Im ); // End the image and release the memory. ?>
View code