PHP various processing function code summary of the image _php tutorial

Source: Internet
Author: User
Tags imagecopy imagejpeg transparent color
First, create a picture resource

Imagecreatetruecolor (Width,height);
Imagecreatefromgif (image name);
Imagecreatefrompng (image name);
Imagecreatefromjpeg (picture name); draw a variety of image imagegif (picture resources, save the path);
Imagepng ()
Imagejpeg ();

Second, get picture properties

Imagesx (res//width
Imagesy (res//height
getimagesize (file path)
Returns an array with 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 a marker 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 content "height=" yyy "width=" xxx ", which can be used directly with the IMG tag.
Destroying image Resources
Imagedestroy (picture resources);

Third, transparent processing

PNG, JPEG transparent color is 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, watermark (text, pictures)

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

Six, the picture rotation

Imagerotate ();//Make an angle picture flip

Seven, the picture of the flip

Flips along the x-axis along the y-axis

Eight, sharpening

Imagecolorsforindex ()

Imagecolorat ()

Draw a graphic on the picture $img =imagecreatefromgif ("./images/map.gif");
Copy the Code code as follows:
$red = Imagecolorallocate ($img, 255, 0, 0);
Imageline ($img, 0, 0, +, +, $red);
Imageellipse ($img, $, $red);
Imagegif ($img, "./images/map2.gif");
Imagedestroy ($IMG);

Picture Normal Zoom
Copy the 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, etc. scale, not handle transparent color
Copy the Code code as follows:
function Thumn ($background, $width, $height, $newfile) {
List ($s _w, $s _h) =getimagesize ($background);//Get 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", "A./images/hee3.jpg");

GIF Transparent Color processing
Copy the 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", "A./images/map3.gif");

Picture clipping
Copy the 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", the 117, the "./images/hee5.jpg");

Picture plus watermark

Text watermark

Copy the 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", "elaborating PHP", 150, 250);

Image 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 the 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 the 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");

Image Sharpening

Copy the 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)); \ \ previous pixel color array
$b _clr2=imagecolorsforindex ($back, Imagecolorat ($back, $i, $j)); \ \ Remove the current color array

$r =intval ($b _clr2["Red"]+ $degree * ($b _clr2["Red"-$b _clr1["Red"]); \ \ Deepen
$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)) ==-1) {//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");

http://www.bkjia.com/PHPjc/328053.html www.bkjia.com true http://www.bkjia.com/PHPjc/328053.html techarticle First, create a picture resource Imagecreatetruecolor (width,height); imagecreatefromgif (picture name); imagecreatefrompng (picture name); Imagecreatefromjpeg (picture name), draw a variety of images ...

  • 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.