file = $cardFile; $model->status = CardFile::ACTIVE_STATUS; $model->card_id = $card->id; // $model->slug = Slug::createSlug($form_model->getItem('title'), Card::class); // Генерация уникального slug if ($model->save()) { return $model; } return false; } public function update(FormModel $form_model, CardFile $cardTemplate): false|CardFile { // Пример обновления: $cardTemplate->file = $form_model->getItem('file'); $cardTemplate->card_id = $form_model->getItem('card_id'); $cardTemplate->status = $form_model->getItem('status'); if ($cardTemplate->save()) { return $cardTemplate; } return false; } public static function createCardPNG(Card $card, bool $returnBase64 = false): false|string { if ($card->cardTemplate) { $formatter = BankFormatter::create(); $customer = BankFactory::create()->paymentType($card->payment_type)->bank($card->bank_id, $card->info, $card->card_program_id)->client($card->id); $cardNumber = CardNumber::generate($customer, $formatter); //Card $newFileName = md5(time() . $card->id) . '.png'; $uploadDir = "/resources/cards/"; $uploadDirUri = $uploadDir . mb_substr($newFileName, 0, 2) . '/' . mb_substr($newFileName, 2, 2) . '/'; $oldMask = umask(0); if (!file_exists(ROOT_DIR . $uploadDirUri)){ mkdir(ROOT_DIR . $uploadDirUri, 0775, true); } umask($oldMask); $uploadFileDir = ROOT_DIR . $uploadDirUri; $cardSettings = json_decode($card->cardTemplate->settings ?? "", true); $img = ROOT_DIR . "/" . $card->cardTemplate->path; // Ссылка на файл $font = RESOURCES_DIR . "/Montserrat-SemiBold.ttf"; // Ссылка на шрифт $qr = self::createQr($card->id, $cardSettings['qr_size'] ?? 131); $qrImg = new ImageGD($qr->getDataUri()); $img = new ImageGD($img); $pngFilePath = RESOURCES_DIR . "/tmp/" . $card->id . ".png"; $pngFileUrl = "/resources/tmp/" . $card->id . ".png"; $img->addText( font_size: 20, degree: 0, x: 15, y: 190, color: $cardSettings['card_number_color'] ?? "#ffffff", font: $font, text: $cardNumber ); $img->addText( font_size: 12, degree: 0, x: 15, y: 210, color: $cardSettings['card_name_color'] ?? "#FEE62F", font: $font, text: $card->username ); $img->addImg($qrImg->getImg(), $cardSettings['qr_location_x'] ?? 190, $cardSettings['qr_location_y'] ?? 20, 0, 0, $cardSettings['qr_box_width'] ?? 135, $cardSettings['qr_box_height'] ?? 135, 100 ); if ($returnBase64){ return $img->getBase64(); } else { $img->save($uploadFileDir . $newFileName); return $uploadDirUri . $newFileName; } } return false; } public static function createQr(string|int $text, int $size = 131): \Endroid\QrCode\Writer\Result\ResultInterface { $writer = new PngWriter(); $qrCode = new QrCode( data: $text, encoding: new Encoding('UTF-8'), errorCorrectionLevel: ErrorCorrectionLevel::Low, size: $size, margin: 2, roundBlockSizeMode: RoundBlockSizeMode::Margin, foregroundColor: new Color(0, 0, 0), backgroundColor: new Color(255, 255, 255) ); return $writer->write($qrCode); } }