ImagickClass.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2022-06-24
  6. * Time: 12:23
  7. */
  8. namespace app\model\image;
  9. /**
  10. * Imagick 图片策略类
  11. * Class ImagickClass
  12. * @package app\model\upload
  13. */
  14. class ImagickClass
  15. {
  16. /**
  17. * 获取图片实例
  18. * @param $path
  19. * @return mixed
  20. */
  21. public function open($path){
  22. $image = (new ImagickService())->open($path);
  23. return $image;
  24. }
  25. /**
  26. * 图片保存
  27. * @param $image
  28. * @param $new_file
  29. */
  30. public function save($image, $new_file, $compress = ''){
  31. return $image->save_to($new_file, $compress);
  32. }
  33. public function getImageParam($image){
  34. $size = $image->getImageParam();
  35. return [
  36. 'width' => $size['width'],
  37. 'height' => $size['height']
  38. ];
  39. }
  40. /**
  41. * 文字水印
  42. * @param $text
  43. * @param $x
  44. * @param $y
  45. * @param $size
  46. * @param $color
  47. * @param $align
  48. * @param $valign
  49. * @param $angle
  50. * @return mixed
  51. */
  52. public function textWater($image, $text, $x, $y, $size, $color, $align, $valign, $angle){
  53. // $spec = $this->getImageParam($image);
  54. // $x = $spec['width']/2;
  55. // $y = $spec['height']/2;
  56. $style = array(
  57. 'font' => PUBLIC_PATH.'static/font/Microsoft.ttf',
  58. 'font_size' => $size,
  59. // 'fill_color' => $size,
  60. // 'under_color' => $size,
  61. );
  62. $image->add_text($text, $x, $y, $angle = 0, $style);
  63. return $image;
  64. }
  65. /**
  66. * 图片水印
  67. * @param $water_path
  68. * @param $water_position
  69. * @param $x
  70. * @param $y
  71. * @return mixed
  72. */
  73. public function imageWater($image, $water_path, $watermark_opacity, $water_rotate, $water_position, $x, $y){
  74. $image->add_watermark($water_path, $x, $y);
  75. return $image;
  76. }
  77. /**
  78. * 缩略图
  79. * @param $width
  80. * @param $height
  81. * @param $fit
  82. * @param $fill_color
  83. */
  84. public function thumb($image, $width, $height, $fit = 'center', $fill_color = 'ffffff'){
  85. if(!empty($fit)){
  86. $fit = 'force';
  87. }else{
  88. $fit = 'scale';
  89. }
  90. $image->resize_to($width, $height, $fit);
  91. return $image;
  92. }
  93. }