Share PHP multi-function image processing class, _php tutorial

Source: Internet
Author: User
Tags transparent color learn php learn php programming

Share PHP multi-function image processing class,


This example for everyone to share the multi-functional PHP image processing class, for your reference, the specific content as follows

<?php/** * Image.class.php image Processing class * @author Administrator * * */class image{private $path = '; /** * Construction method * @param unknown $path */function __construct ($path = ") {if (!empty ($path)) {$this->p     Ath= $path; }}/** +-----------------------------------------------* ratio scaling function +------------------------------------------ -----* @param unknown $name need to process the name of the picture * @param unknown $width scaled width * @param unknown $height scaled height * @param String $thumb _prixs-scaled prefix name * @return Mixed $newname Returns the scaled file name */function thumb ($name, $width, $height, $thumb _prix s= ' Th_ ') {//get picture information $Info = $this->imageinfo ($name);//image width, height, type//Get picture resources, various types of pictures can create resources, Jpg,gif,png $im     agres= $this->img_resouce ($name, $Info);     Gets the size after calculating the proportions of the picture, $size = $this->getnewsize ($name, $width, $height, $Info);     Get new picture resources, handle transparent background $newimg = $this->getimage ($imagres, $size, $Info); Save as a new picture, return the new scaled picture name $newname = $this->savenewimage ($Newimg, $thumb _prixs $name, $Info);    return $newname; }/** +-----------------------------------------------------------------------* Watermark marker function +------------------- ----------------------------------------------------* @param unknown $backname background file name * @param unknown $watername water Print File name * @param number $waterpos watermark location * @param string $wa _prixs watermark Prefix name * @return Boolean */function Watermar K ($backname, $watername, $waterpos =0, $wa _prixs= ' Wa_ ') {if (File_exists ($this->path. $backname) && File_e              Xists ($this->path $watername)) {$backinfo = $this->imageinfo ($backname);              $waterinfo = $this->imageinfo ($watername);         if (! $pos = $this->getpos ($backinfo, $waterinfo, $waterpos)) {echo "watermark picture should not be smaller than the background picture";       return false;                } $backimg = $this->img_resouce ($backname, $backinfo);        $waterimg = $this->img_resouce ($watername, $waterinfo);     Var_dump ($backimg); exit ();   $backimg = $this->copyimage ($backimg, $waterimg, $pos, $waterinfo);     $this->savenewimage ($backimg, $wa _prixs. $backname, $backinfo);       }else{echo "Picture or watermark does not exist";     return false; }}/** +-----------------------------------------------------------------------* Get picture info function +----------------  -------------------------------------------------------* @param unknown $name * @return Unknown */private     function Imageinfo ($img) {$imageInfo =getimagesize ($img);       if ($imageInfo!==false) {$imageType =strtolower (substr (Image_type_to_extension ($imageInfo [2]), 1));       $imageSize =filesize ($IMG);         $Info =array ("width" = $imageInfo [0], "height" + = $imageInfo [1], "type" = = $imageType,       "Size" = = $imageSize, "mime" = = $imageInfo [' MIME ']);     return $Info;     }else{return false; }     }         /**    +--------------------------------------------------------* Create original image Format Function +--------------------------------------------------------* @param unknown $name * @pa Ram Unknown $imaginfo * @return Boolean|resource */Private Function Img_resouce ($name, $imageinfo) {$iamgeres =     $this->path. $name;      Var_dump ($iamgeres); exit ();          Switch ($imageinfo [' type ']) {case ' gif ': $img =imagecreatefromgif ($name);       Break          Case ' jpg ': $img =imagecreatefromjpeg ($name);       Break          Case ' png ': $img =imagecreatefrompng ($name);                  Break   } return $img; }/** +--------------------------------------------------* Get equal to scaled size function +---------------------------------------     -----------* @param unknown $name * @param unknown $width * @param unknown $height * @param unknown $imaginfo  * @return ambigous
 
  
    */Private Function getnewsize ($name, $width, $height, $imaginfo) {$size [' width ']= $imaginfo [' width '];     $size [' Height ']= $imaginfo [' height '];     if ($width < $imaginfo [' width ']) {$size [' width ']= $width;     } if ($height < $imaginfo [' height ']) {$size [' height ']= $height; }//Image scaling algorithm if ($imaginfo [' width ']* $size [' width ']> $imaginfo [' Height ']* $size [' height ']) {$size [' height ']     ]=round ($imaginfo [' Height ']* $size [' width ']/$imaginfo [' width '];     }else{$size [' Width ']=round ($imaginfo [' width ']* $size [' Height ']/$imaginfo [' height '];    } return $size; } Private Function GetImage ($imageres, $size, $imageinfo) {//Create a new True color image $newimg =imagecreatetruecolor ($size [' width ']     , $size [' height ']);     Defines a color as a transparent color $OTSC =imagecolortransparent ($imageres); Gets the number of colors for the color palette of the image if ($OTSC >=0&& $otsc <=imagecolorstotal ($imageres)) {//Get the color of an index $stran =IMAGEC       Olorsforindex ($imageres, $OTSC);     assigning colors to Images  $newt =imagecolorallocate ($imageres, $stran [' Red '], $stran [' Green '], $stran [' Blue '];       Area fill function Imagefill ($newimg, 0, 0, $NEWT);         Defines the transparent color imagecolortransparent ($newimg, $NEWT) for the image; } imagecopyresized ($newimg, $imageres, 0, 0, 0, 0, $size [' width '], $size [' height '], $imageinfo [' width '], $imageinfo [' H            Eight ']);          Imagedestroy ($imageres);     return $newimg; }/** +----------------------------------------------* Save image function +-------------------------------------------- --* @param unknown $newimg * @param unknown $newname * @param unknown $imageinfo * @return Unknown *         /Private Function Savenewimage ($newimg, $newname, $imageinfo) {switch ($imageinfo [' type ']) {case 1://gif         $result =imagegif ($newimg, $this->path. $newname);       Break         Case 2://jpg $result =imagejpeg ($newimg, $this->path. $newname);       Break Case 3://png $result =imagepng ($newimg, $this->paTh. $newname);     Break     } Imagedestroy ($NEWIMG);    return $newname; }/** +-----------------------------------------------------------------* Get watermark position function +------------------      -----------------------------------------------* @param unknown $backinfo background information * @param unknown $waterinfo watermark Information * @param unknown $waterpos watermark position * @return Boolean|multitype:number returns an array of coordinates */Private function GetPos ($bac Kinfo, $waterinfo, $waterpos) {if ($backinfo [' width ']< $waterinfo [' Width ']| |     $backinfo [' Height ']< $waterinfo [' height ']) {return false;         } switch ($waterpos) {case 1://the upper-left corner $posX = 0;          $posY = 0;       Break         Case 2://above $posX = $backinfo [' width ']-$waterinfo [' Width ']/2;          $posY = 0;       Break         Case 3://the upper right $posX = $backinfo [' width ']-$waterinfo [' width '];          $posY = 0;       Break         Case 4://left Chinese $posX = 0; $posY = $backinfo [' height ']-$waterinfo[' height ']/2;       Break         Case 5://is in the middle $posX = $backinfo [' width ']-$waterinfo [' Width ']/2;          $posY = $backinfo [' height ']-$waterinfo [' Height ']/2;       Break         Case 6://Right Chinese $posX = $backinfo [' width ']-$waterinfo [' width '];          $posY = $backinfo [' height ']-$waterinfo [' Height ']/2;       Break         Case 7://bottom Left $posX = 0;          $posY = $backinfo [' height ']-$waterinfo [' height '];       Break         Case 8://Bottom Center $posX = $backinfo [' width ']-$waterinfo [' Width ']/2;          $posY = $backinfo [' height ']-$waterinfo [' height '];       Break         Case 9://Bottom on right $posX = $backinfo [' width ']-$waterinfo [' width '];          $posY = $backinfo [' height ']-$waterinfo [' height '];       Break         Case 0:default: $posX =rand (0, $backinfo [' width ']-$waterinfo [' width '];          $posY =rand (0, $backinfo [' height ']-$waterinfo [' height '];     Break    } return Array (' PosX ' = $posX, ' posY ' = $posY); }    /**     +-------------------------------------------------------------------* Copy image +-------------------------------------------------------     ------------* @param unknown $backimg background resources * @param unknown $waterimg watermark resource * @param unknown $pos watermark Location * @param unknown $waterinfo Watermark information * @return Unknown */Private Function Copyimage ($backimg, $waterimg, $pos, $ Waterinfo) {imagecopy ($backimg, $waterimg, $pos [' PosX '], $pos [' PosY '], 0, 0, $waterinfo [' width '], $waterinfo [' H     Eight ']);          Imagedestroy ($WATERIMG);    return $backimg;  }}?>
 

The above is the whole content of this article, I hope that you learn PHP programming help.

http://www.bkjia.com/PHPjc/1127896.html www.bkjia.com true http://www.bkjia.com/PHPjc/1127896.html techarticle sharing PHP Multi-function image processing class, this example for everyone to share the multi-functional PHP image processing class, for your reference, specific content as follows PHP/** * Image.class.php Image Processing class * @a ...

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