DrawImage.php 8.4 KB

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