(Advanced) PHP image processing functions and code examples

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

Imagecreatetruecolor (width,height); imagecreatefromgif (picture name); imagecreatefrompng (picture name); Imagecreatefromjpeg (image name) ; draw a variety of image imagegif (picture resource, save 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 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");

. The code is as follows:

<? PHP/** * Image Sharpening processing */$red = imagecolorallocate ($img, 255, 0, 0); Imageline ($img, 0, 0, +, +, $red); Imageellipse ($img, $, $red); Imagegif ($img, "./images/map2.gif"); Imagedestroy ($IMG);


Picture Normal Zoom
The code is 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
The code is 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 =i Magecreatetruecolor ($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", "$," "./images/hee3.jpg");

GIF Transparent Color processing
The code is as follows:

function Thumn ($background, $width, $height, $newfile) {list ($s _w, $s _h) =getimagesize ($background); if ($width && Amp ($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)) {//Determine 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", "$," "./images/map3.gif");


Picture clipping

. The code is as follows:

<?php/*** Picture Clipping processing * edit by Www.jbxue.com*/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"); >


Image watermark and Watermark text

. The code is as follows:

<? PHP/** *  * Image Add text watermark */function Mark_text ($background, $text, $x, $y) {$back =imagecreatefromjpeg ($background); $colo R=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

. The code is as follows:

<? PHP/** * Image rotation */$back =imagecreatefromjpeg ("./images/hee.jpg"); $new =imagerotate ($back, 45, 0); Imagejpeg ($new, "./images/hee9.jpg");?>


Picture Horizontal Flip Vertical Flip

. The code is as follows:

<?php/** * 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); The 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");?>

The above is (advanced) PHP image processing functions and code Examples of content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

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