- /**
- * 來源:網際網路
- */
- class image
- {
- var $w_pct = 100; //> var $w_quality = 100; //品質
- var $w_minwidth = 500; //最小寬
- var $w_minheight = 500; //最小高
- var $interlace = 0; //影像是否為隔行掃描的
- var $fontfile ; //字型檔
- var $w_img ; //預設浮水印圖
-
- function __construct()
- {
- $this->fontfile = $_SERVERVER ['DOCUMENT_ROOT'].'/public/fonts/simhei.ttf';
- $this->w_img = '';
- }
- function image()
- {
- $this- >__construct();
- }
-
- function info($img)
- {
- $imageinfo = getimagesize($img); //傳回影像資訊陣列0=>寬的像素1 =>高的像素2=>是圖像類型的標記3 =>是文字字串,內容為“height="yyy" width="xxx"”,
- if($imageinfo === false) return false ;
- $imagetype = strtolower(substr(image_type_to_extension($imageinfo[2]),1)); //取得映像檔類型$imageinfo[2]是映像類型的標記
- $imagesize = filesize($img ); //圖片大小
- $info = array(
- 'width'=>$imageinfo[0],
- 'height'=>$imageinfo[1],
- 'type'=> $imagetype,
- 'size'=>$imagesize,
- 'mime'=>$imageinfo['mime']
- );
- return $info;
- }
-
- /**
- * 追蹤
- *
- * @param string $image
- * @param string $filename
- * @param int $maxwidth
- * @param int $maxheight
- * param string $suffix
- * @param int $autocut
- * @return string
- */
- function thumb($image, $filename = '',$maxwidth = 50, $maxheight = 50, $suffix='_thumb', $autocut = 0)
- {
- if( !$this->check($image)) return false;
- $info = $this->info($image); //取得圖片資訊
- if($info === false) return false;
- $srcwidth = $info['width']; //源圖寬
- $srcheight = $info['height']; //源圖高
- $pathinfo = pathinfo ($image);
- $type = $pathinfo['extension']; //取得副檔名
- if(!$type) $type = $info['type']; //若沒有取到,用$info['type']
- $type = strtolower($type);
- unset($info);
- $scale = min($maxwidth/$srcwidth, $maxheight/$srcheight) ; //取得縮略比例
- //取得依照來源圖的比列
- $createwidth = $width = (int) ($srcwidth*$scale);//取得縮寬();
- $createheight = $height = (int)($srcheight*$scale);//(); //取得縮略高
- $psrc_x = $psrc_y = 0;
- if($autocut) //依照縮圖的比例來取得
- {
- if($maxwidth/$maxheight=$height) //如果縮圖按比列比來源圖窄的話
- {
- $width = $maxheight/$height*$width; //寬依對應比例做處理
- $height = $maxheight;// //高不變
- }
- elseif( $maxwidth/$maxheight>$srcwidth/$srcheight && $maxwidth>=$width)//如果縮圖按比列比源圖寬的話
- {
- $height = $maxwidth/$width*$height ;//
- $width = $maxwidth;
- }
- if($maxwidth == '55'){
- $width = '55';
- $createwidth = '55';
- }
- if($maxwidth == '110'){
- $width = '110';
- $createwidth = '110';
- }
-
- }
- }
- if($srcwidth $createwidth= $srcwidth;
- $createheight = $srcheight;
- }
- $createheight = $srcheight;
- }
- }
- $create imagecreatefrom'.($type=='jpg' ? 'jpeg' : $type); //找到不同的圖片處理函數
- $srcimg = $createfun($image); //回傳圖片識別碼 if($type != 'gif' && function_exists('imagecreatetruecolor')){ $thumbimg = imagecreatetruecolor($createwidth, $createheight); //新建一個真彩色圖像 }else{
- if($maxwidth == '110' && $srcheight >= $srcwidth){
- $height1 = 72;
- $thumbimg = imagecreate($width, $height1); //調色盤為基礎的圖片
- }elseif ($maxwidth == '110' && $srcheight $width1 = 110;
- $thumbimg = imagecreate($width1, $height) ; //新建一個基於調色盤的圖片
- }else{
- $thumbimg = imagecreate($width, $height); //新建一個基於調色盤的圖片
- }
- }
- if(function_exists('imagecopyresampled')){ //重採樣拷貝部分影像並調整大小,真彩
- //imagecopyresampled(新圖,來源圖,新圖左上角x距離,新圖左上角y距離,源圖左上角x距離,源圖左上角y距離,新圖寬,新圖高,源圖寬,源圖高)
- if($maxwidth == '110' && $srcheight >= $srcwidth){
- $psrc_x = 0;
- $psrc_yz = $height/2;
- $psrc_y = $psrc_yz;
- $width = '110';
- }
- }
- if ($maxwidth == '110' && $srcheight >= $srcwidth && $srcheight>= '72' && $srcwidth $psrc_x = 0;
- $psrc_yz = $height/yz = $height/ 2;
- $psrc_y = $psrc_yz;
- $width = '110';
- }
- if($maxheight == '72' && $srcheight = '110'){
- $cha = ($maxheight-$srcheight)/2;
- $psrc_y = -($cha);
- $width = $srcwidth;
- $height = '72';
- $srcheight = '72';
- }
- imagecopyresampled($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $width, $height, $. $srcwidth, $srcheight);
-
- }else{ //拷貝部分圖像並調整大小,調色板
- imagecopyresized($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $ width, $height, $srcwidth, $srcheight);
- }
- if($type=='gif' || $type=='png')
- {
- //imagecolorallocate 為一幅圖片分配顏色
- $background_color = imagecolorallocate($thumbimg, 0, 255, 0); // 為基於調色盤的圖片填滿背景色, 指派一個綠色
- // imagecolortransparent 將某個顏色定義為透明色
- imagecolortransparent($thumbimg, $background_color); // 設定為透明色,若註解掉該行則輸出綠色的圖
- }
- // imageinterlace 啟動或禁止隔行掃描
- if ($type=='jpg' || $type=='jpeg') imageinterlace($thumbimg, $this->interlace);
- $imagefun = 'image'.($type=='jpg' ? ' jpeg' : $type);
- //imagejpeg imagegif imagepng
- if(empty($filename)) $filename = substr($image, 0, strrpos($image, '.')).$suffix. '.'.$type; //取得檔案名稱
- //aaa.gif aaa_thumb.gif
- $imagefun($thumbimg, $filename); //新圖片
- imagedestroy($thumbimg); / /銷毀縮圖
- imagedestroy($srcimg); //銷毀來源圖
- return $filename;
- }
- /**
- * 限制寬或高
- *
- * @param string $image
- * @param int $maxwidth
- * @param int $maxheight
- * @param string $suffix> * @param int $maxheight
- * @param string $suffix > * @return string
- */
- function thumb3($image,$ maxwidth = 0, $maxheight = 0, $suffix='_thumb')
- {
- if( !$this->check($image)) return false;
- $info = $this->info ($image); //取得圖片資訊
- if($info === false) return false;
- $srcwidth = $info['width']; //來源圖寬
- $srcheight = $info['height']; //來源圖高
- $pathinfo = pathinfo($image);
- $type = $pathinfo['extension']; //取得副檔名
- if(! $type) $type = $info['type']; //如果沒有取到,用$info['type']
- $type = strtolower($type);
- unset($info);
- if($maxwidth==0){
- $scale = $maxheight/$srcheight;
- if($scale $createwidth = $srcwidth*$scale;
- $createheight = $maxheight;
- }else{
- $createwidth = $srcwidth;
- $createheight = $srcheight; }
- }
- if($maxheight==0){
- $scale = $maxwidth/$srcwidth;
- if($scale $createwidth = $maxwidth;
- $createheight = $srcheight*$scale;
- }else{
- $createwidth = $srcwidth;
- $createheight = $srcheight;
- }
- }
- $ps = 0;
- $createfun = 'imagecreatefrom'.($type=='jpg' ? 'jpeg' : $type); //找到不同的映像處理函數
- $srcimg = $createfun($image) ; //回傳圖片識別碼
- if($type != 'gif' && function_exists('imagecreatetruecolor')){
- $thumbimg = imagecreatetruecolor($createwidth, $createheight); //新建一個真彩色圖像
- }else{
- $thumbimg = imagecreate($createwidth, $createheight); //新建一個基於調色盤的圖片
- }
- if(function_exists('imagecopyresampled')){ //重採樣拷貝部分影像並調整大小,真彩
- //imagecopyresampled(新圖,源圖,新圖左上角x距離,新圖左上角y距離,源圖左上角x距離,源圖左上角y距離,新圖寬,新圖高,源圖寬,源圖高)
- imagecopyresampled($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $createwidth, $createheight, $srcwidth, $srcheightidth, $srcheight );
-
- }else{ //拷貝部分圖像並調整大小,調色板
- imagecopyresized($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $createwidth, $createheight, $createheight, $srcwidth, $srcheight);
- }
- if($type=='gif' || $type=='png')
- {
- //imagecolorallocate 為一幅圖像分配顏色
- $background_color = imagecolorallocate($thumbimg, 0, 255, 0); // 為基於調色盤的圖片填滿背景色, 指派一個綠色
- // imagecolortransparent 將某個顏色定義為透明色
- imagecolortransparent($thumbimg, $background_color); // 設定為透明色,若註解掉該行則輸出綠色的圖
- }
- // imageinterlace 啟動或禁止隔行掃描
- if($type== 'jpg' || $type=='jpeg') imageinterlace($thumbimg, $this->interlace);
- $imagefun = 'image'.($type=='jpg' ? 'jpeg' : $type );
- //imagejpeg imagegif imagepng
- $filename = substr($image, 0, strrpos($image, '.')).$suffix.'.'.$type; //取得檔案名稱
- //aaa.gif aaa_thumb.gif
- $imagefun($thumbimg, $filename); //新圖片
- imagedestroy($thumbimg); //銷毀縮圖
- imagedestroy($thumbimg); //銷毀縮圖
- imagedestroy($srcimg); //銷毀來源圖
- return $filename;
- }
- /**
- * 產生特定尺寸縮圖解決原版縮圖無法滿足特定尺寸的問題PS:會裁掉圖片不符合縮圖比例的部分
- * @static
- * @access public
- * @ ?
- * @param boolean $interlace 啟用隔行掃描
- * @return void
- */
- static function thumb2($image, $thumbname, $type='', $maxWidth=200 , $maxHeight=50, $interlace=true) {
- // 取得原圖資訊
- $info = Image::getImageInfo($image);
- if ($info !== false) {
- $srcWidth = $info['width'];
- $srcHeight = $info['height'];
- $type = empty($type) ? $info['type'] : $type;
- $type = strtolower($type);
- $interlace = $interlace ? 1 : 0;
- unset($info);
- $scale = max($maxWidth / $srcWidth, $maxHeight / $srcHeight); // 計算縮放比例
- //判斷原圖和縮圖比例如原圖寬於縮圖則裁掉兩邊反之..
- if($maxWidth / $srcWidth > $maxHeight / $srcHeight){
- //高於
- $srcX = 0;
- $srcY = ($srcHeight - $maxHeight / $scale) / 2 ;
- $cutWidth = $srcWidth;
- ;
- $cutHeight = $maxHeight / $scale;
- }else{
- //寬於
- $srcX = ($srcWidth - $maxWidth / $scale) / 2;
- $srcY = 0;
- $cutWidth = $maxWidth / $scale;
- $cutHeight = $srcHeight;
- }
-
- // 載入原圖
- $createFun = 'ImageCreateFrom' . ($type == 'jpg' ? 'jpeg' : $type);
- $srcImg = $createFun($image) ;
-
- //建立錢包
- if ($type != 'gif' && function_exists('imagecreatetruecolor'))
- $thumbImg = imagecreatetruecolor($maxWidth, $maxHeight);
- $thumbImg = imagecreate($maxWidth, $maxHeight);
-
- // 複製圖片
- if (function_exists("ImageCopyResampled"))
imagecoresppmg,mhmg(pybmg,$pmg,$p,m, 0, $srcX, $srcY, $maxWidth, $maxHeight, $切割寬度,$切割高度);
- else
- imagecopyresized($thumbImg, $srcImg, 0, 0, $srcX, $srcY, $maxsrcth , $maxHeight, $cutWidth, $cutHeight);
- if ('gif' == $type || 'png' == $type) {
- //imagealphablending($thumbImg, false);//取消預設的混色模式
- //imagesavealpha( $thumbImg,true);//設定保存完整的alpha 通道資訊
- $background_color = imagecolorallocate($thumbImg, 0, 255, 0); // 指派一個綠色
- imagecolortransparent($thumbImg, $background_color); // 設定為透明色,若註解掉該行則輸出綠色的圖
- }
-
- // 對jpeg圖形設定隔行掃描
- if ('jpg' == $type || ' jpeg' == $type)
- imageinterlace($thumbImg, $interlace);
-
- // 產生圖片
- $imageFun = 'image' . ($type == 'jpg' ? 'jpeg' : $type);
- @chmod(dirname($thumbname),0777);
- $imageFun($thumbImg, $thumbname);
- imageFun($thumbImg, $thumbname);
- imagedestroy( $thumbImg);
- imagedestroy($srcImg);
- 回傳$thumbname;
- }
- 回傳false;
- }
-
-
- /**
- /**
- /** /
-
- static function getImageInfo($img) {
- $imageInfo = getimagesize($img);
- if ($imageInfo !== false) {
- $imageType = strtolower(subsstr image_type_to_extension($imageInfo[2]), 1));
- $imageSize = 檔案大小($img);
- $info = array(
- "寬度" => $imageInfo[0],
- "高度" => $imageInfo[1],
- "型" => $imageType,
- "大小" => $imageSize,
- "mime" => $imageInfo['mime']
- );
- 回傳$info;
- } else {
- 回傳false;
- }
- }
-
- //watermark(來源圖,產生檔案,產生位置,水印檔,水印文字,背景色)
- function watermark($source, $target = '' , $w_pos = 9, $w_img = '', $w_text = '', $w_font = 12, $w_color = ' #cccccc',$x='',$y='')
- {
- if(!$this->check($source)) 回傳false;
- if(!$target) $target = $source;
- if ($w_img == '' && $w_text == '')
- $w_img = $this->w_img;
- $source_info = getimagesize($source);
- $source_w = $source_info[0]; //取得寬
- $source_h = $source_info[1]; //取得高
- if($source_w w_minwidth || $source_h w_minheight) return false; // 寬與達不到要求直接回傳
- switch($source_info[2]) // 新圖
- {
- case 1 :
- $source_img = imagecreatefromgif($ source);
- 休息;
- 案例2 :
- $source_img = imagecreatefromjpeg($source);
- 休息;
- 案例3 :
- $source_img = imagecreatefrompng($source);
- 休息;
- 預設:
- 回傳false;
- }
- if(!empty($w_img) && file_exists($w_img)) //水印檔案
- {
- $ifwaterimage = 1; // 是否浮水印圖
- $water_info = getimaimaize($w_imgesg) ; //浮水印資訊
- $width = $water_info[0];
- $height = $water_info[1];
-
- switch($water_info[2])
- {
- 情況1 :
- $water_img = imagecreatefromgif($w_img);
-
- 休息;
- 案例2 :
- $water_img = imagecreatefromjpeg($ 3 :
- $water_img = imagecreatefrompng($w_img);
-
- 休息;
- 預設:
- 回傳;
- }
- }
- else $ifwaterimage = 0;
- //imagettfbbox本函數計算並傳回一個包圍TrueType文字範圍的虛擬方框的像素大小。
- //imagettfbbox ( 字體大小, 字體角度, 字體檔案,檔案)
- // echo $this->fontfile;
- $temp = imagettfbbox(ceil($w_font*1.6), 0, $this ->fontfile, $w_text);//取得使用truetype 字體的文字的範圍
- $width = $temp[4] - $temp[6]; //右上角X 位置- 左上角X 位置
- $height = $temp[3] - $temp[5]; //右下角Y 位置- 右上角Y 位置
- unset($temp);
- }
- switch($w_pos)
- {
- case 0: //隨機位置
- $wx = rand(0,($source_w - $width));
- $wy = rand(0,($source_h - $height));
- break;
- case 1: //左上角
- $wx = 25;
- $wy = 25;
- break;
- case 2: //上面中間位置
- $ wx = ($source_w - $width) / 2;
- $wy = 0;
- break;
- case 3: //右上角
- $wx = $source_w - $width;
- $wy = 0;
- break;
- case 4: //左邊中間位置
- $wx = 0;
- $wy = ($source_h - $height) / 2;
- break;
- case 5: //中間位置
- $wx = ($source_w - $width) / 2;
- $wy = ($source_h - $height) / 2;
- break;
- case 6: //底部中間位置
- $wx = ($source_w - $width) / 2;
- $wy = $source_h - $height;
- break;
- case 7: //左下角
- $wx = 0;
- $wy = $source_h - $height;
- break;
- case 8: //右邊中間位置
- $wx = $source_w - $width;
- $wy = ($source_h - $height) /2;
- break;
- case 9: //右下角
- $wx = $source_w - $width;
- $wy = $source_h - $height ;
- break;
- default: //隨機
- $wx = rand(0,($source_w - $width));
- $wy = rand(0,($source_h - $height));
- break;
- case 10://自訂位置
- $wx = $x;
- $wy = $y;
- case 11: //底部中間位置--手工客專用
- $wx = ($source_w - $width) / 2;
- $wy = $source_h - $height-50;
- break;
- break;
- }
- if($ifwaterimage) //如果有浮水印圖
- {
-
- //imagecopymerge 拷貝並合併影像的一部份
- //參數(源圖,水印圖,拷貝到源圖x位置,拷貝到來源圖y位置,從水印圖x位置,從水印圖y位置,高,寬,透明度)
- //imagecopymerge($source_img, $water_img, $wx, $wy, 0, 0, $width, $height, $this->w_pct);
-
- imagecopy($source_img,$water_img,$wx,$wy,0,0,$width,$height);
- }
- else
- {
- if(!empty($w_color) && (strlen($w_color)==7))
- {
- $r = hexdec(substr($w_color,1,2) ); //取得紅色
- $g = hexdec(substr($w_color,3,2)); //取得綠色
- $b = hexdec(substr($w_color,5)); //取得藍色
- }
- else
- {
- return;
- }
- //imagecolorallocate 基於調色盤的圖片填滿背景色
- //imagestring 水平畫一行字串
- //imagestring(來源圖,字體大小,位置X,位置Y,文字,顏色)
- //參數($image, float $size, float $angle, int $x, int $y, int $ color, string $fontfile, string $text)
- imagettftext($source_img,$w_font,0,$wx,$wy,imagecolorallocate($source_img,$r,$g,$b),$this->fontfile, $w_text);
- //imagestring($source_img,$w_font,$wx,$wy,$w_text,imagecolorallocate($source_img,$r,$g,$b));
- }
-
- //輸出到檔案或瀏覽器
- switch($source_info[2])
- {
- case 1 :
- imagegif($source_img, $target); //以GIF 格式將影像輸出至瀏覽器或檔案
- break;
- case 2 :
- imagejpeg($source_img, $target, $this->w_quality); //以JPEG 格式將影像輸出到瀏覽器或檔案
- break;
- case 3 :
- imagepng($source_img, $target); //以PNG 格式將圖片輸出到瀏覽器或檔案
- break;
- default :
- return;
- }
- if(isset($water_info))
- {
- unset($water_info); //銷毀
- }
- if(isset($water_img))
- {
- imagedestroy($water_img); //銷毀
- }
- unset($source_info);
- imagedestroy($source_img);
- return true; //gd庫必須存在,後綴為jpg|jpeg|gif|png,文件存在,imagecreatefromjpeg或者imagecreatefromgif存在
- function check($image)
- {
- return extension_loaded('gd') &&
- preg_match("/.(JPG|JPEG|PNG|GIF|jpg|jpeg|gif|png)/i", $image, $m) &&
- file_exists($image) &&
- function_exists('imagecreatefrom '.($m[1] == 'jpg' ? 'jpeg' : $m[1]));
- //imagecreatefromjpeg
- //imagecreatefromgif
- //imagecreatefrompng
- }
- }
-
- /**
- 縮圖
- 1.新建一個圖像資源通過imagecreatefromgif imagecreatefromjpeg imagecreatefrompng
- 2.imagecopyresampled 拷貝圖片,並調整大小
-
- 水印:圖片
- 2.加浮水印
- 圖片浮水印:imagecopymerge 把2張圖合併在一起
- 文字浮水印:imagettftext 寫入文字
-
-
- */
- error_reporting(0);
- $hostdir = dirname(__FILE__);
- $filesnames = scandir($hostdir.'/desktop' );
- $img = new image();
- foreach($filesnames as $name){
- if($name != '.' && $name != '..'){
- $imgPath = $hostdir.'/desktop/';
- $imgsize = getimagesize($imgPath.$name);
- $imgInfo = explode('.',$name);
- $imginfo2 = explode('_',$imgInfo[0]);
- if($imginfo2[count($imginfo2)-1] != 'small'){
- $img_small = $imgPath.$imgInfo[0] .'_small.'.$imgInfo[1];
- $img->thumb2($imgPath.$name,$img_small,'',$imgsize[0]/4,$imgsize[1]/4);
- }
- }
- }
-
-
- 複製程式碼
|
等比例, PHP