国内流行的内容管理系统(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.

299 lines
10KB

  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. switch ($srcInfo[2]) {
  75. case 1:
  76. imagegif($ni, $toFile);
  77. break;
  78. case 2:
  79. imagejpeg($ni, $toFile, 85);
  80. break;
  81. case 3:
  82. imagepng($ni, $toFile);
  83. break;
  84. case 6:
  85. imagebmp($ni, $toFile);
  86. break;
  87. default:
  88. return FALSE;
  89. }
  90. imagedestroy($ni);
  91. }
  92. imagedestroy($im);
  93. return true;
  94. } catch(Throwable $th) {
  95. return false;
  96. } catch (Exception $e) {
  97. return false;
  98. }
  99. }
  100. }
  101. /**
  102. * 获得GD的版本
  103. *
  104. * @access public
  105. * @return int
  106. */
  107. if (!function_exists('gdversion')) {
  108. function gdversion()
  109. {
  110. //没启用php.ini函数的情况下如果有GD默认视作2.0以上版本
  111. if (!function_exists('phpinfo')) {
  112. if (function_exists('imagecreate')) {
  113. return '2.0';
  114. } else {
  115. return 0;
  116. }
  117. } else {
  118. ob_start();
  119. phpinfo(8);
  120. $module_info = ob_get_contents();
  121. ob_end_clean();
  122. if (preg_match("/\bgd\s+version\b[^\d\n\r]+?([\d\.]+)/i", $module_info, $matches)) {
  123. $gdversion_h = $matches[1];
  124. } else {
  125. $gdversion_h = 0;
  126. }
  127. return $gdversion_h;
  128. }
  129. }
  130. }
  131. /**
  132. * 图片自动加水印函数
  133. *
  134. * @access public
  135. * @param string $srcFile 图片源文件
  136. * @param string $fromGo 位置
  137. * @return string
  138. */
  139. if (!function_exists('WaterImg')) {
  140. function WaterImg($srcFile, $fromGo = 'up')
  141. {
  142. include(DEDEDATA.'/mark/inc_photowatermark_config.php');
  143. require_once(DEDEINC.'/image.class.php');
  144. if (isset($GLOBALS['needwatermark'])) {
  145. $photo_markup = $photo_markdown = empty($GLOBALS['needwatermark']) ? '0' : '1';
  146. }
  147. if ($photo_markup != '1' || ($fromGo == 'collect' && $photo_markdown != '1')) {
  148. return;
  149. }
  150. $info = '';
  151. $srcInfo = @getimagesize($srcFile, $info);
  152. $srcFile_w = $srcInfo[0];
  153. $srcFile_h = $srcInfo[1];
  154. if ($srcFile_w < $photo_wwidth || $srcFile_h < $photo_wheight) {
  155. return;
  156. }
  157. if ($fromGo == 'up' && $photo_markup == '0') {
  158. return;
  159. }
  160. if ($fromGo == 'down' && $photo_markdown == '0') {
  161. return;
  162. }
  163. $TRUEMarkimg = DEDEDATA.'/mark/'.$photo_markimg;
  164. if (!file_exists($TRUEMarkimg) || empty($photo_markimg)) {
  165. $TRUEMarkimg = "";
  166. }
  167. if ($photo_waterpos == 0) {
  168. $photo_waterpos = rand(1, 9);
  169. }
  170. $cfg_watermarktext = array();
  171. if ($photo_marktype == '2') {
  172. if (file_exists(DEDEDATA.'/mark/simhei.ttf')) {
  173. $cfg_watermarktext['fontpath'] = DEDEDATA.'/mark/simhei.ttf';
  174. } else {
  175. return;
  176. }
  177. }
  178. $cfg_watermarktext['text'] = $photo_watertext;
  179. $cfg_watermarktext['size'] = $photo_fontsize;
  180. $cfg_watermarktext['angle'] = '0';
  181. $cfg_watermarktext['color'] = '255,255,255';
  182. $cfg_watermarktext['shadowx'] = '0';
  183. $cfg_watermarktext['shadowy'] = '0';
  184. $cfg_watermarktext['shadowcolor'] = '0,0,0';
  185. $photo_marktrans = 85;
  186. $img = new image($srcFile, 0, $cfg_watermarktext, $photo_waterpos, $photo_diaphaneity, $photo_wheight, $photo_wwidth, $photo_marktype, $photo_marktrans, $TRUEMarkimg);
  187. $img->watermark(0);
  188. }
  189. }
  190. /**
  191. * 会对空白地方填充满
  192. *
  193. * @access public
  194. * @param string $srcFile 图片路径
  195. * @param string $toW 转换到的宽度
  196. * @param string $toH 转换到的高度
  197. * @param string $toFile 输出文件到
  198. * @param string $issave 是否保存
  199. * @return bool
  200. */
  201. if (!function_exists('ImageResizeNew')) {
  202. function ImageResizeNew($srcFile, $toW, $toH, $toFile = '', $issave = TRUE)
  203. {
  204. global $cfg_photo_type, $cfg_ddimg_bgcolor;
  205. if ($toFile == '') $toFile = $srcFile;
  206. $info = '';
  207. $srcInfo = GetImageSize($srcFile, $info);
  208. switch ($srcInfo[2]) {
  209. case 1:
  210. if (!$cfg_photo_type['gif']) return FALSE;
  211. $img = imagecreatefromgif($srcFile);
  212. break;
  213. case 2:
  214. if (!$cfg_photo_type['jpeg']) return FALSE;
  215. $img = imagecreatefromjpeg($srcFile);
  216. break;
  217. case 3:
  218. if (!$cfg_photo_type['png']) return FALSE;
  219. $img = imagecreatefrompng($srcFile);
  220. break;
  221. case 6:
  222. if (!$cfg_photo_type['bmp']) return FALSE;
  223. $img = imagecreatefromwbmp($srcFile);
  224. break;
  225. }
  226. $width = imageSX($img);
  227. $height = imageSY($img);
  228. if (!$width || !$height) {
  229. return FALSE;
  230. }
  231. $target_width = $toW;
  232. $target_height = $toH;
  233. $target_ratio = $target_width / $target_height;
  234. $img_ratio = $width / $height;
  235. if ($target_ratio > $img_ratio) {
  236. $new_height = $target_height;
  237. $new_width = $img_ratio * $target_height;
  238. } else {
  239. $new_height = $target_width / $img_ratio;
  240. $new_width = $target_width;
  241. }
  242. if ($new_height > $target_height) {
  243. $new_height = $target_height;
  244. }
  245. if ($new_width > $target_width) {
  246. $new_height = $target_width;
  247. }
  248. $new_img = ImageCreateTrueColor($target_width, $target_height);
  249. if ($cfg_ddimg_bgcolor == 0) $bgcolor = ImageColorAllocate($new_img, 0xff, 0xff, 0xff);
  250. else $bgcolor = 0;
  251. if (!@imagefilledrectangle($new_img, 0, 0, $target_width - 1, $target_height - 1, $bgcolor)) {
  252. return FALSE;
  253. }
  254. if (!@imagecopyresampled($new_img, $img, ($target_width - $new_width) / 2, ($target_height - $new_height) / 2, 0, 0, $new_width, $new_height, $width, $height)) {
  255. return FALSE;
  256. }
  257. //保存为目标文件
  258. if ($issave) {
  259. switch ($srcInfo[2]) {
  260. case 1:
  261. imagegif($new_img, $toFile);
  262. break;
  263. case 2:
  264. imagejpeg($new_img, $toFile, 100);
  265. break;
  266. case 3:
  267. imagepng($new_img, $toFile);
  268. break;
  269. case 6:
  270. imagebmp($new_img, $toFile);
  271. break;
  272. default:
  273. return FALSE;
  274. }
  275. }
  276. //不保存
  277. else {
  278. switch ($srcInfo[2]) {
  279. case 1:
  280. imagegif($new_img);
  281. break;
  282. case 2:
  283. imagejpeg($new_img);
  284. break;
  285. case 3:
  286. imagepng($new_img);
  287. break;
  288. case 6:
  289. imagebmp($new_img);
  290. break;
  291. default:
  292. return FALSE;
  293. }
  294. }
  295. imagedestroy($new_img);
  296. imagedestroy($img);
  297. return TRUE;
  298. }
  299. }