PHP Image processing Function Daquan

Source: Internet
Author: User
Tags imagecopy imagejpeg transparent color
This article mainly introduces the PHP image processing function Daquan, has a certain reference value, now share to everyone, the need for friends can refer to

PHP image processing code sharing, including zooming, cropping, zooming, flipping, rotating, transparent, sharpening and so on.
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");

    1. <? 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

    1. <?php $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

    1. <?php 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", "$," "./images/hee3.jpg");

GIF Transparent Color processing

  1. <?php 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 =IMAGECR Eatetruecolor ($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

    1. <?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

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

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

Picture Horizontal Flip Vertical Flip

Click ( here ) to collapse or open

  1. <?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");?>
    Related recommendations:
  2. PHP utility functions, PHP functions

  3. PHP common functions, PHP functions

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.