首頁 >後端開發 >php教程 >PHP資料夾遍歷,圖片等比例壓縮

PHP資料夾遍歷,圖片等比例壓縮

WBOY
WBOY原創
2016-07-25 08:42:541209瀏覽
等比例, PHP
  1. /**
  2. * 來源:網際網路
  3. */
  4. class image
  5. {
  6. var $w_pct = 100; //> var $w_quality = 100; //品質
  7. var $w_minwidth = 500; //最小寬
  8. var $w_minheight = 500; //最小高
  9. var $interlace = 0; //影像是否為隔行掃描的
  10. var $fontfile ; //字型檔
  11. var $w_img ; //預設浮水印圖
  12. function __construct()
  13. {
  14. $this->fontfile = $_SERVERVER ['DOCUMENT_ROOT'].'/public/fonts/simhei.ttf';
  15. $this->w_img = '';
  16. }
  17. function image()
  18. {
  19. $this- >__construct();
  20. }
  21. function info($img)
  22. {
  23. $imageinfo = getimagesize($img); //傳回影像資訊陣列0=>寬的像素1 =>高的像素2=>是圖像類型的標記3 =>是文字字串,內容為“height="yyy" width="xxx"”,
  24. if($imageinfo === false) return false ;
  25. $imagetype = strtolower(substr(image_type_to_extension($imageinfo[2]),1)); //取得映像檔類型$imageinfo[2]是映像類型的標記
  26. $imagesize = filesize($img ); //圖片大小
  27. $info = array(
  28. 'width'=>$imageinfo[0],
  29. 'height'=>$imageinfo[1],
  30. 'type'=> $imagetype,
  31. 'size'=>$imagesize,
  32. 'mime'=>$imageinfo['mime']
  33. );
  34. return $info;
  35. }
  36. /**
  37. * 追蹤
  38. *
  39. * @param string $image
  40. * @param string $filename
  41. * @param int $maxwidth
  42. * @param int $maxheight
  43. * param string $suffix
  44. * @param int $autocut
  45. * @return string
  46. */
  47. function thumb($image, $filename = '',$maxwidth = 50, $maxheight = 50, $suffix='_thumb', $autocut = 0)
  48. {
  49. if( !$this->check($image)) return false;
  50. $info = $this->info($image); //取得圖片資訊
  51. if($info === false) return false;
  52. $srcwidth = $info['width']; //源圖寬
  53. $srcheight = $info['height']; //源圖高
  54. $pathinfo = pathinfo ($image);
  55. $type = $pathinfo['extension']; //取得副檔名
  56. if(!$type) $type = $info['type']; //若沒有取到,用$info['type']
  57. $type = strtolower($type);
  58. unset($info);
  59. $scale = min($maxwidth/$srcwidth, $maxheight/$srcheight) ; //取得縮略比例
  60. //取得依照來源圖的比列
  61. $createwidth = $width = (int) ($srcwidth*$scale);//取得縮寬();
  62. $createheight = $height = (int)($srcheight*$scale);//(); //取得縮略高
  63. $psrc_x = $psrc_y = 0;
  64. if($autocut) //依照縮圖的比例來取得
  65. {
  66. if($maxwidth/$maxheight=$height) //如果縮圖按比列比來源圖窄的話
  67. {
  68. $width = $maxheight/$height*$width; //寬依對應比例做處理
  69. $height = $maxheight;// //高不變
  70. }
  71. elseif( $maxwidth/$maxheight>$srcwidth/$srcheight && $maxwidth>=$width)//如果縮圖按比列比源圖寬的話
  72. {
  73. $height = $maxwidth/$width*$height ;//
  74. $width = $maxwidth;
  75. }
  76. if($maxwidth == '55'){
  77. $width = '55';
  78. $createwidth = '55';
  79. }
  80. if($maxwidth == '110'){
  81. $width = '110';
  82. $createwidth = '110';
  83. }
  84. }
  85. }
  86. if($srcwidth $createwidth= $srcwidth;
  87. $createheight = $srcheight;
  88. }
  89. $createheight = $srcheight;
  90. }
  91. }
  92. $create imagecreatefrom'.($type=='jpg' ? 'jpeg' : $type); //找到不同的圖片處理函數
  93. $srcimg = $createfun($image); //回傳圖片識別碼 if($type != 'gif' && function_exists('imagecreatetruecolor')){ $thumbimg = imagecreatetruecolor($createwidth, $createheight); //新建一個真彩色圖像 }else{
  94. if($maxwidth == '110' && $srcheight >= $srcwidth){
  95. $height1 = 72;
  96. $thumbimg = imagecreate($width, $height1); //調色盤為基礎的圖片
  97. }elseif ($maxwidth == '110' && $srcheight $width1 = 110;
  98. $thumbimg = imagecreate($width1, $height) ; //新建一個基於調色盤的圖片
  99. }else{
  100. $thumbimg = imagecreate($width, $height); //新建一個基於調色盤的圖片
  101. }
  102. }
  103. if(function_exists('imagecopyresampled')){ //重採樣拷貝部分影像並調整大小,真彩
  104. //imagecopyresampled(新圖,來源圖,新圖左上角x距離,新圖左上角y距離,源圖左上角x距離,源圖左上角y距離,新圖寬,新圖高,源圖寬,源圖高)
  105. if($maxwidth == '110' && $srcheight >= $srcwidth){
  106. $psrc_x = 0;
  107. $psrc_yz = $height/2;
  108. $psrc_y = $psrc_yz;
  109. $width = '110';
  110. }
  111. }
  112. if ($maxwidth == '110' && $srcheight >= $srcwidth && $srcheight>= '72' && $srcwidth $psrc_x = 0;
  113. $psrc_yz = $height/yz = $height/ 2;
  114. $psrc_y = $psrc_yz;
  115. $width = '110';
  116. }
  117. if($maxheight == '72' && $srcheight = '110'){
  118. $cha = ($maxheight-$srcheight)/2;
  119. $psrc_y = -($cha);
  120. $width = $srcwidth;
  121. $height = '72';
  122. $srcheight = '72';
  123. }
  124. imagecopyresampled($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $width, $height, $. $srcwidth, $srcheight);
  125. }else{ //拷貝部分圖像並調整大小,調色板
  126. imagecopyresized($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $ width, $height, $srcwidth, $srcheight);
  127. }
  128. if($type=='gif' || $type=='png')
  129. {
  130. //imagecolorallocate 為一幅圖片分配顏色
  131. $background_color = imagecolorallocate($thumbimg, 0, 255, 0); // 為基於調色盤的圖片填滿背景色, 指派一個綠色
  132. // imagecolortransparent 將某個顏色定義為透明色
  133. imagecolortransparent($thumbimg, $background_color); // 設定為透明色,若註解掉該行則輸出綠色的圖
  134. }
  135. // imageinterlace 啟動或禁止隔行掃描
  136. if ($type=='jpg' || $type=='jpeg') imageinterlace($thumbimg, $this->interlace);
  137. $imagefun = 'image'.($type=='jpg' ? ' jpeg' : $type);
  138. //imagejpeg imagegif imagepng
  139. if(empty($filename)) $filename = substr($image, 0, strrpos($image, '.')).$suffix. '.'.$type; //取得檔案名稱
  140. //aaa.gif aaa_thumb.gif
  141. $imagefun($thumbimg, $filename); //新圖片
  142. imagedestroy($thumbimg); / /銷毀縮圖
  143. imagedestroy($srcimg); //銷毀來源圖
  144. return $filename;
  145. }
  146. /**
  147. * 限制寬或高
  148. *
  149. * @param string $image
  150. * @param int $maxwidth
  151. * @param int $maxheight
  152. * @param string $suffix> * @param int $maxheight
  153. * @param string $suffix > * @return string
  154. */
  155. function thumb3($image,$ maxwidth = 0, $maxheight = 0, $suffix='_thumb')
  156. {
  157. if( !$this->check($image)) return false;
  158. $info = $this->info ($image); //取得圖片資訊
  159. if($info === false) return false;
  160. $srcwidth = $info['width']; //來源圖寬
  161. $srcheight = $info['height']; //來源圖高
  162. $pathinfo = pathinfo($image);
  163. $type = $pathinfo['extension']; //取得副檔名
  164. if(! $type) $type = $info['type']; //如果沒有取到,用$info['type']
  165. $type = strtolower($type);
  166. unset($info);
  167. if($maxwidth==0){
  168. $scale = $maxheight/$srcheight;
  169. if($scale $createwidth = $srcwidth*$scale;
  170. $createheight = $maxheight;
  171. }else{
  172. $createwidth = $srcwidth;
  173. $createheight = $srcheight; }
  174. }
  175. if($maxheight==0){
  176. $scale = $maxwidth/$srcwidth;
  177. if($scale $createwidth = $maxwidth;
  178. $createheight = $srcheight*$scale;
  179. }else{
  180. $createwidth = $srcwidth;
  181. $createheight = $srcheight;
  182. }
  183. }
  184. $ps = 0;
  185. $createfun = 'imagecreatefrom'.($type=='jpg' ? 'jpeg' : $type); //找到不同的映像處理函數
  186. $srcimg = $createfun($image) ; //回傳圖片識別碼
  187. if($type != 'gif' && function_exists('imagecreatetruecolor')){
  188. $thumbimg = imagecreatetruecolor($createwidth, $createheight); //新建一個真彩色圖像
  189. }else{
  190. $thumbimg = imagecreate($createwidth, $createheight); //新建一個基於調色盤的圖片
  191. }
  192. if(function_exists('imagecopyresampled')){ //重採樣拷貝部分影像並調整大小,真彩
  193. //imagecopyresampled(新圖,源圖,新圖左上角x距離,新圖左上角y距離,源圖左上角x距離,源圖左上角y距離,新圖寬,新圖高,源圖寬,源圖高)
  194. imagecopyresampled($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $createwidth, $createheight, $srcwidth, $srcheightidth, $srcheight );
  195. }else{ //拷貝部分圖像並調整大小,調色板
  196. imagecopyresized($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $createwidth, $createheight, $createheight, $srcwidth, $srcheight);
  197. }
  198. if($type=='gif' || $type=='png')
  199. {
  200. //imagecolorallocate 為一幅圖像分配顏色
  201. $background_color = imagecolorallocate($thumbimg, 0, 255, 0); // 為基於調色盤的圖片填滿背景色, 指派一個綠色
  202. // imagecolortransparent 將某個顏色定義為透明色
  203. imagecolortransparent($thumbimg, $background_color); // 設定為透明色,若註解掉該行則輸出綠色的圖
  204. }
  205. // imageinterlace 啟動或禁止隔行掃描
  206. if($type== 'jpg' || $type=='jpeg') imageinterlace($thumbimg, $this->interlace);
  207. $imagefun = 'image'.($type=='jpg' ? 'jpeg' : $type );
  208. //imagejpeg imagegif imagepng
  209. $filename = substr($image, 0, strrpos($image, '.')).$suffix.'.'.$type; //取得檔案名稱
  210. //aaa.gif aaa_thumb.gif
  211. $imagefun($thumbimg, $filename); //新圖片
  212. imagedestroy($thumbimg); //銷毀縮圖
  213. imagedestroy($thumbimg); //銷毀縮圖
  214. imagedestroy($srcimg); //銷毀來源圖
  215. return $filename;
  216. }
  217. /**
  218. * 產生特定尺寸縮圖解決原版縮圖無法滿足特定尺寸的問題PS:會裁掉圖片不符合縮圖比例的部分
  219. * @static
  220. * @access public
  221. * @ ?
  222. * @param boolean $interlace 啟用隔行掃描
  223. * @return void
  224. */
  225. static function thumb2($image, $thumbname, $type='', $maxWidth=200 , $maxHeight=50, $interlace=true) {
  226. // 取得原圖資訊
  227. $info = Image::getImageInfo($image);
  228. if ($info !== false) {
  229. $srcWidth = $info['width'];
  230. $srcHeight = $info['height'];
  231. $type = empty($type) ? $info['type'] : $type;
  232. $type = strtolower($type);
  233. $interlace = $interlace ? 1 : 0;
  234. unset($info);
  235. $scale = max($maxWidth / $srcWidth, $maxHeight / $srcHeight); // 計算縮放比例
  236. //判斷原圖和縮圖比例如原圖寬於縮圖則裁掉兩邊反之..
  237. if($maxWidth / $srcWidth > $maxHeight / $srcHeight){
  238. //高於
  239. $srcX = 0;
  240. $srcY = ($srcHeight - $maxHeight / $scale) / 2 ;
  241. $cutWidth = $srcWidth;
  242. ;
  243. $cutHeight = $maxHeight / $scale;
  244. }else{
  245. //寬於
  246. $srcX = ($srcWidth - $maxWidth / $scale) / 2;
  247. $srcY = 0;
  248. $cutWidth = $maxWidth / $scale;
  249. $cutHeight = $srcHeight;
  250. }
  251. // 載入原圖
  252. $createFun = 'ImageCreateFrom' . ($type == 'jpg' ? 'jpeg' : $type);
  253. $srcImg = $createFun($image) ;
  254. //建立錢包
  255. if ($type != 'gif' && function_exists('imagecreatetruecolor'))
  256. $thumbImg = imagecreatetruecolor($maxWidth, $maxHeight);
  257. $thumbImg = imagecreate($maxWidth, $maxHeight);
  258. // 複製圖片
  259. if (function_exists("ImageCopyResampled"))

    imagecoresppmg,mhmg(pybmg,$pmg,$p,m, 0, $srcX, $srcY, $maxWidth, $maxHeight, $切割寬度,$切割高度);

  260. else
  261. imagecopyresized($thumbImg, $srcImg, 0, 0, $srcX, $srcY, $maxsrcth , $maxHeight, $cutWidth, $cutHeight);
  262. if ('gif' == $type || 'png' == $type) {
  263. //imagealphablending($thumbImg, false);//取消預設的混色模式
  264. //imagesavealpha( $thumbImg,true);//設定保存完整的alpha 通道資訊
  265. $background_color = imagecolorallocate($thumbImg, 0, 255, 0); // 指派一個綠色
  266. imagecolortransparent($thumbImg, $background_color); // 設定為透明色,若註解掉該行則輸出綠色的圖
  267. }
  268. // 對jpeg圖形設定隔行掃描
  269. if ('jpg' == $type || ' jpeg' == $type)
  270. imageinterlace($thumbImg, $interlace);
  271. // 產生圖片
  272. $imageFun = 'image' . ($type == 'jpg' ? 'jpeg' : $type);
  273. @chmod(dirname($thumbname),0777);
  274. $imageFun($thumbImg, $thumbname);
  275. imageFun($thumbImg, $thumbname);
  276. imagedestroy( $thumbImg);
  277. imagedestroy($srcImg);
  278. 回傳$thumbname;
  279. }
  280. 回傳false;
  281. }
  282. /**
  283. /**
  284. /** /
  285. static function getImageInfo($img) {
  286. $imageInfo = getimagesize($img);
  287. if ($imageInfo !== false) {
  288. $imageType = strtolower(subsstr image_type_to_extension($imageInfo[2]), 1));
  289. $imageSize = 檔案大小($img);
  290. $info = array(
  291. "寬度" => $imageInfo[0],
  292. "高度" => $imageInfo[1],
  293. "型" => $imageType,
  294. "大小" => $imageSize,
  295. "mime" => $imageInfo['mime']
  296. );
  297. 回傳$info;
  298. } else {
  299. 回傳false;
  300. }
  301. }
  302. //watermark(來源圖,產生檔案,產生位置,水印檔,水印文字,背景色)
  303. function watermark($source, $target = '' , $w_pos = 9, $w_img = '', $w_text = '', $w_font = 12, $w_color = ' #cccccc',$x='',$y='')
  304. {
  305. if(!$this->check($source)) 回傳false;
  306. if(!$target) $target = $source;
  307. if ($w_img == '' && $w_text == '')
  308. $w_img = $this->w_img;
  309. $source_info = getimagesize($source);
  310. $source_w = $source_info[0]; //取得寬
  311. $source_h = $source_info[1]; //取得高
  312. if($source_w w_minwidth || $source_h w_minheight) return false; // 寬與達不到要求直接回傳
  313. switch($source_info[2]) // 新圖
  314. {
  315. case 1 :
  316. $source_img = imagecreatefromgif($ source);
  317. 休息;
  318. 案例2 :
  319. $source_img = imagecreatefromjpeg($source);
  320. 休息;
  321. 案例3 :
  322. $source_img = imagecreatefrompng($source);
  323. 休息;
  324. 預設:
  325. 回傳false;
  326. }
  327. if(!empty($w_img) && file_exists($w_img)) //水印檔案
  328. {
  329. $ifwaterimage = 1; // 是否浮水印圖
  330. $water_info = getimaimaize($w_imgesg) ; //浮水印資訊
  331. $width = $water_info[0];
  332. $height = $water_info[1];
  333. switch($water_info[2])
  334. {
  335. 情況1 :
  336. $water_img = imagecreatefromgif($w_img);
  337. 休息;
  338. 案例2 :
  339. $water_img = imagecreatefromjpeg($ 3 :
  340. $water_img = imagecreatefrompng($w_img);
  341. 休息;
  342. 預設:
  343. 回傳;
  344. }
  345. }
  346. else $ifwaterimage = 0;
  347. //imagettfbbox本函數計算並傳回一個包圍TrueType文字範圍的虛擬方框的像素大小。
  348. //imagettfbbox ( 字體大小, 字體角度, 字體檔案,檔案)
  349. // echo $this->fontfile;
  350. $temp = imagettfbbox(ceil($w_font*1.6), 0, $this ->fontfile, $w_text);//取得使用truetype 字體的文字的範圍
  351. $width = $temp[4] - $temp[6]; //右上角X 位置- 左上角X 位置
  352. $height = $temp[3] - $temp[5]; //右下角Y 位置- 右上角Y 位置
  353. unset($temp);
  354. }
  355. switch($w_pos)
  356. {
  357. case 0: //隨機位置
  358. $wx = rand(0,($source_w - $width));
  359. $wy = rand(0,($source_h - $height));
  360. break;
  361. case 1: //左上角
  362. $wx = 25;
  363. $wy = 25;
  364. break;
  365. case 2: //上面中間位置
  366. $ wx = ($source_w - $width) / 2;
  367. $wy = 0;
  368. break;
  369. case 3: //右上角
  370. $wx = $source_w - $width;
  371. $wy = 0;
  372. break;
  373. case 4: //左邊中間位置
  374. $wx = 0;
  375. $wy = ($source_h - $height) / 2;
  376. break;
  377. case 5: //中間位置
  378. $wx = ($source_w - $width) / 2;
  379. $wy = ($source_h - $height) / 2;
  380. break;
  381. case 6: //底部中間位置
  382. $wx = ($source_w - $width) / 2;
  383. $wy = $source_h - $height;
  384. break;
  385. case 7: //左下角
  386. $wx = 0;
  387. $wy = $source_h - $height;
  388. break;
  389. case 8: //右邊中間位置
  390. $wx = $source_w - $width;
  391. $wy = ($source_h - $height) /2;
  392. break;
  393. case 9: //右下角
  394. $wx = $source_w - $width;
  395. $wy = $source_h - $height ;
  396. break;
  397. default: //隨機
  398. $wx = rand(0,($source_w - $width));
  399. $wy = rand(0,($source_h - $height));
  400. break;
  401. case 10://自訂位置
  402. $wx = $x;
  403. $wy = $y;
  404. case 11: //底部中間位置--手工客專用
  405. $wx = ($source_w - $width) / 2;
  406. $wy = $source_h - $height-50;
  407. break;
  408. break;
  409. }
  410. if($ifwaterimage) //如果有浮水印圖
  411. {
  412. //imagecopymerge 拷貝並合併影像的一部份
  413. //參數(源圖,水印圖,拷貝到源圖x位置,拷貝到來源圖y位置,從水印圖x位置,從水印圖y位置,高,寬,透明度)
  414. //imagecopymerge($source_img, $water_img, $wx, $wy, 0, 0, $width, $height, $this->w_pct);
  415. imagecopy($source_img,$water_img,$wx,$wy,0,0,$width,$height);
  416. }
  417. else
  418. {
  419. if(!empty($w_color) && (strlen($w_color)==7))
  420. {
  421. $r = hexdec(substr($w_color,1,2) ); //取得紅色
  422. $g = hexdec(substr($w_color,3,2)); //取得綠色
  423. $b = hexdec(substr($w_color,5)); //取得藍色
  424. }
  425. else
  426. {
  427. return;
  428. }
  429. //imagecolorallocate 基於調色盤的圖片填滿背景色
  430. //imagestring 水平畫一行字串
  431. //imagestring(來源圖,字體大小,位置X,位置Y,文字,顏色)
  432. //參數($image, float $size, float $angle, int $x, int $y, int $ color, string $fontfile, string $text)
  433. imagettftext($source_img,$w_font,0,$wx,$wy,imagecolorallocate($source_img,$r,$g,$b),$this->fontfile, $w_text);
  434. //imagestring($source_img,$w_font,$wx,$wy,$w_text,imagecolorallocate($source_img,$r,$g,$b));
  435. }
  436. //輸出到檔案或瀏覽器
  437. switch($source_info[2])
  438. {
  439. case 1 :
  440. imagegif($source_img, $target); //以GIF 格式將影像輸出至瀏覽器或檔案
  441. break;
  442. case 2 :
  443. imagejpeg($source_img, $target, $this->w_quality); //以JPEG 格式將影像輸出到瀏覽器或檔案
  444. break;
  445. case 3 :
  446. imagepng($source_img, $target); //以PNG 格式將圖片輸出到瀏覽器或檔案
  447. break;
  448. default :
  449. return;
  450. }
  451. if(isset($water_info))
  452. {
  453. unset($water_info); //銷毀
  454. }
  455. if(isset($water_img))
  456. {
  457. imagedestroy($water_img); //銷毀
  458. }
  459. unset($source_info);
  460. imagedestroy($source_img);
  461. return true; //gd庫必須存在,後綴為jpg|jpeg|gif|png,文件存在,imagecreatefromjpeg或者imagecreatefromgif存在
  462. function check($image)
  463. {
  464. return extension_loaded('gd') &&
  465. preg_match("/.(JPG|JPEG|PNG|GIF|jpg|jpeg|gif|png)/i", $image, $m) &&
  466. file_exists($image) &&
  467. function_exists('imagecreatefrom '.($m[1] == 'jpg' ? 'jpeg' : $m[1]));
  468. //imagecreatefromjpeg
  469. //imagecreatefromgif
  470. //imagecreatefrompng
  471. }
  472. }
  473. /**
  474. 縮圖
  475. 1.新建一個圖像資源通過imagecreatefromgif imagecreatefromjpeg imagecreatefrompng
  476. 2.imagecopyresampled 拷貝圖片,並調整大小
  477. 水印:圖片
  478. 2.加浮水印
  479. 圖片浮水印:imagecopymerge 把2張圖合併在一起
  480. 文字浮水印:imagettftext 寫入文字
  481. */
  482. error_reporting(0);
  483. $hostdir = dirname(__FILE__);
  484. $filesnames = scandir($hostdir.'/desktop' );
  485. $img = new image();
  486. foreach($filesnames as $name){
  487. if($name != '.' && $name != '..'){
  488. $imgPath = $hostdir.'/desktop/';
  489. $imgsize = getimagesize($imgPath.$name);
  490. $imgInfo = explode('.',$name);
  491. $imginfo2 = explode('_',$imgInfo[0]);
  492. if($imginfo2[count($imginfo2)-1] != 'small'){
  493. $img_small = $imgPath.$imgInfo[0] .'_small.'.$imgInfo[1];
  494. $img->thumb2($imgPath.$name,$img_small,'',$imgsize[0]/4,$imgsize[1]/4);
  495. }
  496. }
  497. }
  498. 複製程式碼

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn