Пример #1
0
 public function renderSvgPage($page, $templates, $style, $size = null)
 {
     if (!isset($page['photos'])) {
         return '';
     }
     $count_photos = count($page['photos']);
     //$template_id=$page['layout']['template_ids']['ph_count_'.$count_photos];
     $style_id = $style->id;
     //$page['layout']['style_id'];
     $placeholder_border_color_top_left = $style->placeholder_border_color_top_left;
     $placeholder_border_color_bottom_right = $style->placeholder_border_color_bottom_right;
     /*
     *
     * public $placeholder_border_color_top_left='#000000';
     
         public $placeholder_border_color_bottom_right='#FFFFFF';
     */
     $layout_index = 0;
     if (!empty($page['layout_index'])) {
         $layout_index = $page['layout_index'];
     }
     if (empty($templates[$count_photos][$layout_index])) {
         $layout_index = 0;
         $page['layout_index'] = 0;
     }
     if (!empty($templates[$count_photos])) {
         $svg = $templates[$count_photos][$layout_index]['svg'];
     } else {
         $svg = '<?xml version="1.0" encoding="UTF-8" standalone="no" ?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' . '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="700" height="250" viewBox="0 0 700 250" xml:space="preserve"><desc>Created with Fabric.js 1.4.0</desc><defs></defs>' . '<g transform="translate(0 0)"></g>' . '<rect x="-2" y="-125" rx="0" ry="0" width="4" height="250" style="stroke: none; stroke-width: 1; stroke-dasharray: ; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: black; opacity: 1;" transform="translate(350 125)"/>' . '</svg>';
     }
     /*if(!empty($page['text'])){
     
                 $svg=$templates[$template_id]['svg_text'];
             }*/
     $background_color = '#ffffff';
     //$page['layout']['background_color'];
     $background_image = false;
     //$page['layout']['background_image'];
     $photo_index = 0;
     $doc = new \DOMDOcument();
     $doc->loadxml($svg);
     //$xpath = new \DOMXpath($doc);
     //$childre=new \DOMNodeList();
     /*
     *
     * <?xml version="1.0" encoding="UTF-8" standalone="no" ?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
     <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="700" height="350" viewBox="0 0 700 350" xml:space="preserve"><desc>Created with Fabric.js 1.4.0</desc><defs></defs><g transform="translate(172.5 85)" data_name="1" data_value="1" passepartout="false" object_text="false" border_left="false" border_top="false" border_right="false" border_bottom="false" border_color="#FFFFFF" border_width="3"><rect x="-172.5" y="-85" rx="0" ry="0" width="345" height="170" style="stroke: none; stroke-width: 1; stroke-dasharray: ; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: #ccc; opacity: 1;" transform="translate(0 0)"/><g transform="translate(0 0)"><text font-family="Arial" font-size="40" font-weight="normal" style="stroke: none; stroke-width: 1; stroke-dasharray: ; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: #bbb; opacity: 1;" transform="translate(-11 39)"><tspan x="0" y="-26" fill="#bbb">1</tspan></text></g><g transform=""><path d="M 0 170 L 0 0 L 345 0 L 345 170 L 0 170 z" style="stroke: #FFFFFF; stroke-width: 3; stroke-dasharray: ; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: transparent; opacity: 1; visibility: hidden;" transform="translate(-172.5 -85)" stroke-linecap="round" /></g></g><g transform="translate(83.75 265)" data_name="2" data_value="2" passepartout="false" object_text="false" border_left="false" border_top="false" border_right="false" border_bottom="false" border_color="#FFFFFF" border_width="3"><rect x="-83.75" y="-85" rx="0" ry="0" width="167.5" height="170" style="stroke: none; stroke-width: 1; stroke-dasharray: ; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: #ccc; opacity: 1;" transform="translate(0 0)"/><g transform="translate(0 0)"><text font-family="Arial" font-size="40" font-weight="normal" style="stroke: none; stroke-width: 1; stroke-dasharray: ; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: #bbb; opacity: 1;" transform="translate(-11 39)"><tspan x="0" y="-26" fill="#bbb">2</tspan></text></g><g transform=""><path d="M 0 170 L 0 0 L 167.5 0 L 167.5 170 L 0 170 z" style="stroke: #FFFFFF; stroke-width: 3; stroke-dasharray: ; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: transparent; opacity: 1; visibility: hidden;" transform="translate(-83.5 -85)" stroke-linecap="round" /></g></g><g transform="translate(261.25 265)" data_name="3" data_value="3" passepartout="false" object_text="false" border_left="false" border_top="false" border_right="false" border_bottom="false" border_color="#FFFFFF" border_width="3"><rect x="-83.75" y="-85" rx="0" ry="0" width="167.5" height="170" style="stroke: none; stroke-width: 1; stroke-dasharray: ; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: #ccc; opacity: 1;" transform="translate(0 0)"/><g transform="translate(0 0)"><text font-family="Arial" font-size="40" font-weight="normal" style="stroke: none; stroke-width: 1; stroke-dasharray: ; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: #bbb; opacity: 1;" transform="translate(-11 39)"><tspan x="0" y="-26" fill="#bbb">3</tspan></text></g><g transform=""><path d="M 0 170 L 0 0 L 167.5 0 L 167.5 170 L 0 170 z" style="stroke: #FFFFFF; stroke-width: 3; stroke-dasharray: ; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: transparent; opacity: 1; visibility: hidden;" transform="translate(-83.5 -85)" stroke-linecap="round" /></g></g><g transform="translate(438.75 85)" data_name="5" data_value="5" passepartout="false" object_text="false" border_left="false" border_top="false" border_right="false" border_bottom="false" border_color="#FFFFFF" border_width="3"><rect x="-83.75" y="-85" rx="0" ry="0" width="167.5" height="170" style="stroke: none; stroke-width: 1; stroke-dasharray: ; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: #ccc; opacity: 1;" transform="translate(0 0)"/><g transform="translate(0 0)"><text font-family="Arial" font-size="40" font-weight="normal" style="stroke: none; stroke-width: 1; stroke-dasharray: ; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: #bbb; opacity: 1;" transform="translate(-11 39)"><tspan x="0" y="-26" fill="#bbb">5</tspan></text></g><g transform=""><path d="M 0 170 L 0 0 L 167.5 0 L 167.5 170 L 0 170 z" style="stroke: #FFFFFF; stroke-width: 3; stroke-dasharray: ; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: transparent; opacity: 1; visibility: hidden;" transform="translate(-83.5 -85)" stroke-linecap="round" /></g></g><g transform="translate(527.5 265)" data_name="6" data_value="6" passepartout="false" object_text="false" border_left="false" border_top="false" border_right="false" border_bottom="false" border_color="#FFFFFF" border_width="3"><rect x="-172.5" y="-85" rx="0" ry="0" width="345" height="170" style="stroke: none; stroke-width: 1; stroke-dasharray: ; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: #ccc; opacity: 1;" transform="translate(0 0)"/><g transform="translate(0 0)"><text font-family="Arial" font-size="40" font-weight="normal" style="stroke: none; stroke-width: 1; stroke-dasharray: ; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: #bbb; opacity: 1;" transform="translate(-11 39)"><tspan x="0" y="-26" fill="#bbb">6</tspan></text></g><g transform=""><path d="M 0 170 L 0 0 L 345 0 L 345 170 L 0 170 z" style="stroke: #FFFFFF; stroke-width: 3; stroke-dasharray: ; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: transparent; opacity: 1; visibility: hidden;" transform="translate(-172.5 -85)" stroke-linecap="round" /></g></g><g transform="translate(616.25 85)" data_name="4" data_value="4" passepartout="false" object_text="false" border_left="false" border_top="false" border_right="false" border_bottom="false" border_color="#FFFFFF" border_width="3"><rect x="-83.75" y="-85" rx="0" ry="0" width="167.5" height="170" style="stroke: none; stroke-width: 1; stroke-dasharray: ; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: #ccc; opacity: 1;" transform="translate(0 0)"/><g transform="translate(0 0)"><text font-family="Arial" font-size="40" font-weight="normal" style="stroke: none; stroke-width: 1; stroke-dasharray: ; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: #bbb; opacity: 1;" transform="translate(-11 39)"><tspan x="0" y="-26" fill="#bbb">4</tspan></text></g><g transform=""><path d="M 0 170 L 0 0 L 167.5 0 L 167.5 170 L 0 170 z" style="stroke: #FFFFFF; stroke-width: 3; stroke-dasharray: ; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: transparent; opacity: 1; visibility: hidden;" transform="translate(-83.5 -85)" stroke-linecap="round" /></g></g></svg>
     */
     $defs = null;
     $defsItem = null;
     for ($i = 0; $i < $doc->childNodes->length; $i++) {
         $childs = $doc->childNodes->item($i);
         //print_r($childs);
         if ($childs->hasChildNodes()) {
             for ($j = 0; $j < $childs->childNodes->length; $j++) {
                 $item = $childs->childNodes->item($j);
                 // echo $item->tagName.' ';
                 if ($item->tagName == 'defs') {
                     $defs = $item;
                 }
                 if ($defs) {
                     $defsItem = $item;
                     $defs = null;
                 }
                 if ($item->tagName == 'g') {
                     for ($a = 0; $a < $item->attributes->length; $a++) {
                         $attr = $item->attributes->item($a);
                         $data_name = $item->attributes->getNamedItem('data_name');
                         if ($data_name && $data_name->textContent) {
                             if ($item->childNodes) {
                                 for ($k = 0; $k < $item->childNodes->length; $k++) {
                                     $element_in_ph = $item->childNodes->item($k);
                                     if ($element_in_ph->tagName == 'rect') {
                                         $place_width = $element_in_ph->attributes->getNamedItem('width')->textContent;
                                         $place_height = $element_in_ph->attributes->getNamedItem('height')->textContent;
                                         $node = $doc->createDocumentFragment();
                                         if ($data_name->textContent != 'T') {
                                             $photo_index = intval($data_name->textContent) - 1;
                                             $photo_id = $page['photos'][$photo_index]['file_key'];
                                             if (!isset($page['photos'][$photo_index]['pos_dx'])) {
                                                 $page['photos'][$photo_index]['pos_dx'] = 0;
                                                 $page['photos'][$photo_index]['pos_dy'] = 0;
                                                 $page['photos'][$photo_index]['scale'] = 1;
                                                 // $page['photos'][$photo_index]['ext']=;
                                             }
                                             $pos_dx = $page['photos'][$photo_index]['pos_dx'];
                                             $pos_dy = $page['photos'][$photo_index]['pos_dy'];
                                             $scale = $page['photos'][$photo_index]['scale'];
                                             $ext = 'jpg';
                                             if (!empty($page['photos'][$photo_index]['ext'])) {
                                                 $ext = $page['photos'][$photo_index]['ext'];
                                             }
                                             $image_size = UserUrl::IMAGE_MIDDLE;
                                             //Нужено сделать выбор картинки на основании scale
                                             if ($size) {
                                                 $image_size = $size;
                                             }
                                             $photo_index++;
                                             $node->appendXML(ImagePlaceholderReplacer::widget(['user_id' => $this->user_id, 'photobook_id' => $this->id, 'photo_id' => $photo_id, 'place_width' => $place_width, 'place_height' => $place_height, 'real_width' => $this->data['sizes'][$photo_id]['width'], 'real_height' => $this->data['sizes'][$photo_id]['height'], 'mtime' => $this->data['sizes'][$photo_id]['mtime'], 'image_size' => $image_size, 'scale' => $scale, 'pos_dx' => $pos_dx, 'pos_dy' => $pos_dy, 'ext' => $ext, 'passpartu_left_top_border_color' => $placeholder_border_color_top_left, 'passpartu_right_bottom_border_color' => $placeholder_border_color_bottom_right]));
                                             $item->replaceChild($node, $element_in_ph);
                                         } else {
                                             $photo_id = $page['text']['file_id'];
                                             $image_size = UserUrl::IMAGE_MIDDLE;
                                             //Нужено сделать выбор картинки на основании scale
                                             if ($size) {
                                                 $image_size = $size;
                                             }
                                             $node->appendXML(TextPlaceholderReplacer::widget(['user_id' => $this->user_id, 'photobook_id' => $this->id, 'photo_id' => $photo_id, 'text_label' => $page['text']['text'], 'place_width' => $place_width, 'place_height' => $place_height, 'image_size' => $image_size]));
                                             $item->replaceChild($node, $element_in_ph);
                                         }
                                     } else {
                                         if ($element_in_ph->tagName == 'path') {
                                             $item->removeChild($element_in_ph);
                                         }
                                     }
                                     if ($element_in_ph->tagName == 'g' && $element_in_ph->hasChildNodes()) {
                                         // $element_in_ph->removeChild($element_in_ph->childNodes->item(0));
                                         try {
                                             $elem = $element_in_ph->childNodes->item(0);
                                             if ($elem->tagName == 'path') {
                                                 // print_r($elem);
                                                 //$element_in_ph->removeChild($elem);
                                             } else {
                                                 $element_in_ph->removeChild($elem);
                                             }
                                         } catch (\DOMException $e) {
                                         } catch (\ErrorException $e) {
                                         }
                                         //$element_in_ph->childNodes->item(0)->removeChild($element_in_ph->childNodes->item(0));
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             if ($defsItem) {
                 $node = $doc->createDocumentFragment();
                 $image_size = UserUrl::IMAGE_MIDDLE;
                 //Нужено сделать выбор картинки на основании scale
                 if ($size) {
                     $image_size = $size;
                 }
                 $content = '';
                 if ($background_image) {
                     $content = ImageBackgroundReplacer::widget(['style_id' => $style_id, 'file_key' => $background_image, 'image_size' => $image_size]);
                 }
                 // $node->appendXML('<rect x="0" y="0" rx="0" ry="0" width="700" height="250" style="stroke: none; stroke-width: 1; stroke-dasharray: ; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: '.$background_color.'; opacity: 1;" transform="translate(0 0)"/>'.$content);
                 $node->appendXML('<rect/>' . $content);
                 $childs->insertBefore($node, $defsItem);
             }
         }
     }
     return $doc->saveXML($doc);
 }