??? ????, ??? ?? ??? ???? ??? ??? ?? ??????.
??? ???? ???? ???? ? ??? ????. ??? ???? ????? ?? ???? ???, ?? ??? ???? ??? ? ???? ????. ? ???? ?? ??? ??? ??? ???? ??? ????.
?? ?? ??: ???? ??? ?? ??? ????? ???? ?????? ??? ? ????. ??? ?? ??? ?? ??? ????? ???? ????.
??? ? ??: ??? ??? ?? ??? ????? ???? ?? ??? ??? ? ????. ??? ??? ???? ??? ? ????.image.php
- /**
- *
- * ??? ?? ???
- * @author FC_LAMP
- * @internal ?? ??: ????, ???
- */
- class Img
- {
- //??? ??
- ??? $exts = ??('jpg', 'jpeg', 'gif', 'bmp', 'png' );
-
- /**
- *
- *
- * @throws ??
- */
- ?? ?? __construct()
- {
- if (! function_exists ( 'gd_info' ))
- {
- throw new Exception ( 'GD ?????? ???? ?????!' );
- }
- }
-
- /**
- *
- * ??? ? ??
- * @param $src_img ???
- * @param $save_img ?? ???
- * @param $option ???? ??(?? ??): $maxwidth width $maxheight height
- * array('width'=>xx,'height'=>xxx)
- * @internal
- * ???? ?? ??? ?? ?? ?? ???? ???? ??? ?? ??
- *?? ????? ?? ??? ????? ???? ??? ?????.
- */
- ?? ?? Thumb_img($src_img, $save_img = '', $option)
- {
-
- if (?? ?? ( $option ['width'] ) ?? ?? ?? ( $option ['height'] ))
- {
- return array ('flag' => False, 'msg' = > '?? ???? ??? ??? 0?? ?? ? ????.' );
- }
- $org_ext = $this->is_img ( $src_img );
- if (! $org_ext [' flag'])
- {
- return $org_ext;
- }
-
- //?? ??? ??? ??? ???? ??
- if (!empty ( $save_img ))
- {
- $f = $this->check_dir ( $save_img );
- if (! $f ['flag'])
- {
- return $f;
- }
- }
-
- //?? ??? ????
- $org_funcs = $this->get_img_funcs ( $org_ext ['msg'] );
-
- //???? ?? ??
- $source = $org_funcs ['create_func'] ( $src_img );
- $src_w = Imagesx ( $source );
- $src_h = imagey ( $source );
-
- //?? ??? ??(??? ?? ?? ?? ??? ??)
- $dst_scale = $option ['height'] / $option ['width'] // ?? ??? ?? ?? ??
- $src_scale = $ src_h / $src_w; // ?? ??? ?? ?? ??
- if ($src_scale >= $dst_scale)
- { // ?? ??
- $w = intval ( $src_w );
- $h = intval ( $dst_scale * $w ) ;
-
- $x = 0;
- $y = ($src_h - $h) / 3;
- } else
- { // ?? ??
- $h = intval ( $ src_h );
- $w = intval ( $h / $dst_scale );
-
- $x = ($src_w - $w) / 2;
- $y = 0;
- }
- // ???
- $croped = imagecreatetruecolor( $w, $h );
- imagecopy( $croped, $source, 0, 0, $x, $y, $ src_w, $src_h );
- // ?? ??
- $scale = $option ['??'] / $w;
- $target = imagecreatetruecolor ( $option ['??'], $option ['?? '] );
- $ final_w = intval ( $w * $scale );
- $final_h = intval ( $h * $scale );
- imagecopyresampled ( $target, $croped, 0, 0, 0 , 0, $final_w, $final_h , $w, $h );
- imagedestroy ( $croped );
-
- //??(??) ???
- if (!empty ( $save_img ))
- {
-
- $org_funcs ['save_func'] ( $target, $save_img );
- } else
- {
- ??( $org_funcs ['header'] );
- $org_funcs ['save_func'] ( $target );
- }
- imagedestroy ( $target );
- return array ('flag' => True, 'msg' => '' );
- }
-
- /**
- *
- * ??? ???
- * @param $src_img ?? ???
- * @param $save_img ?? ??
- * @param $option ???? ?? array('width'= >xx ,'?'=>xxx)
- *
- */
- ?? resize_image($src_img, $save_img = '', $option)
- {
- $org_ext = $this-> ;is_img ( $src_img );
- if (! $org_ext ['flag'])
- {
- return $org_ext;
- }
-
- //?? ??? ?? ?? , ??? ???? ??
- if (!empty ( $save_img ))
- {
- $f = $this->check_dir ( $save_img );
- if (! $f [' flag'])
- {
- return $f;
- }
- }
-
- //?? ??? ????
- $org_funcs = $this->get_img_funcs ( $org_ext ['msg'] );
-
- //?? ?? ????
- $source = $org_funcs ['create_func'] ( $src_img );
- $src_w = Imagesx ( $source );
- $src_h = ??? ( $source );
-
- if (($option ['??'] && $src_w > $option ['??']) || ($option ['??'] && $src_h > $option ['??' ]))
- {
- if ($option ['??'] && $src_w > $option ['??'])
- {
- $widthratio = $option ['??'] / $src_w;
- $resizewidth_tag = true;
- }
-
- if ($option ['height'] && $src_h > $option [' height'])
- {
- $heightratio = $option ['height'] / $src_h;
- $resizeheight_tag = true;
- }
-
- if ($resizewidth_tag && $resizeheight_tag )
- {
- if ($ widthratio < $heightratio)
- $ratio = $widthratio;
- else
- $ratio = $heightratio;
- }
-
- if ($resizewidth_tag && ! $resizeheight_tag)
- $ratio = $widthratio;
- if ($resizeheight_tag && ! $resizewidth_tag)
- $ratio = $heightratio;
-
- $newwidth = $src_w * $ratio;
- $newheight = $src_h * $ratio;
-
- if (function_exists ( "imagecopyresampled" ))
- {
- $newim = imagecreatetruecolor ( $newwidth, $newheight );
- imagecopyresampled( $newim, $source, 0 , 0, 0, 0, $newwidth, $newheight, $src_w, $src_h );
- } else
- {
- $newim = imagecreate( $newwidth , $newheight );
- imagecopyreised ( $ newim, $source, 0, 0, 0, 0, $newwidth, $newheight, $src_w, $src_h );
- }
- }
- //?? ??(??)
- if (!empty ( $save_img ))
- {
-
- $org_funcs ['save_func'] ( $newim, $save_img );
- } else
- {
- ??( $org_funcs ['header'] );
- $org_funcs ['save_func']( $newim );
- }
- imagedestroy( $newim );
- ?? ?? ('flag' => True, 'msg' => '' );
- }
-
- /**
- *
- * ???? ??? ??
- * @param $org_img ?? ???
- * @param $mark_img ???? ???
- * @param $save_img ????? ???? ??? Create? ?????. ????
- * @param array $option ????? ?? ?? ??? ??? ????.
- * x: ????? ?? ??, ???? ???? ???? ??? ? ????.
- * y: ????? ?? ??, ???? ????? ??? ? ?
- * alpha: ?? ?(??? ??), ???? 50
- */
- ?? ?? water_mark($org_img, $mark_img, $save_img = '', $option = array())
- {
- //?? ??
- $org_ext = $this->is_img ( $org_img );
- if ( ! $org_ext ['???'])
- {
- return $org_ext;
- }
- $mark_ext = $this->is_img ( $mark_img );
- if (! $mark_ext [ ' flag'])
- {
- return $mark_ext;
- }
- //?? ??? ??? ??? ??? ??
- if (!empty ( $save_img ))
- {
- $f = $this->check_dir ( $save_img );
- if (! $f ['flag'])
- {
- return $f;
- }
- }
-
- //?? ??? ????
- $org_funcs = $this->get_img_funcs ( $org_ext ['msg'] );
- $org_img_im = $org_funcs ['create_func' ] ( $org_img ) ;
-
- $mark_funcs = $this->get_img_funcs ( $mark_ext ['msg'] );
- $mark_img_im = $mark_funcs ['create_func'] ( $mark_img );
-
- //???? ??? ?? ??
- $mark_img_im_x = 0;
- $mark_img_im_y = 0;
- //???? ??? ?? ? ?? ??
- $mark_img_w = ????imagesx ( $mark_img_im );
- $mark_img_h = ???( $mark_img_im );
-
- $org_img_w = ???????x( $org_img_im );
- $org_img_h = ???x( $org_img_im );
-
- / /?? ??? ??
- $x = $org_img_w - $mark_img_w;
- $org_img_im_x = isset ( $option ['x'] ) ? $option ['x'] : $x;
- $org_img_im_x = ($org_img_im_x > $org_img_w ?? $ org_img_im_x < 0) ? $x : $org_img_im_x;
- $y = $org_img_h - $mark_img_h;
- $org_img_im_y = isset ( $option ['y'] ) ? $option ['y'] : $ y;
- $org_img_im_y = ($org_img_im_y > $org_img_h ?? $org_img_im_y < 0) ? $y : $org_img_im_y;
-
- //??
- $alpha = isset ( $option [' alpha'] ) ? $option ['alpha'] : 50;
- $alpha = ($alpha > 100 ?? $alpha < 50) : $alpha;
-
- //??? ??
- imagecopymerge ( $org_img_im, $mark_img_im, $org_img_im_x, $org_img_im_y, $mark_img_im_x, $mark_img_im_y, $mark_img_w, $mark_img_h, $alpha );
-
- / /??(??) ???
- if (!empty ( $save_img ))
- {
-
- $org_funcs ['save_func'] ( $org_img_im, $save_img );
- } else
- {
- ??( $org_funcs [ 'header'] );
- $org_funcs ['save_func']( $org_img_im );
- }
- //??? ??
- imagedestroy( $org_img_im );
- imagedestroy ( $mark_img_im );
- return array ('flag' => True, 'msg' => '' );
-
- }
-
- /* *
- *
- * ??? ??
- * @paramknown_type $img_path
- * @return array('flag'=>true/false,'msg'=>ext/error message)
- */
- ??? ?? is_img($img_path)
- {
- if (!file_exists ( $img_path ))
- {
- ?? ?? ('flag' => False, 'msg' => "$img_path ??? ?? ??! " );
- }
- $ext = ??( '.', $img_path );
- $ext = strtolower( end( $ext ) );
- if(!in_array( $ext, $ this->exts ))
- {
- return array ('flag' => False, 'msg' => "$img_path ???? ??? ???????!" );
- }
- ?? ?? ('flag' => True, 'msg' => $ext );
- }
-
- /**
- *
- * ??? ??? ??? ?????
- * @paramknown_type $ext
- */
- ??? ?? get_img_funcs($ext)
- {
- //選擇
- ???($ext)
- {
- case 'jpg' :
- $header = 'Content-Type: image/jpeg';
- $createfunc = 'imagecreatefromjpeg';
- $savefunc = 'imagejpeg';
- break;
- case 'jpeg' :
- $header = 'Content-Type:image /jpeg';
- $createfunc = 'imagecreatefromjpeg';
- $savefunc = 'imagejpeg';
- break;
- case 'gif' :
- $header = 'Content-Type:image/ gif';
- $createfunc = 'imagecreatefromgif';
- $savefunc = 'imagegif';
- break;
- case 'bmp' :
- $header = '??? ??:???/bmp ';
- $createfunc = 'imagecreatefrombmp';
- $savefunc = 'imagebmp';
- break;
- ???:
- $header = 'Content-Type:image/png';
- $createfunc = 'imagecreatefrompng';
- $savefunc = 'imagepng';
- }
- ?? ??('save_func' => $savefunc, 'create_func' => $createfunc, 'header' = > $header );
- }
-
- /**
- *
- * ????? ???? ??? ???.
- * @param $save_img
- */
- ??? ?? check_dir($save_img)
- {
- $dir = dirname ( $save_img );
- ??(! is_dir ( $dir ))
- {
- if (! mkdir ( $dir, 0777, true ))
- {
- return array ('flag' => False, 'msg' => "圖文保存目錄 $dir 無法創(chuàng)建!" );
- }
- }
- ?? ?? ('flag' => True, 'msg' => '' );
- }
- }
-
- if (! ?? ?? ( $_FILES ['test'] ['tmp_name'] ))
- {
- //例子
- $img = new Img ();
- //??
- $name = ??( '.', $_FILES ['test'] ['name'] );
- $org_img = 'D:/test.' . end ( $name );
- move_uploaded_file ( $_FILES ['test'] ['tmp_name'], $org_img );
- $option = array ('width' => $_POST ['width'], '??' => $_POST ['??'] );
- if ($_POST ['type'] == 1)
- {
- $s = $img->resize_image ( $org_img , '', $option );
- } else
- {
- $img->thumb_img( $org_img, '', $option );
- }
- ?? ??( $org_img );
- }
-
-
復代碼
?? ??:
水印
- $img = ? Img();
- $org_img = 'D:/tt.png';
- $mark_img = 'D:/t .png';
- //保存水印圖 Images(如果$save_img為空時,將會直接輸圖文)
- $save_img = 'D:/test99h/testone/sss.png';
- //水印設(shè)置調(diào)節(jié)
- $option = array ('x' => 50, 'y' => 50, 'alpha' => 80 );
- //生成水印圖文
- $flag = $img->water_mark ( $org_img, $mark_img, $save_img, $option );
-
-
復代碼
當我們調(diào)節(jié) $option 參數(shù)時,會有數(shù)變化:
1 $option = ??('x' => 0, 'y' => 0, '??' => 50 );
2$option = ??('x' => 50, 'y' => 50, '??' => 80 );
3 如果你不設(shè)置$option 參數(shù),將會采用默認值:
如果要純文字式的水印,可以參看這里:http://www.php.net/manual/zh/image.examples.merged-watermark.php
- //例子
- $img = ? ???();
- $org_img = 'D:/tt.png';
- //壓縮圖picture(100*100)
- $option = array ('width' => 100, 'height' => 100 );
-
- //$save_img為空時,將會直接輸?圖???到瀏覽器
-
- $save_img = 'D:/test99h/testone/sss_thumb.png';
- $flag = $img->thumb_img ( $org_img, $save_img, $option );
-
-
復代碼
調(diào)節(jié)$??? ? ??:
- $option = ??('??' => 200, '??' => 200);
-
-
???碼
水印與壓縮圖
- $img = new Img ();
- //hara圖
- $org_img = 'D:/tt.png';
- //水印標記圖
- $mark_img = 'D:/t.png';
- //保存水印圖文
- $save_img = 'D:/test99h/testone/sss.png';
- //水印設(shè)置調(diào)節(jié)
- $option = array ('x' => 50, 'y' => 50, 'alpha' => 60 );
- //生成水印圖文
- $flag = $img->water_mark ( $org_img, $mark_img, $save_img, $option );
- //壓縮水印圖文
- $option = array ('??' => 200, '??' => 200 );
- //保存壓縮圖
- $save_img2 = 'D:/test99h/testone/sss2_thumb.png';
- $flag = $img->thumb_img ( $save_img, $save_img2, $ ?? ); //等比例壓縮類似
復代碼
??? ???? ???? ??? ?, ?? ? ??? ???? ??? ?? ??? ? ???? ???? ???? ???. ??? ??? ? ? ?? ??? ?????.
?? ??: ??? ??? ??? ?? ??? ?? ????.
|