ホームページ  >  記事  >  バックエンド開発  >  PHP は画像操作、つまり切り抜きと透かしを実装します_PHP チュートリアル

PHP は画像操作、つまり切り抜きと透かしを実装します_PHP チュートリアル

WBOY
WBOYオリジナル
2016-07-13 17:44:401127ブラウズ

 

クラスイメージ{

      

        保護された $img;    

        protected $types = array(

                        1 => 「gif」、

                        2 => 'jpg'、

                        3 => 'png'、

                        6 => 「bmp」

                    );

       

        パブリック関数 __construct($img=''){

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

        }

 

        パブリック関数パラメータ($img){

                $this->img = $img;

                $this を返します;

        }

 

        パブリック関数 getImageInfo($img){

                $info = @getimagesize($img);

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

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

                } 他{

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

                }

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

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

                $info を返します;

        }

 

        パブリック関数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 < $new_h && $old_h < $new_w && !$big){

                    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;

                        } 他{

                                $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;

                    } 他{

                            $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);

            } 他{

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

            }

            if(function_exists("ImageCopyResampled")){

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

            } 他{

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

            }

 

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

            $imageFun = '画像'.$type;

            !@$imageFun($newimg,$filename) && die("エラー!");

            ImageDestroy($newimg);

            ImageDestroy($oldimg);

            $ファイル名を返します;

        }

        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);

                } 他{

                        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);

                } 他{

                        false を返します;

                }

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

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

                

                ダンプ($waterimg);

                ダンプ($oldimg);

                スイッチ($pos){

                        ケース0:

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

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

                    休憩;

                ケース1:

                    $posX = 0;

                    $posY = 0;

                    休憩;

                ケース 2:

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

                    $posY = 0;

                    休憩;

                ケース 3:

                    $posX = $old_w - $water_w;

                    $posY = 0;

                    休憩;

                ケース4:

                    $posX = 0;

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

                    休憩;

                ケース5:

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

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

                    休憩;

                ケース6:

                    $posX = $old_w - $water_w;

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

                    休憩;

                ケース7:

                    $posX = 0;

                    $posY = $old_h - $water_h;

                    休憩;

                ケース8:

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

                    $posY = $old_h - $water_h;

                    休憩;

                ケース9:

                    $posX = $old_w - $water_w;

                    $posY = $old_h - $water_h;

                    休憩;

                デフォルト:

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

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

                    壊す;  

                }

                imagealphablending($oldimg, true);

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

                $fun = '画像'.$type;

                !@$fun($oldimg, $filename) && die('エラー!');

                  imagedestroy($oldimg);

                  imagedestroy($waterimg);

                  $ファイル名を返します;       

        }

}

 

?>

 

 

 

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

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

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

$img = 新しい画像();

$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);

?>

www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/478728.html技術記事 ?クラスイメージ { 保護された $img; protected $types = array( 1 = gif, 2 = jpg, 3 = png, 6 = bmp );パブリック関数 __construct($img=){ !$img $this-param($img); } パブリック関数パラメータ...
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。