Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the various processing functions of php for images?

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail what the various image processing functions of php have, the editor thinks it is very practical, so share it for you to do a reference, I hope you can get something after reading this article.

First, create picture resources

Imagecreatetruecolor (width,height)

Imagecreatefromgif (picture name)

Imagecreatefrompng (picture name)

Imagecreatefromjpeg (picture name); draw a variety of images imagegif (picture resources, save path)

Imagepng ()

Imagejpeg ()

Second, get the attributes of the picture

Imagesx (res// width

Imagesy (res// height

Getimagesize (file path)

Returns an array of four units. 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 new IMAGETYPE constants in PHP 4.3.0. Index 3 is a text string with the content "height=" yyy "width=" xxx "" and can be used directly for IMG tags.

Destroy image resources

Imagedestroy (Picture Resources)

III. Transparent treatment

PNG and jpeg transparent colors are normal, only gif is not normal.

Imagecolortransparent (resource image [, int color]) / / sets a color to be transparent

Imagecolorstotal ()

Imagecolorforindex ()

Fourth, the cutting of the picture

Imagecopyresized ()

Imagecopyresampled ()

Fifth, add watermark (text, picture)

String Encoding conversion string iconv (string $in_charset, string $out_charset, string $str)

VI. Picture rotation

Imagerotate (); / / set the angle of the picture to flip

7. Flip of the picture

Flip along X axis and Y axis

8. Sharpening

Imagecolorsforindex ()

Imagecolorat ()

Draw a figure on the picture $img=imagecreatefromgif (". / images/map.gif")

The copy code is as follows:

$red= imagecolorallocate ($img, 255,0,0)

Imageline ($img, 0,0,100,100, $red)

Imageellipse ($img, 200,100,100,100, $red)

Imagegif ($img, ". / images/map2.gif")

Imagedestroy ($img)

Normal zoom of picture

The copy 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 it

Imagecopyresized ($new, $img,0, 0 height, 0,0 width)

/ / Image output new picture, save as

Imagejpeg ($new, ". / images/hee2.jpg")

Imagedestroy ($new)

Imagedestroy ($img)

The picture is scaled proportionally and the transparent color is not processed.

The copy code is as follows:

Function thumn ($background, $width, $height, $newfile) {

List ($image, $background) = getimagesize ($image); / / get the height and width of the original image

If ($width & & ($sqw)

< $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", 200, 200, "./images/hee3.jpg"); gif透明色处理 复制代码 代码如下: 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)) {/ / judge index color

$tran=imagecolorsforindex ($img, $otsc); / / Index color values

$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, $Simpw, $Spoch)

Imagegif ($new, $newfile)

Imagedestroy ($new)

Imagedestroy ($img)

}

Thumn ("images/map.gif", 200,200, ". / images/map3.gif")

Picture cropping

The copy code is 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")

Watermark the picture

Text watermarking

The copy code is as follows:

Function mark_text ($background, $text, $x, $y) {

$back=imagecreatefromjpeg ($background)

$color=imagecolorallocate ($back, 0,255,0)

Imagettftext ($back, 20,0, $x, $y, $color, "simkai.ttf", $text)

Imagejpeg ($back, ". / images/hee7.jpg")

Imagedestroy ($back)

}

Mark_text (". / images/hee.jpg", "detailed PHP", 150250)

/ / 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, $wimpw, $wreckh)

Imagejpeg ($back, ". / images/hee8.jpg")

Imagedestroy ($back)

Imagedestroy ($water)

}

Mark_pic (". / images/hee.jpg", ". / images/gaolf.gif", 50200)

Picture rotation

The copy code is as follows:

$back=imagecreatefromjpeg (". / images/hee.jpg")

$new=imagerotate ($back, 45,0)

Imagejpeg ($new, ". / images/hee9.jpg")

Picture flipped horizontally and vertically

The copy code is as follows:

Function turn_y ($background, $newfile) {

$back=imagecreatefromjpeg ($background)

$width=imagesx ($back)

$height=imagesy ($back)

$new=imagecreatetruecolor ($width, $height)

For ($xan0; $x < $width; $xylene +) {

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 ($ycm 0; $y < $height; $yearly +) {

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

The copy code is as follows:

Function sharp ($background, $degree, $save) {

$back=imagecreatefromjpeg ($background)

$b_x=imagesx ($back)

$b_y=imagesy ($back)

$dst=imagecreatefromjpeg ($background)

For ($iTun0; $I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report