PHP Image processing (zoom, trim, zoom, flip, rotate, transparent, sharpen)

Source: Internet
Author: User
Tags 0xc0 border color install php imagecopy imagejpeg transparent color rand


Note: If you want to use PHP GD processing we need to open the GD library


GD Library support for PHP under Windows

Find php.ini, open content, find:

; Extension=php_gd2.dll

Put the front semicolon ";" Remove, then save, if there is no semicolon, that is already open.


Linux opens GD Library


# #检测GD库是否安装命令
php5-m | Grep-i GD
Or
Php-i | Grep-i--color GD
# #如未安装GD库, the server is installed by the following method
### if is the source code installs, then adds the parameter
--with-gd
### if the Linux system in the Debian Department is installed with Apt-get, the following
Apt-get Install PHP5-GD
### if it is a centos system, install it with Yum, as follows
Yum Install PHP-GD
### if it's a SUSE Linux system, install it with YaST, as follows
Yast-i PHP5_GD


First, create picture resources

Imagecreatetruecolor (Width,height);

Imagecreatefromgif (picture name);

Imagecreatefrompng (picture name);

Imagecreatefromjpeg (picture name);

Draw a variety of images

Imagegif (picture resources, save path);

Imagepng ()

Imagejpeg ();


Second, get picture properties

Imagesx (res//width

Imagesy (res//height

getimagesize (file path)

Returns an array of four cells. Index 0 contains the pixel value of the image width, and index 1 contains the pixel value of the image height. Index 2 is the tag of the image type: 1 = gif,2 = jpg,3 = png,4 = swf,5 = psd,6 = bmp,7 = TIFF (Intel byte order), 8 = TIFF (Motorola byte order), 9 = jpc,10 = jp2,11 = jpx,12 = jb2,13 = swc,14 = iff,15 = wbmp,16 = XBM. These tags correspond to the newly added imagetype constants of PHP 4.3.0. Index 3 is a text string with the contents "height=" yyy "width=" xxx, which can be used directly for the IMG tag. Destroying image Resources

Imagedestroy (picture resources);


Third, transparent processing

PNG, JPEG transparent colors are normal, only gif is not normal

Imagecolortransparent (resource image [, int color])//Set a color to a transparent color

Imagecolorstotal ()

Imagecolorforindex ();

Four, the picture cutting

Imagecopyresized ()

Imagecopyresampled ();


Five, add watermark (text, picture)

String encoding conversion string Iconv (String $in _charset, String $out _charset, String $str)


Six, picture rotation

Imagerotate ()//Set angle of the picture flip


The flip of the picture

Flip along the x axis along the y axis

Eight, sharpening

Imagecolorsforindex ()

Imagecolorat ()

Draw a graphic on a picture
$img =imagecreatefromgif ("./images/map.gif");
$red = Imagecolorallocate ($img, 255, 0, 0);

Imageline ($img, 0, 0, $red);

Imageellipse ($img, MB, $red);

Imagegif ($img, "./images/map2.gif");

Imagedestroy ($img); general scaling of pictures

$filename = "./images/hee.jpg";

$per = 0.3;

List ($width, $height) =getimagesize ($filename);

$n _w= $width * $per;
$n _h= $width * $per;

$new =imagecreatetruecolor ($n _w, $n _h);

$img =imagecreatefromjpeg ($filename);
Copy part of the image and adjust

Imagecopyresized ($new, $img, 0, 0,0, 0, $n _w, $n _h, $width, $height);
Image output new picture, Save As

Imagejpeg ($new, "./images/hee2.jpg");

Imagedestroy ($new);
Imagedestroy ($img); picture scaling, no transparent color processing

function Thumn ($background, $width, $height, $newfile) {
List ($s _w, $s _h) =getimagesize ($background);//Get the original picture height, width

if ($width && ($s _w < $s _h)) {
$width = ($height/$s _h) * $s _w;
} else {
$height = ($width/$s _w) * $s _h;
}

$new =imagecreatetruecolor ($width, $height);

$img =imagecreatefromjpeg ($background);

Imagecopyresampled ($new, $img, 0, 0, 0, 0, $width, $height, $s _w, $s _h);

Imagejpeg ($new, $newfile);

Imagedestroy ($new);
Imagedestroy ($IMG);
}

Thumn ("Images/hee.jpg", MB, "./images/hee3.jpg"); GIF transparent color processing

function Thumn ($background, $width, $height, $newfile) {
List ($s _w, $s _h) =getimagesize ($background);

if ($width && ($s _w < $s _h)) {
$width = ($height/$s _h) * $s _w;
} else {
$height = ($width/$s _w) * $s _h;
}

$new =imagecreatetruecolor ($width, $height);

$img =imagecreatefromgif ($background);

$OTSC =imagecolortransparent ($IMG);
if ($OTSC >=0 && $otst < Imagecolorstotal ($img)) {//Judging index color
$tran =imagecolorsforindex ($img, $OTSC);//Index color value

$newt =imagecolorallocate ($new, $tran ["Red"], $tran ["Green"], $tran ["Blue"]);

Imagefill ($new, 0, 0, $NEWT);

Imagecolortransparent ($new, $NEWT);
}

Imagecopyresized ($new, $img, 0, 0, 0, 0, $width, $height, $s _w, $s _h);

Imagegif ($new, $newfile);

Imagedestroy ($new);
Imagedestroy ($IMG);
}

Thumn ("Images/map.gif", MB, "./images/map3.gif"); picture cropping

function Cut ($background, $cut _x, $cut _y, $cut _width, $cut _height, $location) {

$back =imagecreatefromjpeg ($background);

$new =imagecreatetruecolor ($cut _width, $cut _height);

Imagecopyresampled ($new, $back, 0, 0, $cut _x, $cut _y, $cut _width, $cut _height, $cut _width, $cut _height);

Imagejpeg ($new, $location);

Imagedestroy ($new);
Imagedestroy ($back);
}

Cut ("./images/hee.jpg", 440, 140, 117, 112, "./images/hee5.jpg"); Picture plus watermark

Text watermark

function Mark_text ($background, $text, $x, $y) {
$back =imagecreatefromjpeg ($background);

$color =imagecolorallocate ($back, 0, 255, 0);

Imagettftext ($back, 0, $x, $y, $color, "Simkai.ttf", $text);

Imagejpeg ($back, "./images/hee7.jpg");

Imagedestroy ($back);
}

Mark_text ("./images/hee.jpg", "PHP in detail", 150, 250);

Picture watermark
function Mark_pic ($background, $waterpic, $x, $y) {
$back =imagecreatefromjpeg ($background);
$water =imagecreatefromgif ($waterpic);
$w _w=imagesx ($water);
$w _h=imagesy ($water);
Imagecopy ($back, $water, $x, $y, 0, 0, $w _w, $w _h);
Imagejpeg ($back, "./images/hee8.jpg");
Imagedestroy ($back);
Imagedestroy ($water);
}
Mark_pic ("./images/hee.jpg", "./images/gaolf.gif", 50, 200); Picture rotation

$back =imagecreatefromjpeg ("./images/hee.jpg");

$new =imagerotate ($back, 45, 0);

Imagejpeg ($new, "./images/hee9.jpg"); Picture horizontal Flip Vertical Flip

function turn_y ($background, $newfile) {
$back =imagecreatefromjpeg ($background);

$width =imagesx ($back);
$height =imagesy ($back);

$new =imagecreatetruecolor ($width, $height);

for ($x =0; $x < $width; $x + +) {
Imagecopy ($new, $back, $width-$x-1, 0, $x, 0, 1, $height);
}

Imagejpeg ($new, $newfile);

Imagedestroy ($back);
Imagedestroy ($new);
}

function turn_x ($background, $newfile) {
$back =imagecreatefromjpeg ($background);

$width =imagesx ($back);
$height =imagesy ($back);

$new =imagecreatetruecolor ($width, $height);

for ($y =0; $y < $height; $y + +) {
Imagecopy ($new, $back, 0, $height-$y-1, 0, $y, $width, 1);
}

Imagejpeg ($new, $newfile);

Imagedestroy ($back);
Imagedestroy ($new);
}

Turn_y ("./images/hee.jpg", "./images/hee11.jpg");
Turn_x ("./images/hee.jpg", "./images/hee12.jpg"); Picture Sharpening

Function Sharp ($background, $degree, $save) {
$back =imagecreatefromjpeg ($background);

$b _x=imagesx ($back);
$b _y=imagesy ($back);

$DST =imagecreatefromjpeg ($background);
for ($i =0; $i < $b _x; $i + +) {
for ($j =0; $j < $b _y; $j + +) {
$b _clr1=imagecolorsforindex ($back, Imagecolorat ($back, $i-1, $j-1)); \ previous pixel color array
$b _clr2=imagecolorsforindex ($back, Imagecolorat ($back, $i, $j)); \ Remove the current array of colors

$r =intval ($b _clr2["Red"]+ $degree * ($b _clr2["Red"]-$b _clr1["Red"));
$g =intval ($b _clr2["green"]+ $degree * ($b _clr2["green"]-$b _clr1["green"));
$b =intval ($b _clr2["Blue"]+ $degree * ($b _clr2["Blue"]-$b _clr1["Blue"));

$r =min (255, Max ($r, 0))//limit R range between 0-255
$g =min (255, Max ($g, 0));
$b =min (255, Max ($b, 0));

if ($d _clr=imagecolorexact ($DST, $r, $g, $b)) {//equals 1 not in color range
$d _clr=imagecolorallocate ($DST, $r, $g, $b);//Create a color
}

Imagesetpixel ($DST, $i, $j, $d _clr);
}

}
Imagejpeg ($DST, $save);
Imagedestroy ($back);
Imagedestroy ($DST);
}

Sharp ("./images/hee.jpg", "./images/hee13.jpg"); October 26th, 2011No comments yet sincere to PHP design implementation and application of authentication code class
validationcode.class.php

/* Develop a validation code class
*
* 1. What is a verification code
*
* 2. The role of the Verification code
*
* 3. Write Verification Code Class (PHP image processing)
*
* 4. Using verification Code
*
*
*/<?php
Class Validationcode {
Private $width;
Private $height;
Private $codeNum;
Private $image; Image Resources
Private $disturbColorNum;
Private $checkCode;

function __construct ($width =80, $height =20, $codeNum =4) {
$this->width= $width;
$this->height= $height;
$this->codenum= $codeNum;
$this->checkcode= $this->createcheckcode ();
$number =floor ($width * $height/15);

if ($number > 240-$codeNum) {
$this->disturbcolornum= 240-$codeNum;
}else{
$this->disturbcolornum= $number;
}

}
To output an image to a browser by accessing this method
function ShowImage ($fontFace = "") {
First step: Create an image background
$this->createimage ();
Step two: Set the jamming elements
$this->setdisturbcolor ();
Step three: Draw the text randomly to the image
$this->outputtext ($fontFace);
Fourth Step: Output image
$this->outputimage ();
}

Obtain a randomly created authentication code string by calling this method
function Getcheckcode () {
return $this->checkcode;
}

  private function CreateImage () {
   //Create an image resource
    $this-> Image=imagecreatetruecolor ($this->width, $this->height);
   //random background color
    $backColor =imagecolorallocate ($this->image, rand (225, 255) , Rand (225,255), rand (225, 255));
   //adds color to background
   imagefill ($this->image, 0, 0, $backColor);
    //set Border color
    $border =imagecolorallocate ($this->image, 0, 0, 0);
    //draws a rectangular border
   imagerectangle ($this->image, 0, 0, $this->width-1, $this->height-1, $ border);
  }

Private Function Setdisturbcolor () {
For ($i =0 $i < $this->disturbcolornum; $i + +) {
$color =imagecolorallocate ($this->image, rand (0, 255), rand (0, 255), rand (0, 255));
Imagesetpixel ($this->image, rand (1, $this->width-2), rand (1, $this->height-2), $color);
}

For ($i =0 $i <10; $i + +) {
$color =imagecolorallocate ($this->image, rand (255), Rand (255), rand (200, 255));
Imagearc ($this->image, rand ( -10, $this->width), rand ( -10, $this->height), rand (+), rand (20, 200), 55, 44, $color);
}
}

Private Function Createcheckcode () {
$code = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ";
$string = ';
For ($i =0 $i < $this->codenum; $i + +) {
$char = $code {rand (0, strlen ($code)-1)};
$string. = $char;
}

return $string;
}

Private Function outputtext ($fontFace = "") {
For ($i =0 $i < $this->codenum; $i + +) {
$fontcolor =imagecolorallocate ($this->image, rand (0, 128), rand (0, 128), rand (0, 128));
if ($fontFace = = "") {
$fontsize =rand (3, 5);
$x =floor ($this->width/$this->codenum) * $i +3;
$y =rand (0, $this->height-15);
Imagechar ($this->image, $fontsize, $x, $y, $this->checkcode{$i}, $fontcolor);
}else{
$fontsize =rand (12, 16);
$x =floor (($this->width-8)/$this->codenum) * $i +8;
$y =rand ($fontSize +5, $this->height);
Imagettftext ($this->image, $fontsize, Rand ( -30), $x, $y, $fontcolor, $fontFace, $this->checkcode{$i});
}
}
}

Private Function Outputimage () {
if (Imagetypes () & Img_gif) {
Header ("Content-type:image/gif");
Imagepng ($this->image);
}else if (Imagetypes () & img_jpg) {
Header ("Content-type:image/jpeg");
Imagepng ($this->image);
}else if (Imagetypes () & Img_png) {
Header ("Content-type:image/png");
Imagepng ($this->image);
}else if (Imagetypes () & Img_wbmp) {
Header ("Content-type:image/vnd.wap.wbmp");
Imagepng ($this->image);
}else{
Die ("PHP does not support image creation");
}
}

function __destruct () {
Imagedestroy ($this->image);
}
}code.php

<?php
Session_Start ();
Include "validationcode.class.php";

$code =new Validationcode (80, 20, 4);

$code->showimage (); Output to a page for registration or logon use

$_session["code"]= $code->getcheckcode (); Save the Authenticode to the server demo.php

<?php
Session_Start ();
echo $_post["code"]. " <br> ";
echo $_session["code"]. " <br> ";

if (Strtoupper ($_post["code"]) ==strtoupper ($_session["code")) {
echo "OK";
}else{
echo "Error";
}
?>
<body>
<form action= "login.php" method= "POST" >
User:<input type= "text" name= "Usenrame" ><br>
Pass:<input type= "PASSOWRD" name= "Pass" ><br>

Code: <input type= "text" name= "code" onkeyup= "if (This.value!=this.value.touppercase ()) this.value= This.value.toUpperCase () "> <br>

<input type= "Submit" Name= "sub" value= "Login" ><br>
</form>
</body>


1. Drawing

Verification Code, Statistics chart

Install the GD library (Picture processing library)

Create a canvas--Create a resource type--– height, width
Drawing images
1, the development of a variety of colors
2, Rectangle, garden, point, line segment, fan, reprint (character, string, FreeType font), each image corresponds to a function

Output image/Save processed image
1, output various types (GIF, PNG, JPEG)

Imagegif ();
Imagepng ();
Imagegpeg ();

Releasing resources
<?php
Step 1 Create a picture resource
$img =imagecreatetruecolor (200,200);
$red =imagecolorallocate ($img, 255,0,0);
$yellow =imagecolorallocate ($img, 255,255,0);
$green =imagecolorallocate ($img, 0,255,0);
$blue =imagecolorallocate ($img, 0,0,255);
Imagefill ($img, 0,0, $yellow);//Color fill
Step2 draw all kinds of graphics
Imagefilledrectangle ($img, 10,10,80,80, $red);//Draw Rectangle and fill
Imagerectangle ($img, 10,10,80,80, $red))//Draw rectangle not filled, color fill

Segment
Imageline ($img, 0, 0, $blue);
Imageline ($img, 0, 0, $blue);

Point
Imagesetpixel ($img, M, $red);
Imagesetpixel ($img, $red);
Imagesetpixel ($img, $red);
Imagesetpixel ($img, EUR, $red);
Imagesetpixel ($img, $red);

Round
Imageellipse ($img, MB, $green);
Round
Imagefilledellipse ($img, $blue);
Border
Output an image or save an image
Header ("Content-type:img/gif");
Imagegif ($IMG);
Releasing resources
Imagedestory ($IMG);

Gao shape diagram

Step 1 Create a picture resource
$img =imagecreatetruecolor (200, 200);

$img =imagecreate (200, 200);

$white =imagecolorallocate ($img, 255, 255, 255);
$gray =imagecolorallocate ($img, 0xc0, 0xc0, 0xc0);
$darkgray =imagecolorallocate ($img, 0x90, 0x90, 0x90);
$navy =imagecolorallocate ($img, 0, 0, 0x80);
$darknavy =imagecolorallocate ($img, 0, 0, 0x50);
$red = Imagecolorallocate ($img, 0xFF, 0, 0);
$darkred =imagecolorallocate ($img, 0x90, 0, 0);

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

3D effect
For ($i =60 $i >50; $i-) {
Imagefilledarc ($img, $i, M, -160, $darkgray, Img_arc_pie);
Imagefilledarc ($img, $i, M, N, $darknavy, Img_arc_pie);
Imagefilledarc ($img, $i, M, N, $darkred, Img_arc_pie);
}
Imagefilledarc ($img, $i, M, -160, $gray, Img_arc_pie);
Imagefilledarc ($img, $i, M, N, $navy, Img_arc_pie);
Imagefilledarc ($img, $i, M, N, $red, Img_arc_pie);

Header ("Content-type:image/gif");
Imagegif ($IMG);

Releasing resources
Imagedestroy ($IMG);

<?php

Step 1 Create a picture resource
$img =imagecreatetruecolor (200, 200);

$img =imagecreate (200, 200);

$white =imagecolorallocate ($img, 255, 255, 255);
$gray =imagecolorallocate ($img, 0xc0, 0xc0, 0xc0);
$darkgray =imagecolorallocate ($img, 0x90, 0x90, 0x90);
$navy =imagecolorallocate ($img, 0, 0, 0x80);
$darknavy =imagecolorallocate ($img, 0, 0, 0x50);
$red = Imagecolorallocate ($img, 0xFF, 0, 0);
$darkred =imagecolorallocate ($img, 0x90, 0, 0);

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

Imagechar ($img, 5, MB, "A", $red);
Imagechar ($img, 5, A., "B", $red);
Imagecharup ($img, 5,,, "C", $red);
Imagecharup ($img, 5,,, "D", $red);

Imagestring ($img, 3, ten, "Hello", $navy);
Imagestringup ($img, 3, ten, "Hello", $navy);

Imagettftext ($img, the $red, "Simkai.ttf", "# #");
Imagettftext ($img, -60,, $red, "Simli.ttf", "# #");

Header ("Content-type:image/gif");
Imagegif ($IMG);

Releasing resources
Imagedestroy ($img); 2, processing the original image

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.