ImageService.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. * 图片处理服务类
  11. */
  12. class ImageService
  13. {
  14. public $imageClass;
  15. public $image;
  16. public $width;
  17. public $height;
  18. public function __construct($driver = 'gd')
  19. {
  20. //可以考虑设计成策略(author 周)
  21. switch($driver){
  22. case 'gd':
  23. $this->imageClass = new GdClass();
  24. break;
  25. case 'imagick':
  26. $this->imageClass = new ImagickClass();
  27. break;
  28. }
  29. // return $this;
  30. }
  31. /**
  32. * 获取图片实例
  33. * @param $path
  34. * @return mixed
  35. */
  36. public function open($path){
  37. $this->image = $this->imageClass->open($path);
  38. $this->getImageParam();
  39. return $this;
  40. }
  41. public function getImageParam(){
  42. $param = $this->imageClass->getImageParam($this->image);
  43. $this->width = $param['width'];
  44. $this->height = $param['height'];
  45. }
  46. /**
  47. * 图片保存
  48. * @param $image
  49. * @param $new_file
  50. */
  51. public function save($new_file, $compress = 90){
  52. return $this->imageClass->save($this->image, $new_file, $compress);
  53. }
  54. /**
  55. * 文字水印
  56. * @param $text
  57. * @param $x
  58. * @param $y
  59. * @param $size
  60. * @param $color
  61. * @param $align
  62. * @param $valign
  63. * @param $angle
  64. * @return mixed
  65. */
  66. public function textWater($text, $x, $y, $size, $color, $align, $valign, $angle){
  67. $this->image = $this->imageClass->textWater($this->image, $text, $x, $y, $size, $color, $align, $valign, $angle);
  68. return $this;
  69. }
  70. /**
  71. * 图片水印
  72. * @param $water_path
  73. * @param $water_position
  74. * @param $x
  75. * @param $y
  76. * @return mixed
  77. */
  78. public function imageWater($water_path, $water_opacity, $water_rotate, $water_position, $x, $y){
  79. $this->image = $this->imageClass->imageWater($this->image, $water_path, $water_opacity, $water_rotate, $water_position, $x, $y);
  80. return $this;
  81. }
  82. /**
  83. * 缩略图
  84. * @param $width
  85. * @param $height
  86. * @param $fit
  87. * @param $fill_color
  88. */
  89. public function thumb($width, $height, $fit = 'center', $fill_color = 'ffffff'){
  90. $this->image = $this->imageClass->thumb($this->image, $width, $height, $fit, $fill_color);
  91. return $this;
  92. }
  93. }