public function save_design() { if ($this->input->post('mode') == 'edit') { $this->db->delete('designs', array('id' => $this->input->post('design_id'))); } $faceMacket = str_replace('http://klever.media/', '', $this->input->post('faceMacket')); $backMacket = str_replace('http://klever.media/', '', $this->input->post('backMacket')); $face = $this->input->post('face'); $back = $this->input->post('back'); // get all fonts $query = $this->db->get('fonts'); $fonts = array(); foreach ($query->result() as $font) { $fonts[$font->family] = $font->source; } // generate pdf face template name and preview name $face_pdf = 'uploads/redactor/face_' . md5(microtime(true)) . '.pdf'; $face_preview = 'uploads/redactor/face_' . md5(microtime(true)) . '.jpg'; // convert face image to pdf $img = new Imagick($faceMacket); $img->setresolution(300, 300); $img->setcolorspace(Imagick::COLORSPACE_CMYK); $img->resizeimage(1076, 720, Imagick::FILTER_LANCZOS, 1); $img->setimageformat('pdf'); $img->writeimage($face_pdf); // include TCPDF ana FPDI include_once APPPATH . 'libraries/tcpdf/tcpdf.php'; include_once APPPATH . 'libraries/tcpdf/fpdi.php'; include_once APPPATH . 'libraries/tcpdf/include/tcpdf_fonts.php'; $fontMaker = new TCPDF_FONTS(); // создаём лист $pdf = new FPDI('L', 'mm', array(91, 61), true, 'UTF-8', false); $pdf->SetMargins(0, 0, 0, true); $pdf->AddPage('L'); // загрузим ранее сохранённый шаблон $pdf->setSourceFile($face_pdf); $pdf->SetMargins(0, 0, 0, true); $tplIdx = $pdf->importPage(1); $pdf->useTemplate($tplIdx, null, null, 0, 0, true); // установим опции для pdf $pdf->setCellHeightRatio(1); $pdf->setCellPaddings(0, 0, 0, 0); $pdf->setCellMargins(0, 0, 0, 0); $pdf->SetAutoPageBreak(false, 0); $pdf->SetPrintHeader(false); $pdf->SetPrintFooter(false); if (!empty($face)) { // отрисуем сначала изображения лица foreach ($face as $item) { if ($item['type'] == 'image') { $pdf->Image($_SERVER['DOCUMENT_ROOT'] . '/' . str_replace('http://klever.media/', '', $item['content']), $this->px_to_mm($item['left']), $this->px_to_mm($item['top']), $this->px_to_mm($item['width']), '', '', '', '', false, 300); } } // потом текст на лице foreach ($face as $item) { if ($item['type'] == 'text') { $cmyk = $this->rgbToCmyk($item['color']); $pdf->SetTextColor($cmyk['c'] * 100, $cmyk['m'] * 100, $cmyk['y'] * 100, $cmyk['k'] * 100); // set font $tcpdfFont = $fontMaker->addTTFfont(realpath('fonts/redactor/' . $fonts[$item['font']])); $pdf->SetFont($tcpdfFont, '', $item['size'] / 2, '', 'false'); $pdf->Text($this->px_to_mm($item['left']), $this->px_to_mm($item['top']), $item['content'], false, false, true, 0, 0, 'L', false, '', 0, false, 'T', 'L', false); } } } // сохраним пдф лица $pdf->Output($_SERVER['DOCUMENT_ROOT'] . '/' . $face_pdf, 'F'); // сделаем превью для пользователя $im = new Imagick(); $im->setResolution(300, 300); $im->readimage($face_pdf . '[0]'); $im->flattenimages(); $im->setImageFormat('jpg'); $im->resizeimage(1076, 720, Imagick::FILTER_LANCZOS, 1); $im->writeImage($face_preview); $im->clear(); $im->destroy(); //exec('$ convert ' . $_SERVER['DOCUMENT_ROOT'] . '/' . $face_pdf . ' ' . $_SERVER['DOCUMENT_ROOT'] . '/' . $face_preview); // есть ли оборот if (!empty($backMacket)) { // generate pdf back template name and preview name $back_pdf = 'uploads/redactor/back_' . md5(microtime(true)) . '.pdf'; $back_preview = 'uploads/redactor/back_' . md5(microtime(true)) . '.jpg'; // convert back image to pdf $img = new Imagick($backMacket); $img->setresolution(300, 300); $img->setcolorspace(Imagick::COLORSPACE_CMYK); $img->resizeimage(1076, 720, Imagick::FILTER_LANCZOS, 1); $img->setimageformat('pdf'); $img->writeimage($back_pdf); // создаём лист $pdf = new FPDI('L', 'mm', array(91, 61), true, 'UTF-8', false); $pdf->AddPage('L'); // загрузим ранее сохранённый шаблон $pdf->setSourceFile($_SERVER['DOCUMENT_ROOT'] . '/' . $back_pdf); $pdf->SetMargins(0, 0, 0, true); $tplIdx = $pdf->importPage(1); $pdf->useTemplate($tplIdx, null, null, 0, 0, true); // установим опции для pdf $pdf->SetMargins(0, 0, 0, true); $pdf->setCellHeightRatio(1); $pdf->setCellPaddings(0, 0, 0, 0); $pdf->setCellMargins(1, 1, 1, 1); $pdf->SetAutoPageBreak(false); $pdf->SetPrintHeader(false); $pdf->SetPrintFooter(false); if (!empty($back)) { // отрисуем сначала изображения оборота foreach ($back as $item) { if ($item['type'] == 'image') { $pdf->Image($_SERVER['DOCUMENT_ROOT'] . '/' . str_replace('http://klever.media/', '', $item['content']), $this->px_to_mm($item['left']), $this->px_to_mm($item['top']), $this->px_to_mm($item['width']), '', '', '', '', false, 300); } } // потом текст на обороте foreach ($back as $item) { if ($item['type'] == 'text') { $cmyk = $this->rgbToCmyk($item['color']); $pdf->SetTextColor($cmyk['c'] * 100, $cmyk['m'] * 100, $cmyk['y'] * 100, $cmyk['k'] * 100); // set font $tcpdfFont = $fontMaker->addTTFfont($_SERVER['DOCUMENT_ROOT'] . '/fonts/redactor/' . $fonts[$item['font']]); $pdf->SetFont($tcpdfFont, '', $item['size'] / 2, '', 'false'); $pdf->Text($this->px_to_mm($item['left']), $this->px_to_mm($item['top']), $item['content'], false, false, true, 0, 0, 'L', false, '', 0, false, 'T', 'L', false); } } } // сохраним пдф оборота $pdf->Output($_SERVER['DOCUMENT_ROOT'] . '/' . $back_pdf, 'F'); // сделаем превью для пользователя $im = new Imagick(); $im->setResolution(300, 300); $im->readimage($back_pdf . '[0]'); $im->setImageFormat('jpg'); $im->flattenimages(); $im->resizeimage(1076, 720, Imagick::FILTER_LANCZOS, 1); $im->writeImage($back_preview); $im->clear(); $im->destroy(); } $this->db->insert('products', array('name' => 'cards_pvc', 'tiraj' => 100, 'weight' => 0.5, 'price' => 0.49, 'cost' => 49, 'macket' => 'my_macket')); $product_id = $this->db->insert_id(); $this->db->insert('designs', array('product_id' => $product_id, 'theme_id' => $this->input->post('theme'), 'face_background' => $faceMacket, 'back_background' => empty($backMacket) ? NULL : $backMacket, 'face' => $face_preview, 'back' => empty($back_preview) ? '' : $back_preview, 'type' => 'system')); $design_id = $this->db->insert_id(); $options = array(); if (!empty($face)) { foreach ($face as $item) { if ($item['type'] == 'text') { $options[] = array('design_id' => $design_id, 'type' => 'text', 'front' => 'face', 'top' => $item['top'], 'left' => $item['left'], 'width' => NULL, 'height' => NULL, 'content' => $item['content'], 'font' => $item['font'], 'color' => $item['color'], 'size' => $item['size']); } else { $options[] = array('design_id' => $design_id, 'type' => 'image', 'front' => 'face', 'top' => $item['top'], 'left' => $item['left'], 'width' => $item['width'], 'height' => $item['height'], 'content' => $item['content'], 'font' => NULL, 'color' => NULL, 'size' => NULL); } } } if (!empty($back)) { foreach ($back as $item) { if ($item['type'] == 'text') { $options[] = array('design_id' => $design_id, 'type' => 'text', 'front' => 'back', 'top' => $item['top'], 'left' => $item['left'], 'width' => NULL, 'height' => NULL, 'content' => $item['content'], 'font' => $item['font'], 'color' => $item['color'], 'size' => $item['size']); } else { $options[] = array('design_id' => $design_id, 'type' => 'image', 'front' => 'back', 'top' => $item['top'], 'left' => $item['left'], 'width' => $item['width'], 'height' => $item['height'], 'content' => $item['content'], 'font' => NULL, 'color' => NULL, 'size' => NULL); } } } if (count($options) > 0) { $this->db->insert_batch('design_options', $options); } echo 'OK'; }
public function test() { // $rgb_arr = sscanf('rgb(243, 243, 243)', "rgb(%d, %d, %d)"); // print_r($this->rgbToCmyk($rgb_arr[0], $rgb_arr[1], $rgb_arr[2])); // exit(); // $img = new Imagick('test.jpg'); $img->resizeimage(1076, 720, Imagick::FILTER_LANCZOS, 1); $img->setresolution(300, 300); $img->setimageformat('pdf'); $img->writeimage('test.pdf'); include_once APPPATH . 'libraries/tcpdf/tcpdf.php'; include_once APPPATH . 'libraries/tcpdf/fpdi.php'; // создаём лист $pdf = new FPDI('L', 'mm', array(91, 61), true, 'UTF-8', false); $pdf->AddPage('L'); // загрузим ранее сохранённый шаблон $pdf->setSourceFile('test.pdf'); $pdf->SetMargins(0, 0, 0, true); $tplIdx = $pdf->importPage(1); $pdf->useTemplate($tplIdx, null, null, 0, 0, true); // установим опции для pdf $pdf->SetMargins(0, 0, 0, true); $pdf->setCellHeightRatio(1); $pdf->setCellPaddings(0, 0, 0, 0); $pdf->setCellMargins(1, 1, 1, 1); $pdf->SetAutoPageBreak(false); $pdf->SetPrintHeader(false); $pdf->SetPrintFooter(false); $pdf->Image('qwe.png', 30, 30, 20, '', '', '', '', false, 300); $pdf->SetTextColor(0, 0, 0, 100); $pdf->SetFont('helvetica', 'BI', 10, '', 'false'); $pdf->Text(12, 9, 'Black CMYK'); $pdf->Output($_SERVER['DOCUMENT_ROOT'] . '/uploads/fynal.pdf', 'F'); $im = new Imagick(); $im->setResolution(300, 300); $im->readimage('uploads/fynal.pdf[0]'); $im->setImageFormat('jpeg'); $im->resizeimage(538, 360, Imagick::FILTER_LANCZOS, 1); $im->writeImage('uploads/fynal.jpg'); $im->clear(); $im->destroy(); // include_once APPPATH . 'libraries/drawer.php'; // // $drawer = new Drawer(); // $drawer->init(); // $drawer->setLayout(); // $drawer->setBackground('test.jpg'); // $drawer->drawImage('test.jpg', 10, 10, 30); // $drawer->drawText('Maxim', 10, 10, 'TimesNewRoman', 20, 'rgb(77, 77, 77)'); // $drawer->savePdf($_SERVER['DOCUMENT_ROOT'] . '/1.pdf'); // $drawer->makePreview($_SERVER['DOCUMENT_ROOT'] . '/1.pdf', $_SERVER['DOCUMENT_ROOT'] . '/1.jpg'); }