国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

356 строки
10KB

  1. <?php if(!defined('DEDEINC')) exit("Request Error!");
  2. /**
  3. * 图像处理相关函数
  4. *
  5. * @version $Id: image.func.php 1 15:59 2010年7月5日Z tianya $
  6. * @package DedeCMS.Helpers
  7. * @copyright Copyright (c) 2020, DedeBIZ.COM
  8. * @license https://www.dedebiz.com/license
  9. * @link https://www.dedebiz.com
  10. */
  11. // ------------------------------------------------------------------------
  12. /**
  13. * 缩图片自动生成函数,来源支持bmp、gif、jpg、png
  14. * 但生成的小图只用jpg或png格式
  15. *
  16. * @access public
  17. * @param string $srcFile 图片路径
  18. * @param string $toW 转换到的宽度
  19. * @param string $toH 转换到的高度
  20. * @param string $toFile 输出文件到
  21. * @return string
  22. */
  23. if ( ! function_exists('ImageResize'))
  24. {
  25. function ImageResize($srcFile, $toW, $toH, $toFile="")
  26. {
  27. global $cfg_photo_type;
  28. if($toFile=='') $toFile = $srcFile;
  29. $info = '';
  30. $srcInfo = GetImageSize($srcFile,$info);
  31. switch ($srcInfo[2])
  32. {
  33. case 1:
  34. if(!$cfg_photo_type['gif']) return FALSE;
  35. $im = imagecreatefromgif($srcFile);
  36. break;
  37. case 2:
  38. if(!$cfg_photo_type['jpeg']) return FALSE;
  39. $im = imagecreatefromjpeg($srcFile);
  40. break;
  41. case 3:
  42. if(!$cfg_photo_type['png']) return FALSE;
  43. $im = imagecreatefrompng($srcFile);
  44. break;
  45. case 6:
  46. if(!$cfg_photo_type['bmp']) return FALSE;
  47. $im = imagecreatefromwbmp($srcFile);
  48. break;
  49. }
  50. $srcW=ImageSX($im);
  51. $srcH=ImageSY($im);
  52. if($srcW<=$toW && $srcH<=$toH ) return TRUE;
  53. $toWH=$toW/$toH;
  54. $srcWH=$srcW/$srcH;
  55. if($toWH<=$srcWH)
  56. {
  57. $ftoW=$toW;
  58. $ftoH=$ftoW*($srcH/$srcW);
  59. }
  60. else
  61. {
  62. $ftoH=$toH;
  63. $ftoW=$ftoH*($srcW/$srcH);
  64. }
  65. if($srcW>$toW||$srcH>$toH)
  66. {
  67. if(function_exists("imagecreateTRUEcolor"))
  68. {
  69. @$ni = imagecreateTRUEcolor($ftoW,$ftoH);
  70. if($ni)
  71. {
  72. imagecopyresampled($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH);
  73. }
  74. else
  75. {
  76. $ni=imagecreate($ftoW,$ftoH);
  77. imagecopyresized($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH);
  78. }
  79. }
  80. else
  81. {
  82. $ni=imagecreate($ftoW,$ftoH);
  83. imagecopyresized($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH);
  84. }
  85. switch ($srcInfo[2])
  86. {
  87. case 1:
  88. imagegif($ni,$toFile);
  89. break;
  90. case 2:
  91. imagejpeg($ni,$toFile,85);
  92. break;
  93. case 3:
  94. imagepng($ni,$toFile);
  95. break;
  96. case 6:
  97. imagebmp($ni,$toFile);
  98. break;
  99. default:
  100. return FALSE;
  101. }
  102. imagedestroy($ni);
  103. }
  104. imagedestroy($im);
  105. return TRUE;
  106. }
  107. }
  108. /**
  109. * 获得GD的版本
  110. *
  111. * @access public
  112. * @return int
  113. */
  114. if ( ! function_exists('gdversion'))
  115. {
  116. function gdversion()
  117. {
  118. //没启用php.ini函数的情况下如果有GD默认视作2.0以上版本
  119. if(!function_exists('phpinfo'))
  120. {
  121. if(function_exists('imagecreate'))
  122. {
  123. return '2.0';
  124. }
  125. else
  126. {
  127. return 0;
  128. }
  129. }
  130. else
  131. {
  132. ob_start();
  133. phpinfo(8);
  134. $module_info = ob_get_contents();
  135. ob_end_clean();
  136. if(preg_match("/\bgd\s+version\b[^\d\n\r]+?([\d\.]+)/i", $module_info,$matches))
  137. {
  138. $gdversion_h = $matches[1];
  139. }
  140. else
  141. {
  142. $gdversion_h = 0;
  143. }
  144. return $gdversion_h;
  145. }
  146. }
  147. }
  148. /**
  149. * 图片自动加水印函数
  150. *
  151. * @access public
  152. * @param string $srcFile 图片源文件
  153. * @param string $fromGo 位置
  154. * @return string
  155. */
  156. if ( ! function_exists('WaterImg'))
  157. {
  158. function WaterImg($srcFile, $fromGo='up')
  159. {
  160. include(DEDEDATA.'/mark/inc_photowatermark_config.php');
  161. require_once(DEDEINC.'/image.class.php');
  162. if( isset($GLOBALS['needwatermark']) )
  163. {
  164. $photo_markup = $photo_markdown = empty($GLOBALS['needwatermark']) ? '0' : '1';
  165. }
  166. if($photo_markup != '1' || ($fromGo=='collect' && $photo_markdown!='1') )
  167. {
  168. return;
  169. }
  170. $info = '';
  171. $srcInfo = @getimagesize($srcFile,$info);
  172. $srcFile_w = $srcInfo[0];
  173. $srcFile_h = $srcInfo[1];
  174. if($srcFile_w < $photo_wwidth || $srcFile_h < $photo_wheight)
  175. {
  176. return;
  177. }
  178. if($fromGo=='up' && $photo_markup=='0')
  179. {
  180. return;
  181. }
  182. if($fromGo=='down' && $photo_markdown=='0')
  183. {
  184. return;
  185. }
  186. $TRUEMarkimg = DEDEDATA.'/mark/'.$photo_markimg;
  187. if(!file_exists($TRUEMarkimg) || empty($photo_markimg))
  188. {
  189. $TRUEMarkimg = "";
  190. }
  191. if($photo_waterpos == 0)
  192. {
  193. $photo_waterpos = rand(1, 9);
  194. }
  195. $cfg_watermarktext = array();
  196. if($photo_marktype == '2')
  197. {
  198. if(file_exists(DEDEDATA.'/mark/simhei.ttf'))
  199. {
  200. $cfg_watermarktext['fontpath'] = DEDEDATA.'/mark/simhei.ttf';
  201. }
  202. else
  203. {
  204. return ;
  205. }
  206. }
  207. $cfg_watermarktext['text'] = $photo_watertext;
  208. $cfg_watermarktext['size'] = $photo_fontsize;
  209. $cfg_watermarktext['angle'] = '0';
  210. $cfg_watermarktext['color'] = '255,255,255';
  211. $cfg_watermarktext['shadowx'] = '0';
  212. $cfg_watermarktext['shadowy'] = '0';
  213. $cfg_watermarktext['shadowcolor'] = '0,0,0';
  214. $photo_marktrans = 85;
  215. $img = new image($srcFile,0, $cfg_watermarktext, $photo_waterpos, $photo_diaphaneity, $photo_wheight, $photo_wwidth, $photo_marktype, $photo_marktrans,$TRUEMarkimg);
  216. $img->watermark(0);
  217. }
  218. }
  219. /**
  220. * 会对空白地方填充满
  221. *
  222. * @access public
  223. * @param string $srcFile 图片路径
  224. * @param string $toW 转换到的宽度
  225. * @param string $toH 转换到的高度
  226. * @param string $toFile 输出文件到
  227. * @param string $issave 是否保存
  228. * @return bool
  229. */
  230. if ( ! function_exists('ImageResizeNew'))
  231. {
  232. function ImageResizeNew($srcFile, $toW, $toH, $toFile='', $issave=TRUE)
  233. {
  234. global $cfg_photo_type, $cfg_ddimg_bgcolor;
  235. if($toFile=='') $toFile = $srcFile;
  236. $info = '';
  237. $srcInfo = GetImageSize($srcFile,$info);
  238. switch ($srcInfo[2])
  239. {
  240. case 1:
  241. if(!$cfg_photo_type['gif']) return FALSE;
  242. $img = imagecreatefromgif($srcFile);
  243. break;
  244. case 2:
  245. if(!$cfg_photo_type['jpeg']) return FALSE;
  246. $img = imagecreatefromjpeg($srcFile);
  247. break;
  248. case 3:
  249. if(!$cfg_photo_type['png']) return FALSE;
  250. $img = imagecreatefrompng($srcFile);
  251. break;
  252. case 6:
  253. if(!$cfg_photo_type['bmp']) return FALSE;
  254. $img = imagecreatefromwbmp($srcFile);
  255. break;
  256. }
  257. $width = imageSX($img);
  258. $height = imageSY($img);
  259. if (!$width || !$height) {
  260. return FALSE;
  261. }
  262. $target_width = $toW;
  263. $target_height = $toH;
  264. $target_ratio = $target_width / $target_height;
  265. $img_ratio = $width / $height;
  266. if ($target_ratio > $img_ratio) {
  267. $new_height = $target_height;
  268. $new_width = $img_ratio * $target_height;
  269. } else {
  270. $new_height = $target_width / $img_ratio;
  271. $new_width = $target_width;
  272. }
  273. if ($new_height > $target_height) {
  274. $new_height = $target_height;
  275. }
  276. if ($new_width > $target_width) {
  277. $new_height = $target_width;
  278. }
  279. $new_img = ImageCreateTrueColor($target_width, $target_height);
  280. if($cfg_ddimg_bgcolor==0) $bgcolor = ImageColorAllocate($new_img, 0xff, 0xff, 0xff);
  281. else $bgcolor = 0;
  282. if (!@imagefilledrectangle($new_img, 0, 0, $target_width-1, $target_height-1, $bgcolor))
  283. {
  284. return FALSE;
  285. }
  286. if (!@imagecopyresampled($new_img, $img, ($target_width-$new_width)/2, ($target_height-$new_height)/2, 0, 0, $new_width, $new_height, $width, $height))
  287. {
  288. return FALSE;
  289. }
  290. //保存为目标文件
  291. if($issave)
  292. {
  293. switch ($srcInfo[2])
  294. {
  295. case 1:
  296. imagegif($new_img, $toFile);
  297. break;
  298. case 2:
  299. imagejpeg($new_img, $toFile,100);
  300. break;
  301. case 3:
  302. imagepng($new_img, $toFile);
  303. break;
  304. case 6:
  305. imagebmp($new_img, $toFile);
  306. break;
  307. default:
  308. return FALSE;
  309. }
  310. }
  311. //不保存
  312. else
  313. {
  314. switch ($srcInfo[2])
  315. {
  316. case 1:
  317. imagegif($new_img);
  318. break;
  319. case 2:
  320. imagejpeg($new_img);
  321. break;
  322. case 3:
  323. imagepng($new_img);
  324. break;
  325. case 6:
  326. imagebmp($new_img);
  327. break;
  328. default:
  329. return FALSE;
  330. }
  331. }
  332. imagedestroy($new_img);
  333. imagedestroy($img);
  334. return TRUE;
  335. }
  336. }