国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

320 lines
11KB

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