applyImage.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * User: lang
  4. * Date: 2024/11/26
  5. * Time: 11:15
  6. */
  7. use Kkokk\Poster\Image\Gd\Canvas as GdCanvas;
  8. use Kkokk\Poster\Image\Gd\Image as GdImage;
  9. use Kkokk\Poster\Image\Gd\Text as GdText;
  10. use Kkokk\Poster\Image\Gd\ImageText as GdImageText;
  11. use Kkokk\Poster\Image\Imagick\Canvas as ImagickCanvas;
  12. use Kkokk\Poster\Image\Imagick\Image as ImagickImage;
  13. use Kkokk\Poster\Image\Imagick\Text as ImagickText;
  14. use Kkokk\Poster\Image\Imagick\ImageText as ImagickImageText;
  15. use Kkokk\Poster\Facades\Poster;
  16. require '../../vendor/autoload.php';
  17. $file = 'C:\\\\\\\\///Users\\\////32822\Pictures////\\\\\\\\\\' . '朝天门.jpg';
  18. // 设置路径
  19. $sourcePath = __DIR__ . "/../poster/test7.png"; // 原始图片所在目录
  20. $targetPath = __DIR__ . "/../poster/src/output1.png"; // 处理后的图片保存目录
  21. $maskPath = __DIR__ . "/../poster/mask/mask1.png"; // 蒙版图片路径
  22. $maskCanvas = Poster::config(['type' => 'png'])
  23. ->buildBg(638, 826, [
  24. 'color' => [
  25. [255, 255, 255],
  26. [0, 0, 0],
  27. [255, 255, 255],
  28. ],
  29. ])->getCanvas();
  30. // $file = 'https://img2.baidu.com/it/u=1310029438,409566289&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1541';
  31. $imageText = (new ImagickImageText())
  32. ->setMaxWidth(300)
  33. ->setFontAlign('left')
  34. ->addText((new ImagickText())
  35. ->setText("床前明月光,")
  36. ->setFontSize(16)
  37. ->setFontColor('#000000')
  38. )
  39. ->addText((new ImagickText())
  40. ->setText("疑似地上霜。")
  41. ->setFontSize(22)
  42. ->setFontColor('#00ff00')
  43. )
  44. ->addText((new ImagickText())
  45. ->setText("举头望明月,")
  46. ->setFontSize(12)
  47. ->setFontColor('#0000ff')
  48. )
  49. ->addText((new ImagickText())
  50. ->setText("低头思故乡。")
  51. ->setFontSize(28)
  52. ->setFontColor('#ff0000')
  53. ->setLineHeight(40)
  54. )
  55. ->addImage((new ImagickImage($file))->scale(50, 30))
  56. ->addText((new ImagickText())
  57. ->setText("图")
  58. ->setFontSize(28)
  59. ->setFontColor('#ff0000')
  60. );
  61. // $imageText = (new GdImageText())
  62. // ->setMaxWidth(300)
  63. // ->setFontAlign('left')
  64. // ->addText((new GdText())
  65. // ->setText("床前明月光,")
  66. // ->setFontSize(16)
  67. // ->setFontColor('#000000')
  68. // )
  69. // ->addText((new GdText())
  70. // ->setText("疑似地上霜。")
  71. // ->setFontSize(22)
  72. // ->setFontColor('#00ff00')
  73. // )
  74. // ->addText((new GdText())
  75. // ->setText("举头望明月,")
  76. // ->setFontSize(12)
  77. // ->setFontColor('#0000ff')
  78. // )
  79. // ->addText((new GdText())
  80. // ->setText("低头思故乡。")
  81. // ->setFontSize(28)
  82. // ->setFontColor('#ff0000')
  83. // ->setLineHeight(40)
  84. // )
  85. // ->addImage((new GdImage($file))->scale(50, 30))
  86. // ->addText((new GdText())
  87. // ->setText("图")
  88. // ->setFontSize(28)
  89. // ->setFontColor('#ff0000')
  90. // );
  91. $canvas = Poster::extension('imagick')
  92. ->config(['type' => 'png'])
  93. // ->buildImDst($file)
  94. ->buildBg(638, 826, [
  95. 'color' => [
  96. [255, 0, 0],
  97. [255, 125, 0],
  98. [255, 255, 0],
  99. [0, 255, 0],
  100. [0, 255, 255],
  101. [0, 0, 255],
  102. [255, 0, 255]
  103. ],
  104. 'alpha' => 50,
  105. 'to' => 'bottom',
  106. 'radius' => '40',
  107. ], true, 'center', 'center', 0, 0,
  108. function (\Kkokk\Poster\Image\Builder $builder) {
  109. $builder->buildLine(10, 100, 100, 100, [0, 0, 0, 1]);
  110. $builder->buildArc(100, 100, 100, 100, 0, 180, [0, 0, 0, 1]);
  111. $builder->buildText('明月几时有,把酒问青天,不知天上宫阙,今夕是何年。', 10, 200, 28,
  112. [0, 0, 0], 0, '', 6, 10);
  113. $builder->buildText('明月几时有,把酒问青天,不知天上宫阙,今夕是何年。', 10, 40);
  114. $builder->buildQr('http://www.520yummy.com/poster-doc/guide/', 'center', 'center');
  115. })
  116. ->buildText($imageText, 300, 300);
  117. // ->crop(0, 0, 500, 500)
  118. $canvas->getCanvas()->applyMask($maskCanvas)->getStream();