Image processing Class (improved version)

Source: Internet
Author: User
 $info [0], ' height ' = $info [1], ' type ' =>image_type_to_extension ($info [2],false), ' mime ' = $info [' MIME ']);// Get picture information $type=self:: $info [' type ']; $fun = "imagecreatefrom{$type}"; self:: $image = $fun ($SRC);} /** * @param int $width $height should be declared in the configuration file, you can cancel the parameter * @return thumbnail image resource * thumbnail image formation and use */public function thumb ($width, $ Height) {//New Town color picture $image_thumb =imagecreatetruecolor ($width, $height); #获取图片的宽高比 $src _m = self:: $info [' width ']/self::   $info [' height ']; #源文件空格比 $DST _m = $width/$height; #缩略图宽高比 # source file picture is the width of the N:1 type, change the high if ($src _m > $dst _m) {$cha _width = $width; $cha _height = Ceil ($width/$src _m);} else{#源文件图片是 1:n Type, change the width $cha_width = Floor ($height * $src _m); $cha _height = $height;} #对缩略图的其实位置进行重置 $dst _x = ($width-$cha _width)/2; $dst _y = ($height-$cha _height)/2 imagecopyresampled ($image _thumb, sel F:: $image, $dst _x, $dst _y, 0, 0, $cha _width, $cha _height, Self:: $info [' Width '],self:: $info [' height ']); #生成缩略图self:: $im Age = $image _thumb;//#显示缩略图图片//Self::show (self:: $image); #保存缩Sketch Self::save (Self::getnewname ());//Destroy Picture Imagedestroy ($this->image_thumb); #返回缩略图名字return self::getnewname ();} #水印的生成坐标private static function setLocal ($pos) {#对 The pos parameter, specify the corresponding watermark generation coordinate # Watermark image to be recorded in the config file $conf [' Mark ']switch ($pos) {   Case 1: $x = 0, $y = 0;break; case 2:default: $x = self:: $info [' width ']-50; $y = self:: $info [' Height ']-50; #不该是20 This customization, should be changed to a watermark image of the width of the high}return $local =array (' x ' = $x, ' y ' = $y);} #添加文字水印public function Fontmark ($content, $font _url, $size, $angle) {#字体颜色 $col = Imagecolorallocatealpha (self:: $image, Mt_rand (200,255), Mt_rand (200,255), Mt_rand (200,255), #获取水印输出位置坐标 $local = self::setlocal (2); Imagettftext (self:: $image, $size, $angle, $local [' X '], $local [' Y '], $col, $font _url, $content); #显示缩略图图片self:: Show (self:: $image); #保存文字水印 No save Path added Self::save (Self::getnewname ()); #返回水印图片的名字return Self::getnewname ();  } #添加图片水印public function ImageMark ($url, $alpha) {$info = getimagesize ($url); #获取图片信息 $type =image_type_to_extension ($info [2],false); $fun = "imagecreatefrom{$type}"; #获取水印输出位置坐标 $locaL = self::setlocal (2); $water = $fun ($url); #水印图片imagecopymerge (self:: $image, $water, $local [' X '], $local [' y '],0, 0, $info [0], $info [1], +); #销毁图片水印imagedestroy ( $water); #显示缩略图图片self:: Show (self:: $image), #保存图片水印 no Save Path added Self::save (Self::getnewname ()); #返回水印图片的名字return Self:: Getnewname ();} #生成随机的 Picture Name/** * @return String returns a new name * */private static function Getnewname () {#获取一个时间 $str = time (); #获取随机字符串 $ string = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOASDFGHJKZXCVBNM1234567890"; for ($i =0; $i <6; $i + +) {$str. = $string [Mt_ Rand (0, strlen ($string)-1)];} Return $str. Self:: $info [' type '];} #在浏览器中输出图片private static function Show () {header ("Content-type:". Self:: $info [' mime ']), $funs = "image". Self:: $info [' Type ']; $funs (self:: $image);} #把图片保存到硬盘private static function Save ($newname) {$funs = "image". Self:: $info [' type ']; $funs (self:: $image, $newname. ".".  Self:: $info [' type ']); #在此处加入配置文件的生成目录} #销毁图片public function __destruct () {Imagedestroy (self:: $image);}}

The above describes the image processing class (improved version), including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.

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