| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- <?php
- namespace App\Traits;
- use App\PrintPartImage;
- use Endroid\QrCode\QrCode;
- use Illuminate\Support\Facades\File;
- use Illuminate\Support\Str;
- use Intervention\Image\Facades\Image;
- use Picqer\Barcode\BarcodeGeneratorJPG;
- trait DrawImage
- {
- public function draw($orderPackage, $template, $img = null)
- {
- if (!$img) $img = $this->getBgImg($template);
- if (!$template) return $img;
- $items = array_filter($template->value, function ($item) {
- return $item['type'] != 'bg';
- });
- $array = array_filter($template->value, function ($item) {
- return $item['type'] == 'bg';
- });
- $array = array_shift($array);
- // 按照模型比列修改image
- $items = (function () use ($items, $img, $array) {
- $heightScale = $img->getHeight() / $array['height'];
- $weightScale = $img->getWidth() / $array['width'];
- // foreach ($items as $key => &$item) {
- foreach ($items as &$item) {
- if (array_key_exists('width', $item)) $item['width'] = intval($weightScale * floatval($item['width']));
- if (array_key_exists('height', $item)) $item['height'] = intval($heightScale * floatval($item['height']));
- if (array_key_exists('left', $item)) $item['left'] = intval($weightScale * floatval($item['left']));
- if (array_key_exists('top', $item)) $item['top'] = intval($heightScale * floatval($item['top']));
- if (array_key_exists('font-size', $item)) $item['font-size'] = intval($heightScale * floatval($item['font-size']));
- if (array_key_exists('border-width', $item)) $item['border-width'] = intval($heightScale * floatval($item['border-width']));
- $item['heightScale'] = $heightScale;
- $item['weightScale'] = $weightScale;
- }
- return $items;
- })();
- // 根据下标排序
- usort($items, function ($a, $b) {
- return $a['z_index'] > $b['z_index'] ? 1 : -1;
- });
- foreach ($items as $item) {
- switch ($item['type']) {
- case 'textBox':
- $this->setTextBox($img, $item, $orderPackage);
- break;
- case 'image':
- $this->setImage($img, $item);
- break;
- case 'qRCode':
- $this->setQRCode($img, $item, $orderPackage);
- break;
- case 'stripeCode':
- $this->setStripeCode($img, $item, $orderPackage);
- break;
- default:
- break;
- }
- }
- return $img;
- }
- private function getBgImg($template): \Intervention\Image\Image
- {
- $bg = array_filter($template->value, function ($item) {
- return $item['type'] == 'bg';
- });
- $values = array_shift($bg);
- return Image::canvas($values['width'], $values['height'], '#fff');
- }
- public function setTextBox($image, $params, $orderPackages): \Intervention\Image\Image
- {
- $text = $this->getValue($params, $orderPackages);
- $bgImage = Image::canvas($params['width'], $params['height'], '#000');
- $img = $this->getTextBox($params, $text);
- $bgImage = $bgImage->insert($img, 'top-left', $params['border-width'], $params['border-width']);
- return $image->insert($bgImage, 'top-left', $params['left'] ?? 0, $params['top'] ?? 0);
- }
- public function setStripeCode($img, $params, $orderPackages)
- {
- $code = $this->getValue($params, $orderPackages);
- $bgImage = Image::canvas(404, 92, '#fff');
- $item = [
- 'width' => 404, 'height' => 20,
- 'justify-content' => 'center', 'align-items' => 'center',
- 'text' => $code, 'border-width' => 0,
- 'top' => 58, 'left' => 0, 'font-size' => 20
- ];
- $textBox = $this->getTextBox($item, $code);
- $bgImage->insert($textBox, 'top-left', 0, 68);
- $generatorPng = new BarcodeGeneratorJPG();
- $barCode = $generatorPng->getBarcode($code, $generatorPng::TYPE_CODE_128_A, 1, 50);
- $stripeCode = Image::make($barCode)->resize(382, 50);
- $stripeCode_weight = 404 * $params['scale'] * $params['weightScale'];
- $stripeCode_height = 92 * $params['scale'] * $params['heightScale'];
- $bgImage->insert($stripeCode, 'top-left', 6, 9)->resize($stripeCode_weight, $stripeCode_height);
- $left = $params['left'] + ((404 * (1- $params['scale'])) * $params['weightScale']) / 2;
- $top = $params['top'] + ((92 * (1-$params['scale'])) * $params['heightScale']) / 2;
- return $img->insert($bgImage, 'top-left', intval($left), intval($top));
- }
- public function setImage($img, $params)
- {
- $printPartImage = PrintPartImage::query()->where('name', $params['text'])->first();
- $path = ['app', 'public'];
- $path = join(DIRECTORY_SEPARATOR, $path);
- $dirPath = storage_path($path);
- $uri = $printPartImage->file['url'];
- $uri = str_replace('/', DIRECTORY_SEPARATOR, $uri);
- $path = $dirPath . $uri . '.' . $printPartImage->file['type'];
- $image = Image::make(File::get($path));
- $image->resize($params['width'], $params['height']);
- $img->insert($image, 'top-left', $params['left'], $params['top']);
- return $img;
- }
- public function getTextBox($params, $text): \Intervention\Image\Image
- {
- $bgImg = Image::canvas($params['width'], $params['height'], '#fff');
- if ($params['border-width'] !== 0) {
- $borderWidth = $params['border-width'] * 2;
- $paddingBgImage = Image::canvas($params['width'], $params['height'], '#000');
- $paddingImage = Image::canvas($params['width'] - $borderWidth, $params['height'] - $borderWidth, '#fff');
- $paddingBgImage->insert($paddingImage, 'top-left', intval($params['border-width'] / 2), intval($params['border-width'] / 2));
- $bgImg->insert($paddingBgImage);
- }
- $path_arr = ['fonts', 'simsun.ttc'];
- $x = $params['width'] / 2;
- if ($params['justify-content'] == 'center') $x = $params['width'] / 2;
- if ($params['justify-content'] == 'flex-start') $x = $params['width'] / 2;
- $y = $params['height'] / 2;
- if ($params['align-items'] == 'center') $y = $params['height'] / 2;
- if ($params['align-items'] == 'flex-start') $y = 0;
- if ($params['align-items'] == 'flex-end') $y = ($params['height'] - 22) / 2;
- // 字体路径
- $path = app('path.public') . (DIRECTORY_SEPARATOR . join(DIRECTORY_SEPARATOR, $path_arr));
- $aligns = [
- 'flex-start' => 'right',
- 'center' => 'center'
- ];
- $vAligns = [
- 'center' => 'center',
- 'flex-start' => 'top',
- 'flex-end' => 'bottom',
- ];
- return $bgImg->text($text, intval($x), intval($y), function ($font) use ($params, $path, $aligns, $vAligns) {
- $font->file($path);
- $font->size($params['font-size'] ?? 24);
- $font->align($aligns[$params['justify-content']]); // 水平对齐方式
- // center
- // right
- $font->valign($vAligns[$params['align-items']]); // 垂直对齐方式
- // center
- // middle
- // top
- // bottom
- });
- }
- public function setQRCode($img, $params, $orderPackages)
- {
- $text = $this->getValue($params, $orderPackages);
- $qrCode = new QrCode($text);
- $qrCode->setSize(100);
- $qrCode->setMargin(0);
- $scale = $params['scale'] ?? 1;
- return $img->insert(Image::make($qrCode->writeString())->resize(100 * $scale * $params['weightScale'], 100 * $scale * $params['heightScale']), 'top-left', $params['left'], $params['top']);
- }
- // 获取图片
- public function getImage($base64, &$path): \Intervention\Image\Image
- {
- $path = $this->getImagePath();
- $img = Image::make($base64);
- $img->save($path); // 图片保存
- return $img;
- }
- // 获取打印图片的路径
- private function getImagePath(): string
- {
- $img_name = Str::uuid() . '.jpg';
- $path_arr = ['app', 'public', 'print'];
- $dir_path = implode(DIRECTORY_SEPARATOR, $path_arr);
- if (!file_exists(storage_path($dir_path))) mkdir(storage_path($dir_path));
- $path_arr[] = 'image';
- $dir_path = implode(DIRECTORY_SEPARATOR, $path_arr);
- if (!file_exists(storage_path($dir_path))) mkdir(storage_path($dir_path));
- $path_arr[] = $img_name;
- return storage_path(implode(DIRECTORY_SEPARATOR, $path_arr));
- }
- // 保存至保存图片路径下
- public function saveImage($image, &$path)
- {
- $path = $this->getImagePath();
- $image->save($path); // 图片保存
- }
- // 根据图片路径读取而base64编码的图片
- public function readImageBase64($path): string
- {
- if ($fp = fopen($path, "rp", 0)) {
- $gambar = fread($fp, filesize($path));
- fclose($fp);
- return chunk_split(base64_encode($gambar));
- }
- return "";
- }
- // 通过设置获取所需参数
- private function getValue($params, $orderPackage = [], $k = 'text'): string
- {
- $key = strtolower($params[$k]);
- if ($value = strstr($key, '$')) return $orderPackage[ltrim($value, '$')] ?? '';
- else return $params[$k];
- }
- }
|