- PHP add watermark & scale thumbnail & fixed height & fixed width class.
- Class image_process{
- Public $source; Original
- Public $source _width; Original width
- Public $source _height; Original height
- Public $source _type_id;
- Public $orign _name;
- Public $orign _dirname;
- Incoming original path
- Public function __construct ($source) {
- $this->typelist = Array (1=> ' gif ',2=> ' jpg ',3=> ' png ');
- $ginfo = getimagesize ($source);
- $this->source_width = $ginfo [0];
- $this->source_height = $ginfo [1];
- $this->source_type_id = $ginfo [2];
- $this->orign_url = $source;
- $this->orign_name = basename ($source);
- $this->orign_dirname = dirname ($source);
- }
- Determine the format of the image file, and return the code that is recognized by PHP
- Public Function Judgetype ($type, $source) {
- if ($type = = 1) {
- Return Imagecreatefromgif ($source); Gif
- }else if ($type = = 2) {
- Return Imagecreatefromjpeg ($source); Jpg
- }else if ($type = = 3) {
- Return Imagecreatefrompng ($source); Png
- }else{
- return false;
- }
- }
- Create a watermark Picture
- Public Function Watermakeimage ($logo) {
- $linfo = getimagesize ($logo);
- $logo _width = $linfo [0];
- $logo _height = $linfo [1];
- $logo _type_id = $linfo [2];
- $sourceHandle = $this->judgetype ($this->source_type_id, $this->orign_url);
- $logoHandle = $this->judgetype ($logo _type_id, $logo);
- if (! $sourceHandle | |! $logoHandle) {
- return false;
- }
- $x = ($this->source_width-$logo _width)/2;
- $y = ($this->source_height-$logo _height)/2;
- Imagecopy ($sourceHandle, $logoHandle, $x, $y, 0,0, $logo _width, $logo _height);
- $newPic = $this->orign_dirname. ' \water_ '. Time (). '. '. $this->typelist[$this->source_type_id];
- if ($this->saveimage ($sourceHandle, $newPic)) {
- Imagedestroy ($sourceHandle);
- Imagedestroy ($logoHandle);
- }
- }
- Fixed height width
- Public Function Fixsizeimage ($width, $height) {
- if ($width > $this->source_width) $this->source_width;
- if ($height > $this->source_height) $this->source_height;
- if ($width = = = False) {
- $width = Floor ($this->source_width/($this->source_height/$height));
- }
- if ($height = = = False) {
- $height = Floor ($this->source_height/($this->source_width/$width));
- }
- $this->tinyimage ($width, $height);
- }
- Scale pictures in equal proportion
- Public Function Scaleimage ($scale) {
- $width = Floor ($this->source_width * $scale);
- $height = Floor ($this->source_height * $scale);
- $this->tinyimage ($width, $height);
- }
- Create a thumbnail image
- Public Function Tinyimage ($width, $height) {
- $tinyImage = Imagecreatetruecolor ($width, $height);
- $handle = $this->judgetype ($this->source_type_id, $this->orign_url);
- if (function_exists (' imagecopyresampled ')) {
- Imagecopyresampled ($tinyImage, $handle, 0, 0, 0, 0, $width, $height, $this->source_width, $this->source_height);
- }else{
- Imagecopyresized ($tinyImage, $handle, 0, 0, 0, 0, $width, $height, $this->source_width, $this->source_height);
- }
- $newPic = $this->orign_dirname. ' \thumb_ '. Time (). ' _ '. $width. ' _ ". $height.". $this->typelist[$this->source_type_id];
- if ($this->saveimage ($tinyImage, $newPic)) {
- Imagedestroy ($tinyImage);
- Imagedestroy ($handle);
- }
- }
- Save picture
- Private Function SaveImage ($image, $url) {
- if (imagejpeg ($image, $url)) {
- return true;
- }
- }
- }
- $imgHandle = new Image_process (' D:\AppServ\www\test\getimg\14061907445601.jpg ');
- $imgHandle->watermakeimage (' D:\AppServ\www\test\getimg\shougongke.png '); Create a watermark Picture
- $imgHandle->fixsizeimage (200,150); Fixed-length pictures
- $imgHandle->scaleimage (0.2); Equal scale Scaling
- ?>
Copy Code
|