首页 > php教程 > php手册 > php实现图片的操作,也就是裁切,跟加水印

php实现图片的操作,也就是裁切,跟加水印

WBOY
发布: 2016-06-13 10:43:28
原创
1410 人浏览过

 

class image {

      

        protected $img;    

        protected $types = array(

                        1 => 'gif',

                        2 => 'jpg',

                        3 => 'png',

                        6 => 'bmp'

                    );

       

        public function __construct($img=''){

                !$img && $this->param($img);

        }

 

        public function param($img){

                $this->img = $img;

                return $this;

        }

 

        public function getImageInfo($img){

                $info = @getimagesize($img);

                if(isset($this->types[$info[2]])){

                        $info['ext'] = $info['type'] = $this->types[$info[2]];

                } else{

                        $info['ext'] = $info['type'] = 'jpg';

                }

                $info['type'] == 'jpg' && $info['type'] = 'jpeg';

                $info['size'] = @filesize($img);

                return $info;

        }

 

        public function thumb($filename,$new_w=160,$new_h=120,$cut=0,$big=0){

        $info  = $this->getImageInfo($this->img);

        if(!empty($info[0])) {

            $old_w  = $info[0];

            $old_h  = $info[1];

            $type   = $info['type'];

            $ext    = $info['ext'];

            unset($info);

            if($old_w

                    return false;

            }

            if($cut == 0){

                    $scale = min($new_w/$old_w, $new_h/$old_h);

                    $width  = (int)($old_w*$scale);

                    $height = (int)($old_h*$scale);

                    $start_w = $start_h = 0;

                    $end_w = $old_w;

                    $end_h = $old_h;

            } elseif($cut == 1){

                        $scale1 = round($new_w/$new_h,2);

                        $scale2 = round($old_w/$old_h,2);

                        if($scale1 > $scale2){

                                $end_h = round($old_w/$scale1,2);

                                $start_h = ($old_h-$end_h)/2;

                                $start_w  = 0;

                                $end_w    = $old_w;

                        } else{

                                $end_w  = round($old_h*$scale1,2);

                                $start_w  = ($old_w-$end_w)/2;

                                $start_h = 0;

                                $end_h   = $old_h;

                        }

                        $width = $new_w;

                    $height= $new_h;

                } elseif($cut == 2){

                        $scale1 = round($new_w/$new_h,2);

                    $scale2 = round($old_w/$old_h,2);

                    if($scale1 > $scale2){

                            $end_h = round($old_w/$scale1,2);

                            $end_w = $old_w;

                    } else{

                            $end_w = round($old_h*$scale1,2);

                            $end_h = $old_h;

                    }

                    $start_w = 0;

                    $start_h = 0;

                    $width = $new_w;

                    $height= $new_h;

                }

            $createFun  = 'ImageCreateFrom'.$type;

            $oldimg     = $createFun($this->img);

            if($type!='gif' && function_exists('imagecreatetruecolor')){

                $newimg = imagecreatetruecolor($width, $height);

            } else{

                $newimg = imagecreate($width, $height);

            }

            if(function_exists("ImageCopyResampled")){

                    ImageCopyResampled($newimg, $oldimg, 0, 0, $start_w, $start_h, $width, $height, $end_w,$end_h);

            } else{

                ImageCopyResized($newimg, $oldimg, 0, 0, $start_w, $start_h, $width, $height, $end_w,$end_h);

            }

 

            $type == 'jpeg' && imageinterlace($newimg,1);

            $imageFun = 'image'.$type;

            !@$imageFun($newimg,$filename) && die("error!");

            ImageDestroy($newimg);

            ImageDestroy($oldimg);

            return $filename;

        }

        return false;

    }

        public function water($filename,$water,$pos=0,$pct=80){//加水印

                $info = $this->getImageInfo($water);

                if(!empty($info[0])){

                        $water_w = $info[0];

                        $water_h = $info[1];

                        $type = $info['type'];

                        $fun  = 'imagecreatefrom'.$type;

                        $waterimg = $fun($water);

                } else{

                        return false;

                }

                $info = $this->getImageInfo($this->img);

                if(!empty($info[0])){

                        $old_w = $info[0];

                        $old_h = $info[1];

                        $type  = $info['type'];

                        $fun   = 'imagecreatefrom'.$type;

                        $oldimg = $fun($this->img);

                } else{

                        return false;

                }

                $water_w >$old_w && $water_w = $old_w;

                $water_h >$old_h && $water_h = $old_h;

                

                dump($waterimg);

                dump($oldimg);

                switch($pos){

                        case 0:

                    $posX = rand(0,($old_w - $water_w));

                    $posY = rand(0,($old_h - $water_h));

                    break;

                case 1:

                    $posX = 0;

                    $posY = 0;

                    break;

                case 2:

                    $posX = ($old_w - $water_w) / 2;

                    $posY = 0;

                    break;

                case 3:

                    $posX = $old_w - $water_w;

                    $posY = 0;

                    break;

                case 4:

                    $posX = 0;

                    $posY = ($old_h - $water_h) / 2;

                    break;

                case 5:

                    $posX = ($old_w - $water_w) / 2;

                    $posY = ($old_h - $water_h) / 2;

                    break;

                case 6:

                    $posX = $old_w - $water_w;

                    $posY = ($old_h - $water_h) / 2;

                    break;

                case 7:

                    $posX = 0;

                    $posY = $old_h - $water_h;

                    break;

                case 8:

                    $posX = ($old_w - $water_w) / 2;

                    $posY = $old_h - $water_h;

                    break;

                case 9:

                    $posX = $old_w - $water_w;

                    $posY = $old_h - $water_h;

                    break;

                default:

                    $posX = rand(0,($old_w - $water_w));

                    $posY = rand(0,($old_h - $water_h));

                    break;  

                }

                imagealphablending($oldimg, true);

                imagecopymerge($oldimg, $waterimg, $posX, $posY, 0, 0, $water_w,$water_h,$pct);

                $fun = 'image'.$type;

                !@$fun($oldimg, $filename) && die('error!');

                  imagedestroy($oldimg);

                  imagedestroy($waterimg);

                  return $filename;       

        }

}

 

?>

 

 

 

$img1 = './image/test.jpg';

$img2 = './image/test_new.jpg';

$water = './image/water.gif';

$img = new image();

$img->param($img1)->thumb('./image/test_0.jpg', 200,200,0);

$img->param($img1)->thumb('./image/test_1.jpg', 200,200,1);

$img->param($img1)->thumb('./image/test_2.jpg', 200,200,2);

$img->param($img1)->water($img2,$water,9);

?>

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板