PHP Graphics processing tutorial-php generating pictures

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags apache application code content create example find function

In the Web application is often used to generate pictures of this function, in the PHP tutorial to create pictures need to support the GD library to create graphics, with this graphic function, we can easily generate thumbnails, verification code, to add watermarks and so on.

In PHP to install the GD library to normal operation to create picture features, the following methods, in the win system, find php.ini put

; Extension=php_gd2.dll front ";" Go, and back up Apache OK.

Here's a look at the example

PHP's GD library can generate a variety of image files, such as gif,png,jpg,wbmp,xpm, and so on, below to see a generation of square files.

<?php
$height = 300;
$width = 300;
Create a background image
$im = Imagecreatetruecolor ($width, $height);
assigning colors
$white = Imagecolorallocate ($im, 255, 255, 255);
$blue = imagecolorallocate ($im, 0, 0, 64);
Draw Color to Image
Imagefill ($im, 0, 0, $blue);
Draw string: hello,php
Imagestring ($im, hello,php, $white);
Output image, definition header
Header (' content-type:image/png ');
Send image to Browser
Imagepng ($im);
Clear Resources
Imagedestroy ($im);
?>
View the results as long as browsing the PHP file can be, if you want to image call <img src=a.php/>

Instance two, using basic functions to create a picture imagecreate ()

Resource imagescreate (int x,inty) Imagedestroy is the memory space that the picture occupies

int Ingaedestroy (image) Imagecopy ()

int imagecopy (dst_im,sr_im,int x,int y,int x,int y,)

Here's a look at the example

<?php
Header ("Content-type:image/jpeg");
Loading images
$imagen 1 = imagecreatefromjpeg ("imagen1.jpg");
$imagen 2 = imagecreatefromjpeg ("imagen2.jpg");

Copying images
Imagecopy ($imagen 1, $imagen 2,0,0,0,0,200,150);

Output JPEG image
Imagejpeg ($imagen 1);

Free memory
Imagedestroy ($imagen 2);
Imagedestroy ($imagen 1);

?>
Example three picture text on a picture

We'll use imagecreatefromjpeg in this function to recreate a picture from a file or form.

Resource Imagecrratefromjpge (string,imagename);

Imagecratefrompng ();

Resource Imagecrratefromjpge (string,imagename);
See examples

&lt;?php


//png format image processing function


function Loadpng ($imgname) {


$im = @ImageCreateFromPNG ($imgname);


if (! $im) {//Load image failed


$im = imagecreate (400, 30);


$BGC = imagecolorallocate ($im, 255, 255, 255);


$TC = imagecolorallocate ($im, 0, 0, 0);


Imagefilledrectangle ($im, 0, 0, $BGC);


imagestring ($im, 4, 5, 5, "Error Loading: $imgname", $TC);


    }


return $im;


 }


$imgPng =loadpng ("./karte.png");


/* Output image to Browser/


header ("Content-type:image/png");


imagepng ($imgPng);


?&gt;


 


&lt;?php


$img = imagecreate (100, 100);


$black = imagecolorallocate ($img, 0, 0, 0);


$white = imagecolorallocate ($img, 255, 255, 255);


$orange = imagecolorallocate ($img, 255, 128, 64);


$lightorange = imagecolorallocate ($img, 255, 220, 164);


Imagefilledrectangle ($img, 0, 0, $white);


Imagerectangle ($img, 0, 0, $black);


Imagerectangle ($img, 5, 5, $black);


$points = Array (12,10, 20,15,15,20);


$nump = (int) count ($points)/2;


Imagepolygon ($img, $points, $nump, $orange);


imageline ($img, $orange);


imageline ($img, $lightorange);


imageline ($img, $orange);


Imagerectangle ($img, $orange);


Imagefill ($img, $lightorange);


Imagefill ($img, $lightorange);


imagestring ($img, 2,, "I ' m a PHP", $black);


imagestring ($img, 2,, "image", $black);


imagepng ($IMG);


?&gt;

Write text on the image Imagechar Imageloadfont imagefontheight, Imagefontwidth function

<?php
$imagen = Imagecreate (350,76);
$BG = Imagecolorallocate ($imagen, 255,255,255);
$negro = Imagecolorallocate ($imagen, 0,0,0);
/*
Get built-in font width and height
*/
$w 1 = imagefontwidth (1); Font 1 font width
$h 1 = imagefontheight (1); Font 1 font height

$w 2 = imagefontwidth (2); Font 2 font width
$h 2 = imagefontheight (2); Font 2 font height

$w 3 = imagefontwidth (3); Font 3 font width
$h 3 = imagefontheight (3); Font 3 font height

$w 4 = imagefontwidth (4); Font 4 font width
$h 4 = imagefontheight (4); Font 4 Font height

$w 5 = imagefontwidth (5); Font 5 font width

Related Article

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.