First, create picture resources
Imagecreatetruecolor (Width,height);
Imagecreatefromgif (picture name);
Imagecreatefrompng (picture name);
Imagecreatefromjpeg (picture name), draw a variety of image 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 graphics on the picture $img =imagecreatefromgif ("./images/map.gif");
Copy Code code as follows:
$red = Imagecolorallocate ($img, 255, 0, 0);
Imageline ($img, 0, 0, $red);
Imageellipse ($img, MB, $red);
Imagegif ($img, "./images/map2.gif");
Imagedestroy ($IMG);
Picture Normal scaling
Copy Code code as follows:
$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
Copy Code code as follows:
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
Copy Code code as follows:
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
Copy Code code as follows:
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
Copy Code code as follows:
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
Copy Code code as follows:
$back =imagecreatefromjpeg ("./images/hee.jpg");
$new =imagerotate ($back, 45, 0);
Imagejpeg ($new, "./images/hee9.jpg");
Picture Horizontal Flip Vertical Flip
Copy Code code as follows:
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
Copy Code code as follows:
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)); \ n before a pixel color array
$b _clr2=imagecolorsforindex ($back, Imagecolorat ($back, $i, $j)) \ \ Pull out 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");