thumbnail.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. <?php
  2. //error_reporting(E_ALL & ~E_NOTICE);
  3. //ini_set("display_errors", 1);
  4. ini_set('memory_limit', '-1');
  5. class ImagickUtils
  6. {
  7. /**
  8. * @var Imagick
  9. */
  10. private $image = null;
  11. private $type = null;
  12. /**
  13. * 构造函数
  14. *
  15. * ImagickUtils constructor.
  16. */
  17. public function __construct()
  18. {
  19. }
  20. /**
  21. * 析构函数
  22. */
  23. public function __destruct()
  24. {
  25. if ($this->image !== null) $this->image->destroy();
  26. }
  27. /**
  28. * 载入图像
  29. * @param $path
  30. * @return Imagick|null
  31. * @throws \ImagickException
  32. */
  33. public function open($path)
  34. {
  35. $this->image = new Imagick($path);
  36. if ($this->image) {
  37. $this->type = strtolower($this->image->getImageFormat());
  38. }
  39. return $this->image;
  40. }
  41. /**
  42. * @return Imagick
  43. */
  44. public function getImage(){
  45. return $this->image;
  46. }
  47. /**
  48. * @param int $x
  49. * @param int $y
  50. * @param null $width
  51. * @param null $height
  52. * @throws \ImagickException
  53. */
  54. public function crop($x = 0, $y = 0, $width = null, $height = null)
  55. {
  56. if ($width == null) $width = $this->image->getImageWidth() - $x;
  57. if ($height == null) $height = $this->image->getImageHeight() - $y;
  58. if ($width <= 0 || $height <= 0) return;
  59. if ($this->type == 'gif') {
  60. $image = $this->image;
  61. $canvas = new Imagick();
  62. $images = $image->coalesceImages();
  63. foreach ($images as $frame) {
  64. $img = new Imagick();
  65. $img->readImageBlob($frame);
  66. $img->cropImage($width, $height, $x, $y);
  67. $canvas->addImage($img);
  68. $canvas->setImageDelay($img->getImageDelay());
  69. $canvas->setImagePage($width, $height, 0, 0);
  70. }
  71. $image->destroy();
  72. $this->image = $canvas;
  73. } else {
  74. $this->image->cropImage($width, $height, $x, $y);
  75. }
  76. }
  77. /*
  78. * 更改图像大小
  79. $fit: 适应大小方式
  80. 'force': 把图片强制变形成 $width X $height 大小
  81. 'scale': 按比例在安全框 $width X $height 内缩放图片, 输出缩放后图像大小 不完全等于 $width X $height
  82. 'scale_fill': 按比例在安全框 $width X $height 内缩放图片,安全框内没有像素的地方填充色, 使用此参数时可设置背景填充色 $bg_color = array(255,255,255)(红,绿,蓝, 透明度) 透明度(0不透明-127完全透明))
  83. 其它: 智能模能 缩放图像并载取图像的中间部分 $width X $height 像素大小
  84. $fit = 'force','scale','scale_fill' 时: 输出完整图像
  85. $fit = 图像方位值 时, 输出指定位置部分图像
  86. 字母与图像的对应关系如下:
  87. north_west north north_east
  88. west center east
  89. south_west south south_east
  90. */
  91. /**
  92. * @param int $width
  93. * @param int $height
  94. * @param string $fit
  95. * @param array $fill_color
  96. * @throws \ImagickException
  97. */
  98. public function resize_to($width = 100, $height = 100, $fit = 'center', $fill_color = array(255, 255, 255, 0))
  99. {
  100. switch ($fit) {
  101. case 'force':
  102. if ($this->type == 'gif') {
  103. $image = $this->image;
  104. $canvas = new Imagick();
  105. $images = $image->coalesceImages();
  106. foreach ($images as $frame) {
  107. $img = new Imagick();
  108. $img->readImageBlob($frame);
  109. $img->thumbnailImage($width, $height, false);
  110. $canvas->addImage($img);
  111. $canvas->setImageDelay($img->getImageDelay());
  112. }
  113. $image->destroy();
  114. $this->image = $canvas;
  115. } else {
  116. $this->image->thumbnailImage($width, $height, false);
  117. }
  118. break;
  119. case 'scale':
  120. if ($this->type == 'gif') {
  121. $image = $this->image;
  122. $images = $image->coalesceImages();
  123. $canvas = new Imagick();
  124. foreach ($images as $frame) {
  125. $img = new Imagick();
  126. $img->readImageBlob($frame);
  127. $img->thumbnailImage($width, $height, true);
  128. $canvas->addImage($img);
  129. $canvas->setImageDelay($img->getImageDelay());
  130. }
  131. $image->destroy();
  132. $this->image = $canvas;
  133. } else {
  134. $this->image->thumbnailImage($width, $height, true);
  135. }
  136. break;
  137. case 'scale_fill':
  138. $size = $this->image->getImagePage();
  139. $src_width = $size['width'];
  140. $src_height = $size['height'];
  141. $x = 0;
  142. $y = 0;
  143. $dst_width = $width;
  144. $dst_height = $height;
  145. if ($src_width * $height > $src_height * $width) {
  146. $dst_height = intval($width * $src_height / $src_width);
  147. $y = intval(($height - $dst_height) / 2);
  148. } else {
  149. $dst_width = intval($height * $src_width / $src_height);
  150. $x = intval(($width - $dst_width) / 2);
  151. }
  152. $image = $this->image;
  153. $canvas = new Imagick();
  154. $color = 'rgba(' . $fill_color[0] . ',' . $fill_color[1] . ',' . $fill_color[2] . ',' . $fill_color[3] . ')';
  155. if ($this->type == 'gif') {
  156. $images = $image->coalesceImages();
  157. foreach ($images as $frame) {
  158. $frame->thumbnailImage($width, $height, true);
  159. $draw = new ImagickDraw();
  160. $draw->composite($frame->getImageCompose(), $x, $y, $dst_width, $dst_height, $frame);
  161. $img = new Imagick();
  162. $img->newImage($width, $height, $color, 'gif');
  163. $img->drawImage($draw);
  164. $canvas->addImage($img);
  165. $canvas->setImageDelay($img->getImageDelay());
  166. $canvas->setImagePage($width, $height, 0, 0);
  167. }
  168. } else {
  169. $image->thumbnailImage($width, $height, true);
  170. $draw = new ImagickDraw();
  171. $draw->composite($image->getImageCompose(), $x, $y, $dst_width, $dst_height, $image);
  172. $canvas->newImage($width, $height, $color, $this->get_type());
  173. $canvas->drawImage($draw);
  174. $canvas->setImagePage($width, $height, 0, 0);
  175. }
  176. $image->destroy();
  177. $this->image = $canvas;
  178. break;
  179. default:
  180. $size = $this->image->getImagePage();
  181. $src_width = $size['width'];
  182. $src_height = $size['height'];
  183. // $crop_x = 0;
  184. // $crop_y = 0;
  185. $crop_w = $src_width;
  186. $crop_h = $src_height;
  187. if ($src_width * $height > $src_height * $width) {
  188. $crop_w = intval($src_height * $width / $height);
  189. } else {
  190. $crop_h = intval($src_width * $height / $width);
  191. }
  192. switch ($fit) {
  193. case 'north_west':
  194. $crop_x = 0;
  195. $crop_y = 0;
  196. break;
  197. case 'north':
  198. $crop_x = intval(($src_width - $crop_w) / 2);
  199. $crop_y = 0;
  200. break;
  201. case 'north_east':
  202. $crop_x = $src_width - $crop_w;
  203. $crop_y = 0;
  204. break;
  205. case 'west':
  206. $crop_x = 0;
  207. $crop_y = intval(($src_height - $crop_h) / 2);
  208. break;
  209. case 'center':
  210. $crop_x = intval(($src_width - $crop_w) / 2);
  211. $crop_y = intval(($src_height - $crop_h) / 2);
  212. break;
  213. case 'east':
  214. $crop_x = $src_width - $crop_w;
  215. $crop_y = intval(($src_height - $crop_h) / 2);
  216. break;
  217. case 'south_west':
  218. $crop_x = 0;
  219. $crop_y = $src_height - $crop_h;
  220. break;
  221. case 'south':
  222. $crop_x = intval(($src_width - $crop_w) / 2);
  223. $crop_y = $src_height - $crop_h;
  224. break;
  225. case 'south_east':
  226. $crop_x = $src_width - $crop_w;
  227. $crop_y = $src_height - $crop_h;
  228. break;
  229. default:
  230. $crop_x = intval(($src_width - $crop_w) / 2);
  231. $crop_y = intval(($src_height - $crop_h) / 2);
  232. }
  233. $image = $this->image;
  234. $canvas = new Imagick();
  235. if ($this->type == 'gif') {
  236. $images = $image->coalesceImages();
  237. foreach ($images as $frame) {
  238. $img = new Imagick();
  239. $img->readImageBlob($frame);
  240. $img->cropImage($crop_w, $crop_h, $crop_x, $crop_y);
  241. $img->thumbnailImage($width, $height, true);
  242. $canvas->addImage($img);
  243. $canvas->setImageDelay($img->getImageDelay());
  244. $canvas->setImagePage($width, $height, 0, 0);
  245. }
  246. } else {
  247. $image->cropImage($crop_w, $crop_h, $crop_x, $crop_y);
  248. $image->thumbnailImage($width, $height, true);
  249. $canvas->addImage($image);
  250. $canvas->setImagePage($width, $height, 0, 0);
  251. }
  252. $image->destroy();
  253. $this->image = $canvas;
  254. }
  255. }
  256. // 添加水印图片
  257. /**
  258. * @param $path
  259. * @param int $x
  260. * @param int $y
  261. * @throws \ImagickException
  262. */
  263. public function add_watermark($path, $x = 0, $y = 0)
  264. {
  265. $watermark = new Imagick($path);
  266. $draw = new ImagickDraw();
  267. $draw->composite($watermark->getImageCompose(), $x, $y, $watermark->getImageWidth(), $watermark->getimageheight(), $watermark);
  268. if ($this->type == 'gif') {
  269. $image = $this->image;
  270. $canvas = new Imagick();
  271. // $images = $image->coalesceImages();
  272. foreach ($image as $frame) {
  273. $img = new Imagick();
  274. $img->readImageBlob($frame);
  275. $img->drawImage($draw);
  276. $canvas->addImage($img);
  277. $canvas->setImageDelay($img->getImageDelay());
  278. }
  279. $image->destroy();
  280. $this->image = $canvas;
  281. } else {
  282. $this->image->drawImage($draw);
  283. }
  284. }
  285. // 添加水印文字
  286. /**
  287. * @param $text
  288. * @param int $x
  289. * @param int $y
  290. * @param int $angle
  291. * @param array $style
  292. */
  293. public function add_text($text, $x = 0, $y = 0, $angle = 0, $style = array())
  294. {
  295. $draw = new ImagickDraw();
  296. if (isset($style['font'])) $draw->setFont($style['font']);
  297. if (isset($style['font_size'])) $draw->setFontSize($style['font_size']);
  298. if (isset($style['fill_color'])) $draw->setFillColor($style['fill_color']);
  299. if (isset($style['under_color'])) $draw->setTextUnderColor($style['under_color']);
  300. if ($this->type == 'gif') {
  301. foreach ($this->image as $frame) {
  302. $frame->annotateImage($draw, $x, $y, $angle, $text);
  303. }
  304. } else {
  305. $this->image->annotateImage($draw, $x, $y, $angle, $text);
  306. }
  307. }
  308. // 保存到指定路径
  309. /**
  310. * @param $path
  311. */
  312. public function save_to($path)
  313. {
  314. if ($this->type == 'gif') {
  315. $this->image->writeImages($path, true);
  316. } else {
  317. $this->image->writeImage($path);
  318. }
  319. }
  320. // 输出图像
  321. /**
  322. * @param bool $header
  323. * @throws \ImagickException
  324. */
  325. public function output($header = true)
  326. {
  327. if ($header) header('Content-type: ' . $this->type);
  328. echo $this->image->getImagesBlob();
  329. }
  330. /**
  331. * @return mixed
  332. */
  333. public function get_width()
  334. {
  335. $size = $this->image->getImagePage();
  336. return $size['width'];
  337. }
  338. /**
  339. * @return mixed
  340. */
  341. public function get_height()
  342. {
  343. $size = $this->image->getImagePage();
  344. return $size['height'];
  345. }
  346. // 设置图像类型, 默认与源类型一致
  347. /**
  348. * @param string $type
  349. */
  350. public function set_type($type = 'png')
  351. {
  352. $this->type = $type;
  353. $this->image->setImageFormat($type);
  354. }
  355. // 获取源图像类型
  356. /**
  357. * @return null
  358. */
  359. public function get_type()
  360. {
  361. return $this->type;
  362. }
  363. // 当前对象是否为图片
  364. /**
  365. * @return bool
  366. */
  367. public function is_image()
  368. {
  369. if ($this->image)
  370. return true;
  371. else
  372. return false;
  373. }
  374. /**
  375. * @param int $width
  376. * @param int $height
  377. * @param bool $fit
  378. */
  379. public function thumbnail($width = 100, $height = 100, $fit = true)
  380. {
  381. $this->image->thumbnailImage($width, $height, $fit);
  382. } // 生成缩略图 $fit为真时将保持比例并在安全框 $width X $height 内生成缩略图片
  383. /*
  384. 添加一个边框
  385. $width: 左右边框宽度
  386. $height: 上下边框宽度
  387. $color: 颜色: RGB 颜色 'rgb(255,0,0)' 或 16进制颜色 '#FF0000' 或颜色单词 'white'/'red'...
  388. */
  389. /**
  390. * @param $width
  391. * @param $height
  392. * @param string $color
  393. */
  394. public function border($width, $height, $color = 'rgb(220, 220, 220)')
  395. {
  396. $color = new ImagickPixel();
  397. $color->setColor($color);
  398. $this->image->borderImage($color, $width, $height);
  399. }
  400. /**
  401. * @param $radius
  402. * @param $sigma
  403. */
  404. public function blur($radius, $sigma)
  405. {
  406. $this->image->blurImage($radius, $sigma);
  407. } // 模糊
  408. /**
  409. * @param $radius
  410. * @param $sigma
  411. */
  412. public function gaussian_blur($radius, $sigma)
  413. {
  414. $this->image->gaussianBlurImage($radius, $sigma);
  415. } // 高斯模糊
  416. /**
  417. * @param $radius
  418. * @param $sigma
  419. * @param $angle
  420. */
  421. public function motion_blur($radius, $sigma, $angle)
  422. {
  423. $this->image->motionBlurImage($radius, $sigma, $angle);
  424. } // 运动模糊
  425. /**
  426. * @param $radius
  427. */
  428. public function radial_blur($radius)
  429. {
  430. $this->image->radialBlurImage($radius);
  431. } // 径向模糊
  432. /**
  433. * @param null $type
  434. */
  435. public function add_noise($type = null)
  436. {
  437. $this->image->addNoiseImage($type == null ? imagick::NOISE_IMPULSE : $type);
  438. } // 添加噪点
  439. /**
  440. * @param $black_point
  441. * @param $gamma
  442. * @param $white_point
  443. */
  444. public function level($black_point, $gamma, $white_point)
  445. {
  446. $this->image->levelImage($black_point, $gamma, $white_point);
  447. } // 调整色阶
  448. /**
  449. * @param $brightness
  450. * @param $saturation
  451. * @param $hue
  452. */
  453. public function modulate($brightness, $saturation, $hue)
  454. {
  455. $this->image->modulateImage($brightness, $saturation, $hue);
  456. } // 调整亮度、饱和度、色调
  457. /**
  458. * @param $radius
  459. * @param $sigma
  460. */
  461. public function charcoal($radius, $sigma)
  462. {
  463. $this->image->charcoalImage($radius, $sigma);
  464. } // 素描
  465. /**
  466. * @param $radius
  467. */
  468. public function oil_paint($radius)
  469. {
  470. $this->image->oilPaintImage($radius);
  471. } // 油画效果
  472. public function flop()
  473. {
  474. $this->image->flopImage();
  475. } // 水平翻转
  476. public function flip()
  477. {
  478. $this->image->flipImage();
  479. } // 垂直翻转
  480. }
  481. function caclSize($src, $width = null, $height = null)
  482. {
  483. if (!isset($width) && !isset($height))
  484. return false;
  485. if (isset($width) && $width <= 0)
  486. return false;
  487. if (isset($height) && $height <= 0)
  488. return false;
  489. $size = getimagesize($src);
  490. if (!$size)
  491. return false;
  492. list($src_w, $src_h, $src_type) = $size;
  493. if (!isset($width))
  494. $width = $src_w * ($height / $src_h);
  495. if (!isset($height)) {
  496. $height = $src_h * ($width / $src_w);
  497. }
  498. return [$width, $height];
  499. }
  500. /**
  501. * 生成缩略图函数(支持图片格式:gif、jpeg、png和bmp)
  502. * @author ruxing.li
  503. * @param string $src 源图片路径
  504. * @param int $width 缩略图宽度(只指定高度时进行等比缩放)
  505. * @param int $height 缩略图高度(只指定宽度时进行等比缩放)
  506. * @param string $filename 保存路径(不指定时直接输出到浏览器)
  507. * @return bool
  508. */
  509. function mkThumbnail($src, $width = null, $height = null, $filename = null)
  510. {
  511. if (!isset($width) && !isset($height))
  512. return false;
  513. if (isset($width) && $width <= 0)
  514. return false;
  515. if (isset($height) && $height <= 0)
  516. return false;
  517. $size = getimagesize($src);
  518. if (!$size)
  519. return false;
  520. list($src_w, $src_h, $src_type) = $size;
  521. $src_mime = $size['mime'];
  522. switch ($src_type) {
  523. case 1 :
  524. $img_type = 'gif';
  525. break;
  526. case 2 :
  527. $img_type = 'jpeg';
  528. break;
  529. case 3 :
  530. $img_type = 'png';
  531. break;
  532. case 15 :
  533. $img_type = 'wbmp';
  534. break;
  535. default :
  536. return false;
  537. }
  538. $exif=[];
  539. try{
  540. if ($src_type==2&&function_exists('exif_read_data')) {
  541. $exif = exif_read_data($src);
  542. }
  543. }catch (\Exception $e){
  544. }
  545. $imagecreatefunc = 'imagecreatefrom' . $img_type;
  546. $src_img = $imagecreatefunc($src);
  547. if (isset($exif['Orientation']) && !empty($exif['Orientation'])) {
  548. switch ($exif['Orientation']) {
  549. case 8:
  550. $src_img = imagerotate($src_img, 90, 255);
  551. $tmp = $src_w;
  552. $src_w = $src_h;
  553. $src_h = $tmp;
  554. break;
  555. case 3:
  556. $src_img = imagerotate($src_img, 180, 255);
  557. break;
  558. case 6:
  559. $src_img = imagerotate($src_img, -90, 255);
  560. $tmp = $src_w;
  561. $src_w = $src_h;
  562. $src_h = $tmp;
  563. break;
  564. }
  565. }
  566. if (!isset($width))
  567. $width = $src_w * ($height / $src_h);
  568. if (!isset($height)) {
  569. $height = $src_h * ($width / $src_w);
  570. }
  571. $dest_img = imagecreatetruecolor($width, $height);
  572. $imagefunc = 'image' . $img_type;
  573. imagesavealpha($src_img, true);//这里很重要 意思是不要丢了$sourePic图像的透明色;
  574. imagealphablending($dest_img, false);//这里很重要,意思是不合并颜色,直接用$img图像颜色替换,包括透明色;
  575. imagesavealpha($dest_img, true);//这里很重要,意思是不要丢了$thumb图像的透明色;
  576. if (imagecopyresampled($dest_img, $src_img, 0, 0, 0, 0, $width, $height, $src_w, $src_h)) {
  577. header('Content-Type: ' . $src_mime);
  578. $imagefunc($dest_img);
  579. }
  580. imagedestroy($src_img);
  581. imagedestroy($dest_img);
  582. }
  583. function run()
  584. {
  585. if(!isset($_GET['file'])){
  586. $file = '/blank.png';
  587. }else{
  588. $file = $_GET['file'];
  589. }
  590. $filePath = "." . $file;
  591. if (!file_exists($filePath)) {
  592. $filePath = './files/blank.png';
  593. }
  594. if(!isset($_GET['x-oss-process'])){
  595. header('Content-Type: image/jpeg');
  596. readfile($filePath);
  597. exit();
  598. }else{
  599. $param = $_GET['x-oss-process'];
  600. }
  601. $width = null;
  602. $height = null;
  603. //此处style名称与阿里云的对应,已经添加几个内置样式,TODO 增加阿里云的处理规则https://help.aliyun.com/document_detail/44687.html?spm=5176.doc44695.6.945.5m3Qxo
  604. if ((preg_match('/style\/(\d+)([w|h])+/', $param, $match))) {
  605. if ($match[2] == 'w') {
  606. $width = $match[1];
  607. }
  608. if ($match[2] == 'h') {
  609. $height = $match[1];
  610. }
  611. } else {
  612. throw new \Exception('x-oss-process参数格式错误');
  613. }
  614. if (extension_loaded('imagick')) {
  615. list($width, $height) = caclSize($filePath, $width, $height);
  616. $object = new ImagickUtils();
  617. $object->open($filePath);
  618. $dirDegree = intval($object->getImage()->getImageProperty("exif:Orientation"));
  619. switch ($dirDegree) {
  620. case \Imagick::ORIENTATION_LEFTBOTTOM:
  621. $object->getImage()->rotateImage(new ImagickPixel(), -90);
  622. $tmp = $width;
  623. $width = $height;
  624. $height = $tmp;
  625. break;
  626. case \Imagick::ORIENTATION_BOTTOMRIGHT:
  627. $object->getImage()->rotateImage(new ImagickPixel(), 180);
  628. break;
  629. case \Imagick::ORIENTATION_RIGHTTOP:
  630. $object->getImage()->rotateImage(new ImagickPixel(), 90);
  631. $tmp = $width;
  632. $width = $height;
  633. $height = $tmp;
  634. break;
  635. }
  636. $object->resize_to($width, $height);
  637. $object->output();
  638. //return;
  639. } else {
  640. mkThumbnail($filePath, $width, $height);
  641. }
  642. exit();
  643. }
  644. //var_dump($_GET);
  645. run();