image.init.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. if (!defined('IS_INITPHP')) exit('Access Denied!');
  3. /*********************************************************************************
  4. * InitPHP 3.8.2 国产PHP开发框架 扩展类库-图片处理类
  5. *-------------------------------------------------------------------------------
  6. * 版权所有: CopyRight By initphp.com
  7. * 您可以自由使用该源码,但是在使用过程中,请保留作者信息。尊重他人劳动成果就是尊重自己
  8. *-------------------------------------------------------------------------------
  9. * Author:zhuli Dtime:2014-11-25
  10. ***********************************************************************************/
  11. class imageInit {
  12. /**
  13. * 图像处理-缩略图处理
  14. * @param string $source 被缩略的图
  15. * @param stirng $newname缩略后的新图
  16. * @param int $width 宽度
  17. * @param int $height 高度
  18. * @param bool $isauto 缩略方式,true-保持比例缩放|false-超出部分裁剪
  19. */
  20. public function make_thumb($source, $newname, $width = 100, $height = 100, $isauto = true) {
  21. if (!file_exists($source)) return false;
  22. $image_info = $this->get_img_info($source); //获取原始图片信息
  23. if ($image_info['type'] == 'bmp') return false;
  24. list($imagecreate, $imagecopyre) = $this->get_image_create($image_info['type']); //获取创建图像和拷贝图像的函数
  25. if (empty($image_info) || !$imagecreate) return false;
  26. /* 画布和原始图创建 */
  27. $thumb = $imagecreate($width, $height); //创建一个图片-画布
  28. $imagecreatefrom = 'imagecreatefrom' .$image_info['type'];
  29. $source = $imagecreatefrom($source);
  30. if(function_exists('ImageColorAllocate') && function_exists('ImageColorTransparent')){
  31. $black = ImageColorAllocate($thumb,0,0,0); //着色
  32. $bgTransparent = ImageColorTransparent($thumb,$black);
  33. }
  34. /*根据原始图片的长宽比自动缩略*/
  35. $src_x = $src_y = 0;
  36. //保持比例缩放
  37. if ($isauto == true) {
  38. $x_ratio = $width / $image_info['width'];
  39. $y_ratio = $height / $image_info['height'];
  40. if(($x_ratio * $image_info['height']) < $height) {
  41. $h = ceil($x_ratio * $image_info['height']);
  42. $w = $width;
  43. } else {
  44. $w = ceil($y_ratio * $image_info['width']);
  45. $h = $height;
  46. }
  47. $thumb = $imagecreate($w, $h); //创建一个图片-画布
  48. $imagecopyre($thumb, $source, 0, 0, 0, 0, $w, $h, $image_info['width'], $image_info['height']); //拷贝
  49. }
  50. //超出部分裁剪
  51. else{
  52. if ($image_info['width'] < $image_info['height']) {
  53. $src_y = round(($image_info['height'] - $image_info['width']) / 2);
  54. $image_info['height'] = $image_info['width'];
  55. } else {
  56. $src_x = round(($image_info['width'] - $image_info['height']) / 2);
  57. $image_info['width'] = $image_info['height'];
  58. }
  59. /* 拷贝和创建图像 */
  60. $imagecopyre($thumb, $source, 0, 0, $src_x, $src_y, $width, $height, $image_info['width'], $image_info['height']);
  61. }
  62. $result = $this->make($image_info['type'], $thumb, $newname);
  63. if (!$result) return array(); //缩略图创建失败
  64. imagedestroy($thumb);
  65. return array($width, $height);
  66. }
  67. /**
  68. * 图像处理-图片水印
  69. * @param string $source_path 需要生成水印的图片
  70. * @param string $water_path 水印图片位置
  71. * @param string $position 显示的位置:0 随机|1 左上|2 中上|3 右上 |4 左下|5中下|6右下|其它中间
  72. * @param string $pct 水印图片透明度
  73. * @param string $quality 生成图片的品质,0-100,值越大品质越高
  74. * @return bool
  75. */
  76. public function water_mark($source_path, $water_path, $postion, $pct=100, $quality=100) {
  77. if (!file_exists($source_path) || !file_exists($water_path)) return false;
  78. /* 图片信息 */
  79. $source_info = $this->get_img_info($source_path);
  80. $water_info = $this->get_img_info($water_path);
  81. if ($source_info['type'] == 'bmp' || $water_info['type'] == 'bmp') return false;
  82. if ($source_info['type']) {
  83. $imagecreatefrom = 'imagecreatefrom' .$source_info['type'];
  84. $source = $imagecreatefrom($source_path);
  85. }
  86. if ($water_info['type']) {
  87. $imagecreatefrom = 'imagecreatefrom' .$water_info['type'];
  88. $water = $imagecreatefrom($water_path);
  89. }
  90. /* 处理 */
  91. imagealphablending($source, true); //混色模式
  92. list($x, $y) = $this->get_water_postion($postion, $source_info['width'], $source_info['height'], $water_info['width'], $water_info['height']);
  93. if ($water_info['type'] == 'png') {
  94. $temp = imagecreatetruecolor($source_info['width'], $source_info['height']); //创建着色板
  95. imagecopy($temp, $source, 0, 0, 0, 0, $source_info['width'], $source_info['height']); //将源图拷贝进着色版
  96. imagecopy($temp, $water, $x, $y, 0, 0, $water_info['width'], $water_info['height']); //将水印拷贝进着色办
  97. $source = $temp;
  98. } else {
  99. imagecopymerge($source, $water, $x, $y, 0,0, $water_info['width'], $water_info['height'], $pct); //合并
  100. }
  101. $ext = strtolower(substr(strrchr(basename($source_path), '.'), 1));
  102. $shource_dir = dirname($source_path); //修改水印BUG
  103. $source_path = $shource_dir . '/' . basename($source_path, '.' . $ext);
  104. $this->make($source_info['type'], $source, $source_path, $quality);
  105. imagedestroy($source);
  106. imagedestroy($water);
  107. return true;
  108. }
  109. /**
  110. * 图像处理-获取水印位置
  111. * @param int $position 水印位置
  112. * @param int $source_width 原始图片的宽
  113. * @param int $source_height原始图片的高
  114. * @param int $water_width 水印图片的宽
  115. * @param int $water_height 水印图片的高
  116. * @return array(宽,高)
  117. */
  118. private function get_water_postion($position, $source_width, $source_height, $water_width, $water_height) {
  119. if ($position == 0) {
  120. $x = rand(0, ($source_width - $water_width));
  121. $y = rand(0, ($source_height - $water_height));
  122. } elseif ($position == 1) {
  123. $x = 5;
  124. $y = 5;
  125. } elseif ($position == 2) {
  126. $x = ($source_width - $water_width) / 2;
  127. $y = 5;
  128. } elseif ($position == 3) {
  129. $x = $source_width - $water_width - 5;
  130. $y = 5;
  131. } elseif ($position == 4) {
  132. $x = 5;
  133. $y = $source_height - $water_height - 5;
  134. } elseif ($position == 5) {
  135. $x = ($source_width - $water_width) / 2;
  136. $y = $source_height - $water_height - 5;
  137. } elseif ($position == 6) {
  138. $x = $source_width - $water_width - 5;
  139. $y = $source_height - $water_height - 5;
  140. } else {
  141. $x = ($source_width - $water_width) / 2;
  142. $y = ($source_height - $water_height) / 2;
  143. }
  144. return array($x, $y);
  145. }
  146. /*
  147. * 图像处理-生成图片
  148. * @param string $type 图片类型
  149. * @param string $image 图片资源
  150. * @param string $filename 图片文件名
  151. * @param int $quality 图片质量,对jpeg有效
  152. * @return bool
  153. */
  154. private function make($type, $image, $filename, $quality = '75') {
  155. $file_type = ($type !== 'jpeg') ? $type : 'jpg';
  156. $filename = $filename . '.' . $file_type;
  157. $this->createFolder(dirname($filename)); //创建目录
  158. $makeimage = 'image' . $type;
  159. if (!function_exists($makeimage)) {
  160. return false;
  161. }
  162. if ($type == 'jpeg') {
  163. $makeimage($image, $filename, $quality);
  164. } else {
  165. $makeimage($image, $filename);
  166. }
  167. return true;
  168. }
  169. /**
  170. * 图像处理-获取图像信息
  171. * @param string $source 源文件图片
  172. * @return array(图片的宽、高、类型)
  173. */
  174. private function get_img_info($source) {
  175. $imginfo = array();
  176. $ext = strtolower(substr(strrchr($source, '.'), 1)); //获取图片类型
  177. $image_type = array(1=>'gif', 2=>'jpeg', 3=>'png', 6=>'bmp');
  178. if (function_exists('read_exif_data') && in_array($ext, array('jpg','jpeg','jpe','jfif'))) { //jpeg情况
  179. $temp = @read_exif_data($source);
  180. $imginfo['width'] = $temp['COMPUTED']['Width'];
  181. $imginfo['height'] = $temp['COMPUTED']['Height'];
  182. $imginfo['type'] = 2;
  183. unset($temp);
  184. }
  185. if (empty($imginfo)) { //png,gif,bmp
  186. list($imginfo['width'], $imginfo['height'], $imginfo['type']) = @getimagesize($source);
  187. }
  188. $imginfo['type'] = $image_type[$imginfo['type']];
  189. return $imginfo;
  190. }
  191. /**
  192. * 图像处理-返回图像创建和图像
  193. * 说明:gif和jpeg等函数的图像创建函数不一样
  194. * @param string $imagetype 图像类型-gif、jpeg、png、bmp
  195. * @return array(图像创建函数, 图像拷贝函数)
  196. */
  197. private function get_image_create($imagetype) {
  198. if ($imagetype != 'gif' && function_exists('imagecreatetruecolor') && function_exists('imagecopyresampled')) {
  199. return array('imagecreatetruecolor','imagecopyresampled');
  200. } elseif (function_exists('imagecreate') && function_exists('imagecopyresized')) {
  201. return array('imagecreate','imagecopyresized');
  202. } else {
  203. return array();
  204. }
  205. }
  206. /**
  207. * 图像处理-创建目录 如果目录存在,则不创建,不存在则创建 static
  208. * @param $path 路径
  209. * @return
  210. */
  211. public static function createFolder($path) {
  212. if (!is_dir($path)) {
  213. imageInit::createFolder(dirname($path));
  214. @mkdir($path);
  215. @chmod($path, 0777);
  216. @fclose(@fopen($path . '/index.html', 'w'));
  217. @chmod($path . '/index.html', 0777);
  218. }
  219. }
  220. }