PHP add watermark & scale thumbnail & fixed height & fixed width class. When using the Foreach Loop, sleep is required to set a time or follow the returned value after processing, otherwise the processing is not complete.
Download: Http://pan.baidu.com/s/1ntKAfFF
- File name: image_process.class.php
- Class image_process{
- public $source;//Original
- Public $source _width;//Wide
- Public $source _height;//High
- Public $source _type_id;
- Public $orign _name;
- Public $orign _dirname;
- Incoming picture 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);
- }
- Judgment and processing, return PHP recognizable encoding
- 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;
- }
- }
- Generate Watermark Map
- Public Function Watermarkimage ($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;
- $y = $this->source_height-$logo _height;
- Imagecopy ($sourceHandle, $logoHandle, $x, $y, 0,0, $logo _width, $logo _width) or Die ("fail to combine");
- $newPic = $this->orign_dirname. ' \water_ '. Time (). '. '. $this->typelist[$this->source_type_id];
- if ($this->saveimage ($sourceHandle, $newPic)) {
- Imagedestroy ($sourceHandle);
- Imagedestroy ($logoHandle);
- }
- }
- Fix width
- Height = true headspace height
- width = true headspace 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);
- }
- Proportional scaling
- $scale Zoom ratio
- Public Function Scaleimage ($scale) {
- $width = Floor ($this->source_width * $scale);
- $height = Floor ($this->source_height * $scale);
- $this->tinyimage ($width, $height);
- }
- Create a thumbnail
- Private 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 = Time (). ' _ '. $width. ' _ '. $height. '. $this->typelist[$this->source_type_id];
- $newPic = $this->orign_dirname. ' \thumb_ '. $newPic;
- if ($this->saveimage ($tinyImage, $newPic)) {
- Imagedestroy ($tinyImage);
- Imagedestroy ($handle);
- }
- }
- Save picture
- Private Function SaveImage ($image, $url) {
- if (imagejpeg ($image, $url)) {
- return true;
- }
- }
- }
Copy Code
- Use
-
- Include (' image_process.class.php ');
- $m = Array (
- ' D:\myspace\test\image_process\1.jpg ',
- ' D:\myspace\test\image_process\2.jpg ',
- ' D:\myspace\test\image_process\3.jpg ',
- ' D:\myspace\test\image_process\4.jpg '
- );
- $img = ' D:\myspace\test\image_process\1.jpg ';
- $logo = ' D:\myspace\test\image_process\logo.png ';
- foreach ($m as $item) {
- $s = new image_process ($item);
- $s->watermarkimage ($logo);
- $s->scaleimage (0.8);
- $s->fixsizeimage (200,false);
- Sleep (1);
- }
Copy Code |