How to implement PHP code processing pictures

Source: Internet
Author: User
Tags imagecopy imagejpeg transparent color
This article mainly describes how to implement the PHP code processing pictures, interested in the reference of friends, I hope to be helpful to everyone.

First, the image scaling code

Non-proportional picture scaling code

$fileNames = './images/hee.jpg '; list ($s _w, $s _h) =getimagesize ($fileNames); $per =0.3; $n _w= $s _w* $per; $n _h= $s _h* $per; $s _img=imagecreatefromjpeg ($fileNames); $n _img=imagecreatetruecolor ($n _w, $n _h); imagecopyresized ($n _img, $s _img, 0,0,0,0, $n _w, $n _h, $s _w, $s _h); Header ("Content-type:image/jpeg"); imagejpeg ($n _img, './images/hee2.jpg '); Imagedestroy ($n _img), Imagedestroy ($s _img), function sf ($backgroung, $BL, $location) {list ($width, $height) = GetImageSize ($backgroung); $n _w= $width * $bl, $n _h= $height * $BL; $s _img=imagecreatefromjpeg ($backgroung); $n _img= Imagecreatetruecolor ($n _w, $n _h) imagecopyresized ($n _img, $s _img,0,0,0,0, $n _w, $n _h, $width, $height); Header (" Content-type:image/jpeg ") imagejpeg ($n _img, $location) Imagedestroy ($s _img); Imagedestroy ($n _img);} SF ('./images/hee.jpg ', 0.3, './images/hee9.jpg ');

Equal scale picture scaling (and transparent color processing)

function Imagesf ($backgroung, $n _w, $n _h, $newFiles) {list ($s _w, $s _h, $imagetype) =getimagesize ($backgroung);// Equal scale fixed algorithm if ($n _w && ($s _w < $s _h)) {$n _w = ($n _h/$s _h) * $s _w;} else {$n _h = ($n _w/$s _w) * $s _h;} if ($imagetype ==2) {$s _img=imagecreatefromjpeg ($backgroung);} ElseIf ($imagetype ==3) {$s _img=imagecreatefrompng ($backgroung);} ElseIf ($imagetype ==1) {$s _img=imagecreatefromgif ($backgroung);} Else{echo "PHP does not support this type of image!! ";} $n _img=imagecreatetruecolor ($n _w, $n _h);//Obtain an index of the original graph $img_index=imagecolortransparent ($s _img);// Determines whether the original graphic index is within the number of graphic palettes (palette number 0~255); if ($img _index>=0 && $img _index<imagecolorstotal ($s _img)) {// Returns an associative array of key names with Red,green,blue and alpha, containing the corresponding values for the specified color index $trans=imagecolorsforindex ($s _img, $img _index);//print_r ($ Trans); Array ([red] = 255 [Green] = 255 [Blue] + 255 [Alpha] + 127)//Take the original color, assign a color to the new diagram $n_color=imagecolorallocate ( $n _img, $trans [' Red '], $trans [' Green '], $trans [' Blue ']; Imagefill ($n _img,0,0, $n _color);//the Acquired color, Sets the transparent color imagecolortransparent for the new diagram ($n _imG, $n _color);} Imagecopyresampled ($n _img, $s _img,0,0,0,0, $n _w, $n _h, $s _w, $s _h), if (Imagetypes () & img_jpg) {Header (" Content-type:image/jpeg "); imagejpeg ($n _img, $newFiles);} ElseIf (Imagetypes () & Img_png) {header ("content-type:image/png"); Imagepng ($n _img, $newFiles);} ElseIf (Imagetypes () & Img_gif) {header ("content-type:image/gif"); Imagegif ($n _img, $newFiles);} Else{echo "PHP does not support this type of image!! ";} Imagedestroy ($s _img); Imagedestroy ($n _img);} IMAGESF ('./images/map.gif ', 200,200, './images/map2.gif ');





Second, the picture clipping code

Picture clipping

function Imagecut ($backgroung, $cut _x, $cut _y, $cut _width, $cut _hight, $fileNames) {list (,, $imagetype) =getimagesize ($ Backgroung), if ($imagetype ==1) {$s _img=imagecreatefromgif ($backgroung);} ElseIf ($imagetype ==2) {$s _img=imagecreatefromjpeg ($backgroung);} ElseIf ($imagetype ==2) {$s _img=imagecreatefrompng ($backgroung);} Else{echo "PHP does not support this type of image!! ";} $n _img=imagecreatetruecolor ($cut _width, $cut _hight) imagecopyresized ($n _img, $s _img,0,0, $cut _x, $cut _y, $cut _width , $cut _hight, $cut _width, $cut _hight), if (Imagetypes () & Img_gif) {header ("content-type:image/gif"); Imagegif ($n _ IMG, $fileNames);} ElseIf (Imagetypes () & img_jpg) {header ("content-type:image/jpeg"); imagejpeg ($n _img, $fileNames);} ElseIf (Imagetypes () & Img_png) {header ("content-type:image/png"); Imagepng ($n _img, $fileNames);} Else{echo "PHP does not support this type of image!! ";}} Imagecut ('./images/hee.jpg ', ' n ', ' 345 ', '/images/hee4.jpg '); function cut ($backgroung, $x, $y, $cut _width, $cut _hight, $location) {$s _img=imagecreatefromjpeg ($backgroung); $n _img=imagecreatetruecolor ($Cut_width, $cut _hight); Imagecopy ($n _img, $s _img,0,0, $x, $y, $cut _width, $cut _hight); Header ("Content-type:image/jpeg Imagejpeg ($n _img, $location); Imagedestroy ($s _img); Imagedestroy ($n _img);} Cut ('./images/hee.jpg ', 345, './images/hee8.jpg ');



The picture is cropped and scaled to the cropped image, not scaled (that is, copying part of the image and resizing) imagecopyresized ()-Copy part of the image and resize it

Imagecopyresampled ()-resample copy part of image and resize

function CUTSF ($backgroung, $x, $y, $cut _width, $cut _hight, $n _w, $n _h, $location) {$s _img=imagecreatefromjpeg ($ Backgroung); $n _img=imagecreatetruecolor ($n _w, $n _h); imagecopyresized ($n _img, $s _img,0,0, $x, $y, $n _w, $n _h, $cut _ width, $cut _hight), header ("Content-type:image/jpeg"), imagejpeg ($n _img, $location); Imagedestroy ($s _img); Imagedestroy ($n _img);} CUTSF ('./images/hee.jpg ', 345, 330,200,200, './images/hee10.jpg ');





Three, picture and watermark: text watermark and image watermark code

picture Plus text watermark
function Mark_text ($backgroung, $x, $y, $text, $fileNames) {list (,, $imagetype) =getimagesize ($backgroung); ImageType = = 1) {$s _img=imagecreatefromgif ($backgroung);} ElseIf ($imagetype = = 2) {$s _img=imagecreatefromjpeg ($backgroung);} ElseIf ($imagetype = = 3) {$s _img=imagecreatefrompng ($backgroung);} $green =imagecolorallocate ($s _img,0,255,0) imagettftext ($s _img,20,0, $x, $y, $green, './font/msyh.ttf ', $text); Imagetypes () & Img_gif) {header ("content-type:image/gif"); Imagegif ($s _img, $fileNames);} ElseIf (Imagetypes () & img_jpg) {header ("content-type:image/jpeg"); Imagejpeg ($s _img, $fileNames);} ElseIf (Imagetypes () & Img_png) {header ("content-type:image/png"); Imagepng ($s _img, $fileNames);} Imagedestroy ($s _img);} $text =iconv (' gb2312 ', ' utf-8 ', ' elaborating php '),//mark_text ('./images/hee.jpg ', 150,250, $text, './images/hee5.jpg ');


picture plus image Watermark

function Mark_pic ($backgroung, $x, $y, $waterpic, $fileNames) {$s _img=imagecreatefromjpeg ($backgroung); $waterpic _img =imagecreatefromgif ($waterpic); $waterpic _w=imagesx ($waterpic _img); $waterpic _h=imagesy ($waterpic _img); imagecopy ($s _img,  $waterpic _img, $x, $y, 0,0, $waterpic _w, $waterpic _h); Header ("Content-type:image/jpeg"); Imagejpeg ($s _ IMG, $fileNames); Imagedestroy ($s _img); Imagedestroy ($waterpic _img);} Mark_pic ('./images/hee.jpg ', 50,200, './images/gaolf.gif ', './images/hee6.jpg ');





Four, the picture rotation code
Picture Rotation Resource imagerotate ()-Rotate the image at a given angle to return to the new resource
function Imagexz ($backgroung, $angle, $location) {$s _img=imagecreatefromjpeg ($backgroung); $n _img=imagerotate ($s _ IMG, $angle, 0), header ("Content-type:image/jpeg"), imagejpeg ($n _img, $location); Imagedestroy ($s _img); Imagedestroy ( $n _img);} Imagexz ('./images/hee.jpg ', ' images/hee10.jpg '); 90 180 270 360






Five, picture flip

The
picture is flipped along the y-axis
, right and left.
function turn_y ($backgroung, $newFiles) {$s _img=imagecreatefromjpeg ($backgroung); $width =imagesx ($s _img); $height Imagesy ($s _img), $n _img=imagecreatetruecolor ($width, $height); for ($x =0; $x < $width; $x + +) {imagecopy ($n _img, $s _ IMG, $width-$x -1,0, $x, 0, 1, $height);} Header ("Content-type:image/jpeg"), imagejpeg ($n _img, $newFiles), Imagedestroy ($s _img); Imagedestroy ($n _img);} Turn_y ('./images/hee.jpg ', './images/hee11.jpg ');



The
image is flipped along the x-axis
.
function Turn_x ($backgroung, $newFiles) {$s _img=imagecreatefromjpeg ($backgroung); $width =imagesx ($s _img); $height =imagesy ($ S_IMG), $n _img=imagecreatetruecolor ($width, $height); for ($y =0; $y < $height; $y + +) {imagecopy ($n _img, $s _img, 0,$ height-$y-1, 0, $y, $width, 1);} Header ("Content-type:image/jpeg"), imagejpeg ($n _img, $newFiles), Imagedestroy ($s _img); Imagedestroy ($n _img);}
 Turn_x ('./images/hee.jpg ', './images/hee12.jpg '); 





vi. picture sharpening
Function Sharp ($background, $degree, $save) {$back =imagecreatefromjpeg ($background); $b _x=imagesx ($back); $b _y= Imagesy ($back); $dst =imagecreatefromjpeg ($background); for ($i = $b _x-1; $i >0; $i-) {for ($j = $b _y-1; $j >0; $j--) {$ B_clr1=imagecolorsforindex ($back, Imagecolorat ($back, $i-1, $j-1)); $b _clr2=imagecolorsforindex ($back, Imagecolorat ($back, $i, $j)); $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)), $g =min (255, Max ($g, 0)), $b =min (255, Max ($b, 0)), if (($d _clr=imagecolorexact ($DST, $r, $g, $b)) ==-1) {$d _clr= Imagecolorallocate ($DST, $r, $g, $b);} Imagesetpixel ($DST, $i, $j, $d _clr);}} Imagejpeg ($DST, $save); Imagedestroy ($back); Imagedestroy ($DST);} Sharp ("./images/hee.jpg", "./images/hee15.jpg");



Image sharpening (Analytic principle) (first get the original color, and then sharpen the original color, and finally in the sharpening color to the original)

#解析开始:
#1 Create a picture resource to sharpen
#eg: $s _img=imagecreatefromjpeg ('./images/hee.jpg ');
#2 get the width and height of the image resource, create a two-layer loop (the outer control-wide inner control is high) to sharpen each pixel
#eg: $width =imagesx ($s _img);
# $height =imagesy ($s _img);
# for ($i = $width-1; $i >0; $i-) {
# for ($j = $height-1; $j >0; $j-) {
#
# }
# }
#3 sharpen each pixel color in the inner loop (write in the inner loop):
#3.1 Use function $color=imagecolorsforindex () to get the color of the image index, that is, get the current color and get the previous color
#eg: $s _img_color1=imagecolorsforindex ($s _img, imagecolorat ($s _img, $i-1, $j-1));//Previous Color
# $s _img_color2=imagecolorsforindex ($s _img, imagecolorat ($s _img, $i, $j));//Current color
#3.2 fixed algorithm sharpening of acquired colors such as sharpening: $degree =20
#eg: $r =intval ($color ["Red"]+ $degree * ($color ["Red"]-$color ["Red"]);
# $g =intval ($s _img_color2["green"]+ $degree * ($s _img_color2["green"]-$s _img_color1["green"));
# $b =intval ($s _img_color2["blue"]+ $degree * ($s _img_color2["blue"]-$s _img_color1["blue"));
#3.3 to control the obtained RGB color within the 0~255 normal range
# $r =min (255, Max ($r, 0));
# $g =min (255, Max ($g, 0));
# $b =min (255, Max ($b, 0));
#4 re-assigns each pixel color of the sharpening to each pixel color of the original (written in the inner loop)
#4.1 index of color after sharpening: $rh _color
#eg: if (($rh _color=imagecolorexact ($s _img, $r, $g, $b)) ==-1) {
# $rh _color=imagecolorallocate ($s _img, $r, $g, $b);
# }
#4.2 re-assigns each pixel color of the sharpening to each pixel color of the original image
Eg:imagesetpixel ($s _img, $i, $j, $rh _color);
#5 Save Picture Resources (write outside the loop)
#eg: imagejpeg ($s _img './images/hee16.jpg ');
#6 Close the picture resource (write outside the loop)
#eg: Imagedestroy ($s _img);
#解析结束


Follow the resolution in the write once picture sharpening code

Function Sharp ($background, $degree, $location) {#step 1 Create a picture resource $s_img=imagecreatefromjpeg ($background); #step 2 Get the picture resource's wide height $b_x=imagesx ($s _img), $b _y=imagesy ($s _img), #step 32-layer loop to control the wide inner layer control of the high for ($i = $b _x-1, $i >0; $i--) {for ($j = $b _y-1; $j >0; $j--) {#step 4 Gets the color of the image index: the current color and the previous color $s_img_color1=imagecolorsforindex ($s _img, imagecolorat ($s _img, $i-1, $j-1)); $s _ Img_color2=imagecolorsforindex ($s _img, imagecolorat ($s _img, $i, $j)); #step 5 fixed algorithm sharpening the obtained color $r=intval ($s _img_color2[ "Red"]+ $degree * ($s _img_color2["red"-$s _img_color1["red")) $g =intval ($s _img_color2["green"]+ $degree * ($s _img_ Color2["Green" – $s _img_color1["green")), $b =intval ($s _img_color2["blue"]+ $degree * ($s _img_color2["blue"-$s _img _color1["Blue"]); #step 6 The obtained RGB color control within the normal range of 0~255 $r=min (255, Max ($r, 0)); $g =min (255, Max ($g, 0)); $b =min (255, Max ($b, 0) ); #step 7 Gets the index of the sharpened color if (($d _clr=imagecolorexact ($s _img, $r, $g, $b)) ==-1) {$d _clr=imagecolorallocate ($s _img, $r, $g, $b) ;} #step 8 Re-assign the sharpened color to each pixel of the picture resource imagesetpixel ($s _img, $i, $j, $d _clr);}} ImAgejpeg ($s _img, $location); Imagedestroy ($s _img);} Sharp ('./images/hee.jpg ', './images/hee16.jpg ');
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.