DrawImage.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. namespace App\Traits;
  3. use App\PrintPartImage;
  4. use Endroid\QrCode\QrCode;
  5. use Illuminate\Support\Facades\File;
  6. use Illuminate\Support\Str;
  7. use Intervention\Image\Facades\Image;
  8. use Picqer\Barcode\BarcodeGeneratorJPG;
  9. trait DrawImage
  10. {
  11. public function draw($orderPackage, $template, $img = null)
  12. {
  13. if (!$img) $img = $this->getBgImg($template);
  14. if (!$template) return $img;
  15. $items = array_filter($template->value, function ($item) {
  16. return $item['type'] != 'bg';
  17. });
  18. $array = array_filter($template->value, function ($item) {
  19. return $item['type'] == 'bg';
  20. });
  21. $array = array_shift($array);
  22. // 按照模型比列修改image
  23. $items = (function () use ($items, $img, $array) {
  24. $heightScale = $img->getHeight() / $array['height'];
  25. $weightScale = $img->getWidth() / $array['width'];
  26. foreach ($items as $key => &$item) {
  27. if (array_key_exists('width', $item)) $item['width'] = intval($weightScale * floatval($item['width']));
  28. if (array_key_exists('height', $item)) $item['height'] = intval($heightScale * floatval($item['height']));
  29. if (array_key_exists('left', $item)) $item['left'] = intval($weightScale * floatval($item['left']));
  30. if (array_key_exists('top', $item)) $item['top'] = intval($heightScale * floatval($item['top']));
  31. if (array_key_exists('font-size', $item)) $item['font-size'] = intval($heightScale * floatval($item['font-size']));
  32. if (array_key_exists('border-width', $item)) $item['border-width'] = intval($heightScale * floatval($item['border-width']));
  33. $item['heightScale'] = $heightScale;
  34. $item['weightScale'] = $weightScale;
  35. }
  36. return $items;
  37. })();
  38. // 根据下标排序
  39. usort($items, function ($a, $b) {
  40. return $a['z_index'] > $b['z_index'] ? 1 : -1;
  41. });
  42. foreach ($items as $item) {
  43. switch ($item['type']) {
  44. case 'textBox':
  45. $this->setTextBox($img, $item, $orderPackage);
  46. break;
  47. case 'image':
  48. $this->setImage($img, $item);
  49. break;
  50. case 'qRCode':
  51. $this->setQRCode($img, $item, $orderPackage);
  52. break;
  53. case 'stripeCode':
  54. $this->setStripeCode($img, $item, $orderPackage);
  55. break;
  56. default:
  57. break;
  58. }
  59. }
  60. return $img;
  61. }
  62. private function getBgImg($template): \Intervention\Image\Image
  63. {
  64. $bg = array_filter($template->value, function ($item) {
  65. return $item['type'] == 'bg';
  66. });
  67. $values = array_shift($bg);
  68. return Image::canvas($values['width'], $values['height'], '#fff');
  69. }
  70. public function setTextBox($image, $params, $orderPackages): \Intervention\Image\Image
  71. {
  72. $text = $this->getValue($params, $orderPackages);
  73. $bgImage = Image::canvas($params['width'], $params['height'], '#000');
  74. $img = $this->getTextBox($params, $text);
  75. $bgImage = $bgImage->insert($img, 'top-left', $params['border-width'], $params['border-width']);
  76. return $image->insert($bgImage, 'top-left', $params['left'] ?? 0, $params['top'] ?? 0);
  77. }
  78. public function setStripeCode($img, $params, $orderPackages)
  79. {
  80. $code = $this->getValue($params, $orderPackages);
  81. $bgImage = Image::canvas(404, 92, '#fff');
  82. $item = [
  83. 'width' => 404, 'height' => 20,
  84. 'justify-content' => 'center', 'align-items' => 'center',
  85. 'text' => $code, 'border-width' => 0,
  86. 'top' => 58, 'left' => 0, 'font-size' => 20
  87. ];
  88. $textBox = $this->getTextBox($item, $code);
  89. $bgImage->insert($textBox, 'top-left', 0, 68);
  90. $generatorPng = new BarcodeGeneratorJPG();
  91. $barCode = $generatorPng->getBarcode($code, $generatorPng::TYPE_CODE_128_A, 1, 50);
  92. $stripeCode = Image::make($barCode)->resize(382, 50);
  93. $stripeCode_weight = 404 * $params['scale'] * $params['weightScale'];
  94. $stripeCode_height = 92 * $params['scale'] * $params['heightScale'];
  95. $bgImage->insert($stripeCode, 'top-left', 6, 9)->resize($stripeCode_weight, $stripeCode_height);
  96. $left = $params['left'] + ((404 * (1- $params['scale'])) * $params['weightScale']) / 2;
  97. $top = $params['top'] + ((92 * (1-$params['scale'])) * $params['heightScale']) / 2;
  98. return $img->insert($bgImage, 'top-left', intval($left), intval($top));
  99. }
  100. public function setImage($img, $params)
  101. {
  102. $printPartImage = PrintPartImage::query()->where('name', $params['text'])->first();
  103. $path = ['app', 'public'];
  104. $path = join(DIRECTORY_SEPARATOR, $path);
  105. $dirPath = storage_path($path);
  106. $uri = $printPartImage->file['url'];
  107. $uri = str_replace('/', DIRECTORY_SEPARATOR, $uri);
  108. $path = $dirPath . $uri . '.' . $printPartImage->file['type'];
  109. $image = Image::make(File::get($path));
  110. $image->resize($params['width'], $params['height']);
  111. $img->insert($image, 'top-left', $params['left'], $params['top']);
  112. return $img;
  113. }
  114. public function getTextBox($params, $text): \Intervention\Image\Image
  115. {
  116. $bgImg = Image::canvas($params['width'], $params['height'], '#fff');
  117. if ($params['border-width'] !== 0) {
  118. $borderWidth = $params['border-width'] * 2;
  119. $paddingBgImage = Image::canvas($params['width'], $params['height'], '#000');
  120. $paddingImage = Image::canvas($params['width'] - $borderWidth, $params['height'] - $borderWidth, '#fff');
  121. $paddingBgImage->insert($paddingImage, 'top-left', intval($params['border-width'] / 2), intval($params['border-width'] / 2));
  122. $bgImg->insert($paddingBgImage);
  123. }
  124. $path_arr = ['fonts', 'simsun.ttc'];
  125. $x = $params['width'] / 2;
  126. if ($params['justify-content'] == 'center') $x = $params['width'] / 2;
  127. if ($params['justify-content'] == 'flex-start') $x = $params['width'] / 2;
  128. $y = $params['height'] / 2;
  129. if ($params['align-items'] == 'center') $y = $params['height'] / 2;
  130. if ($params['align-items'] == 'flex-start') $y = 0;
  131. if ($params['align-items'] == 'flex-end') $y = ($params['height'] - 22) / 2;
  132. // 字体路径
  133. $path = app('path.public') . (DIRECTORY_SEPARATOR . join(DIRECTORY_SEPARATOR, $path_arr));
  134. $aligns = [
  135. 'flex-start' => 'right',
  136. 'center' => 'center'
  137. ];
  138. $vAligns = [
  139. 'center' => 'center',
  140. 'flex-start' => 'top',
  141. 'flex-end' => 'bottom',
  142. ];
  143. return $bgImg->text($text, intval($x), intval($y), function ($font) use ($params, $path, $aligns, $vAligns) {
  144. $font->file($path);
  145. $font->size($params['font-size'] ?? 24);
  146. $font->align($aligns[$params['justify-content']]); // 水平对齐方式
  147. // center
  148. // right
  149. $font->valign($vAligns[$params['align-items']]); // 垂直对齐方式
  150. // center
  151. // middle
  152. // top
  153. // bottom
  154. });
  155. }
  156. public function setQRCode($img, $params, $orderPackages)
  157. {
  158. $text = $this->getValue($params, $orderPackages);
  159. $qrCode = new QrCode($text);
  160. $qrCode->setSize(100);
  161. $qrCode->setMargin(0);
  162. $scale = $params['scale'] ?? 1;
  163. return $img->insert(Image::make($qrCode->writeString())->resize(100 * $scale * $params['weightScale'], 100 * $scale * $params['heightScale']), 'top-left', $params['left'], $params['top']);
  164. }
  165. // 获取图片
  166. public function getImage($base64, &$path): \Intervention\Image\Image
  167. {
  168. $path = $this->getImagePath();
  169. $img = Image::make($base64);
  170. $img->save($path); // 图片保存
  171. return $img;
  172. }
  173. // 获取打印图片的路径
  174. private function getImagePath(): string
  175. {
  176. $img_name = Str::uuid() . '.jpg';
  177. $path_arr = ['app', 'public', 'print'];
  178. $dir_path = implode(DIRECTORY_SEPARATOR, $path_arr);
  179. if (!file_exists(storage_path($dir_path))) mkdir(storage_path($dir_path));
  180. $path_arr[] = 'image';
  181. $dir_path = implode(DIRECTORY_SEPARATOR, $path_arr);
  182. if (!file_exists(storage_path($dir_path))) mkdir(storage_path($dir_path));
  183. $path_arr[] = $img_name;
  184. return storage_path(implode(DIRECTORY_SEPARATOR, $path_arr));
  185. }
  186. // 保存至保存图片路径下
  187. public function saveImage($image, &$path)
  188. {
  189. $path = $this->getImagePath();
  190. $image->save($path); // 图片保存
  191. }
  192. // 根据图片路径读取而base64编码的图片
  193. public function readImageBase64($path): string
  194. {
  195. if ($fp = fopen($path, "rp", 0)) {
  196. $gambar = fread($fp, filesize($path));
  197. fclose($fp);
  198. return chunk_split(base64_encode($gambar));
  199. }
  200. return "";
  201. }
  202. // 通过设置获取所需参数
  203. private function getValue($params, $orderPackage = [], $k = 'text'): string
  204. {
  205. $key = strtolower($params[$k]);
  206. if ($value = strstr($key, '$')) return $orderPackage[ltrim($value, '$')] ?? '';
  207. else return $params[$k];
  208. }
  209. }