twig = $twig; $this->params = $params; $this->directory = $directory; $this->templateRepo = $templateRepo; $this->dedicatedFile = $dedicatedFile; $this->arboTwig = $this->params->get('template_twig_directory'); $this->arboTwigFile = $this->params->get('template_twig_file'); $this->logService = $logService; $this->generationService = $generationService; $this->clearService = $clearService; $this->mediaFilesLocation .= $this->params->get('media_dir'); } /** * @param string $twigContent * * @throws UnprocessableEntityMaestroException * * @return void */ public function createTwigFile(string $twigContent): void { if ($this->arboTwig && $this->arboTwigFile) { $directory = $this->directory->getAbsolutePath($this->arboTwig, 'directory'); $path = $this->directory->getAbsolutePath($this->arboTwig . DIRECTORY_SEPARATOR . $this->arboTwigFile, 'file'); $filesystem = new Filesystem(); $tmp = $filesystem->tempnam($directory, 'twig_'); $filesystem->appendToFile($tmp, $twigContent); $filesystem->copy($tmp, $path, true); $filesystem->remove($tmp); } else { throw new UnprocessableEntityMaestroException("'template_twig_directory' && 'template_twig_file'. Check env's configuration.", "twig", "createTwigFile"); } } /** * @param array $elementGabValues * @param int $elementNumber * @param string $exportType * * @return array */ public function getVariablesValues(array $elementGabValues, int $elementNumber, string $exportType, ?int $templateId, array $allMedias = []): array { $variablesValues = ['products' => []]; foreach ($elementGabValues['values'] as $key => $elementGabValue) { if (property_exists($elementGabValue, 'id')) { $fields = []; if (isset($elementGabValue->values) && !empty($elementGabValue->values)) { $fields = $this->createVariablesValues($elementGabValue->values, $exportType, $allMedias); } $medias = []; if (isset($elementGabValue->medias) && !empty($elementGabValue->medias)) { $medias = $this->createMediasValues($elementGabValue->medias, $exportType, $templateId, $allMedias); } $mergedData = $fields; $mergedData['_folio_'] = $elementGabValues['folio']; $mergedData['_utils_'] = array_merge($elementGabValue->utils, $medias); if ($key === array_key_first($elementGabValues)) { $commonValues = array_map(function ($val) { $val = ""; return $val; }, $mergedData); $variablesValues = $commonValues; } if (!empty($elementGabValue->id)) { $variablesValues['products'][$elementGabValue->id] = $mergedData; $variablesValues['products'][$elementGabValue->id]['product_name__'] = $elementGabValue->name; } else { $variablesValues = array_merge($variablesValues, $mergedData); } } } if (isset($elementGabValue->options)) { $optionsGab = $elementGabValue->options; foreach ($optionsGab as $optionGab) { if ($optionGab->type != "color") { $variablesValues[$optionGab->twigVar] = $optionGab->value; } } } return $variablesValues; } /** * [Create XML file] * * @param array $variablesValues * * @return string */ public function getXmlContent(array $variablesValues): string { $filePath = $this->directory->getFilePath("", $this->arboTwigFile); $function = new TwigFunction('txtToXml', function (string $param, array $rules = []) { return $this->txtToXml($param, $rules); }); $this->twig->addFunction($function); $functionRet = new TwigFunction('addBreakLine', function () { return $this->addBreakLine(); }); $this->twig->addFunction($functionRet); $this->twig->enableAutoReload(); //@TODO : Only clean cache when templated is updated try { $xmlString = $this->twig->render($filePath, $variablesValues); } catch (Exception $e) { $this->logService->setLog($e->getMessage()); throw new UnprocessableEntityMaestroException($e->getMessage(), "twig", "render", false); } $result = html_entity_decode($xmlString); $result = str_replace('BreakLine', "\n", $result); return $result; } /** * [Create HTML file] * * @param array $variablesValues * @param int $templateId * @param array $elementGabValues * * @return string */ public function getHtmlContent(array $variablesValues, int $templateId, array $elementGabValues): ?string { $this->directory->getFilePath($this->params->get("template_twig_directory"), "export.twig.html"); if (!array_key_exists("products", $variablesValues)) { $variablesValues['products'][] = $variablesValues; } return $this->txtToHtml($variablesValues, $templateId, $elementGabValues); } /** * @param array $values * @param string $exportType * * @return array */ private function createVariablesValues(array $values, string $exportType, array $allMedias = []): array { $variablesValues = []; foreach ($values as $value) { if (isset($value->key)) { if (is_array($value->value) || $value->fieldType === 'list' || $value->fieldType === 'list multiple' || $value->fieldType === 'media') { if ($value->fieldType === 'list') { if (!empty($value->value)) { $variablesValues[$value->twigVar] = reset($value->value); } else { $variablesValues[$value->twigVar] = ""; } } else if ($value->fieldType === 'list multiple') { if (!empty($value->value)) { $variablesValues[$value->twigVar] = $value->value; } else { $variablesValues[$value->twigVar] = []; } } else if ($value->fieldType === 'media') { if (isset($value->value)) { $medias = $this->createMediasValues(json_decode($value->value, true), $exportType, null, $allMedias); } else { $medias = []; } $variablesValues[$value->twigVar] = $medias; } else { $collections = []; if (isset($value->products)) { foreach ($value->value as $collection) { $inputs = []; foreach ($collection['value'] as $input) { $keyInput = $input->twigVar; if ($input->fieldType === 'list') { if (!empty($input->value)) { $inputs[$keyInput] = reset($input->value); } else { $inputs[$keyInput] = ""; } } elseif ($input->fieldType === 'list multiple') { if (is_array($input->value)) { $inputs[$keyInput] = $this->createVariableList($input->value); } else if (!empty(json_decode($input->value, true))) { $inputs[$keyInput] = $this->createVariableList(json_decode($input->value, true)); } else { $inputs[$keyInput] = []; } } else { $inputs[$keyInput] = $this->cleanHtmlTags($input->value); } } $collections[] = $inputs; } } else { foreach ($value->value as $collection) { $inputs = []; foreach ($collection as $input) { $keyInput = $input->twigVar; if ($input->fieldType === 'list') { if (!empty($input->value)) { if (is_array($input->value)) { $inputs[$keyInput] = reset($input->value); } else { $inputs[$keyInput] = $input->value; } } else { $inputs[$keyInput] = ""; } } elseif ($input->fieldType === 'list multiple') { if (is_array($input->value)) { $inputs[$keyInput] = $this->createVariableList($input->value); } else if (!empty(json_decode($input->value, true))) { $inputs[$keyInput] = $this->createVariableList(json_decode($input->value, true)); } else { $inputs[$keyInput] = []; } } else if ("table" === $input->fieldType && $input->value != null) { $inputs[$keyInput] = $this->extractTableContent($input->value, $exportType); } else if ($input->fieldType === 'media') { if (isset($input->value) && !empty($input->value)) { $medias = $this->createMediasValues(json_decode($input->value, true), $exportType, null, $allMedias); } else { $medias = []; } $inputs = $medias; } else { $inputs[$keyInput] = $this->cleanHtmlTags($input->value); } } $collections[] = $inputs; } } $variablesValues[$value->twigVar] = $collections; } } else { $val = $value->value; if ("table" === $value->fieldType && $val != null) { $val = $this->extractTableContent($val, $exportType); } else { $val = $this->cleanHtmlTags($val); } $variablesValues[$value->twigVar] = $val; } } } return $variablesValues; } /** * @param string table * @param string $exportType * * @return array */ private function extractTableContent(string $table, string $exportType): array { $tableData = [ "maxCol" => 0, "maxRow" => 0, "cols" => [], "rows" => [] // row number + header row number ]; $table = json_decode(json_decode($table), true); // Double json decode since table in database already json encoded and request json encode again the content if (isset($table['cols'])) { if (isset($table['maxCol'])) { $tableData['maxCol'] = $table['maxCol']; } if ("xml" === $exportType) { $tableData['cols'] = $table['cols']; usort($tableData['cols'], function ($a, $b) { // Sort cols by startHeaderRow return $a['startHeaderRow'] <=> $b['startHeaderRow']; }); } else if ("html" === $exportType) { foreach ($table["cols"] as $col) { $tableData["cols"][$col["startHeaderRow"] - 1][] = $col; } } } if (isset($table['rows'])) { $maxHeaderRow = $tableData['maxCol'] ? max(array_column($table['cols'], "startHeaderRow")) : 0; // Find how many lines are used by columns if (isset($table['maxRow'])) { $tableData['maxRow'] = $table['maxRow'] + $maxHeaderRow; // Determine how many lines we have in table including columns row } $rowCells = []; if ("xml" === $exportType) { foreach ($table['rows'] as $row) { foreach ($tableData['cols'] as $col) { if (!$col['empty']) { if (!empty($col['field'])) { $value = $this->cleanHtmlTags($row[$col['field']]); } else { $value = ""; } $rowCells[] = [ "col" => $row['col'], "row" => $row['row'], "value" => $value, "startHeaderCol" => $col["startHeaderCol"], "startHeaderRow" => $maxHeaderRow + (int) $row["position"] + 1 ]; } } } } else if ("html" === $exportType) { $rows = $table['rows']; for ($i = 0, $iMax = count($rows); $i < $iMax; $i++) { $rowsArray = []; foreach ($tableData['cols'] as $headerRow) { foreach ($headerRow as $col) { if (!$col['empty']) { if (!empty($col['field'])) { $value = $this->cleanHtmlTags($rows[$i][$col['field']]); } else { $value = ""; } $rowsArray[] = [ "col" => $rows[$i]['col'], "row" => $rows[$i]['row'], "value" => $value, "startHeaderCol" => $col["startHeaderCol"], "startHeaderRow" => $maxHeaderRow + (int) $rows[$i]["position"] + 1 ]; } } } $rowCells[] = $rowsArray; } } $tableData['rows'] = $rowCells; } return $tableData; } private function createVariableList(array $values) { $result = []; foreach ($values as $val) { if (is_array($val) && isset($val['name'])) { $result[] = $val['name']; } else { $result[] = $val; } } return $result; } /** * @param string $value [description] * * @return string [description] */ private function cleanHtmlTags(?string $value): string { $value = strip_tags($value, '

'); $value = str_replace('

', "
", $value); $value = str_replace('

', "", $value); $value = str_replace('

', "", $value); return $value; } /** * @param array $medias [description] * * @return array [description] */ private function createMediasValues(array $medias, string $exportType, ?int $templateId, array $allMedias): array { $mediasValues = []; foreach ($medias as $media) { $name = ""; $value = ""; $twigKey = ""; $copyright = ""; $legend = ""; if (is_object($media)) { if (property_exists($media, 'name') && property_exists($media, 'twigKey') && property_exists($media, 'copyright') && property_exists($media, 'legend') && property_exists($media, 'fileName')) { $name = $media->name; $twigKey = $media->twigKey; $copyright = $media->copyright; $legend = $media->legend; if ($templateId) { $template = $this->templateRepo->findById($templateId); $templateMedia = $template->getMedias()->filter(function ($medTemplate) use ($media) { return $medTemplate->getPosition() == $media->position; }); foreach ($templateMedia as $med) { $value = $this->manageMediaData($exportType, $media, $allMedias, $med); } } else { throw new UnprocessableEntityMaestroException("No template id provided to manage object " . get_class($media)); } } else { throw new UnprocessableEntityMaestroException("Missing property in object " . get_class($media)); } } else if (is_array($media)) { if (!isset($media['name'], $media['copyright'], $media['legend'])) { if (isset($media["id"])) { $index = array_search($media["id"], array_column($allMedias, "id")); if (false !== $index) { $newMedia = $allMedias[$index]; $newMedia['link'] = $media['link']; $newMedia['name'] = $newMedia['originalName']; $media = $newMedia; } else { throw new UnprocessableEntityMaestroException("No media id found in array for id : " . $media["id"]); } $name = $media['name']; $copyright = $media['copyright']; $legend = $media['legend']; } } $value = $this->manageMediaData($exportType, $media); } else { throw new UnprocessableEntityMaestroException("Unhandled type : " . gettype($media)); } $nameCopyright = str_replace("PHOTO", "CREDIT", $name); $nameLegend = str_replace("PHOTO", "LEGEND", $name); if (!empty($value)) { !empty($twigKey) ? $mediasValues["medias"][$twigKey] = $value : $mediasValues["medias"][] = $value; } if (!empty($copyright)) { $mediaCopyrigth = "<" . $nameCopyright . ">" . $copyright . ""; !empty($twigKey) ? $mediasValues["copyright"][$twigKey] = $mediaCopyrigth : $mediasValues["copyright"][] = $mediaCopyrigth; } if (!empty($legend)) { $mediaLegend = "<" . $nameLegend . ">" . $legend . ""; !empty($twigKey) ? $mediasValues["legend"][$twigKey] = $mediaLegend : $mediasValues["legend"][] = $mediaLegend; } } return $mediasValues; } private function manageMediaData(string $exportType, $media, array $allMedias = [], $med = null): string { $name = ""; $objectNameLink = ""; $mediaId = ""; $exportMediaId = ""; $isObject = true; if (is_object($media)) { $name = $media->name; $mediaId = $media->mediaId; $exportMediaId = $media->exportMediaId; if (isset($media->linkData) && isset($media->linkData["link"])) { if (isset($media->linkData["loadMetadata"]) && true === $media->linkData["loadMetadata"]) { $media = $this->generationService->getLinkMetadata($media); } $objectNameLink = $this->generationService->formatLinksFileName($media->linkData["link"]); } } else if (is_array($media)) { $isObject = false; if (isset($media['name']) && !empty($media['name']) && isset($media['loadMetadata']) && true !== ($media['loadMetadata'])) { $name = $media["name"]; } else if (isset($media["link"])) { if (isset($media["loadMetadata"]) && true === $media["loadMetadata"]) { $media = $this->generationService->getLinkMetadata($media); } if (isset($media["id"])) { $cleanName = $this->clearService->cleanFilesName([$media["name"]]); // Use real media name cleaned (like in $renamedMediasFiles in SendAction to match) instead extract thumb name from path if it's dam media with id $name = reset($cleanName); } else { $name = $this->generationService->formatLinksFileName($media["link"]); } } else { throw new UnprocessableEntityMaestroException("Cannot find media name !"); } $mediaId = $media['id']; // Id or null for media field $exportMediaId = isset($media['exportMediaId']) ? $media['exportMediaId'] : null; // Not in array for media field } if ($exportType == "xml") { $mediaInfo = null; if ($isObject) { $value = ""; if (isset($mediaId) && -1 !== $mediaId) { $value = "<" . $name . ' href="'; $mediaInfo = $this->getMediaInformation($media, $allMedias, $med); if ($mediaInfo && isset($mediaInfo['cadrage']) && $mediaInfo['cadrage']) { $value .= $this->mediaFilesLocation . strtolower($mediaInfo['name']) . '" w="' . $mediaInfo['width'] . '" h="' . $mediaInfo['height'] . '" posx="' . $mediaInfo['x'] . '" posy="' . $mediaInfo['y'] . '" rotation="' . $mediaInfo['rotation'] . '" miroir="' . $mediaInfo['miroir'] . '" do_cadrage="1">'; } else { $value .= $this->mediaFilesLocation . strtolower($mediaInfo['name']) . '" do_cadrage="0">'; } } else if (!empty($objectNameLink)) { $value .= $this->mediaFilesLocation . strtolower($objectNameLink) . '" do_cadrage="0">'; } } else { // HERE WE JUST NEED TO CREATE AN HREF ARRAY NO BALISE NEEDED ! $value = $this->mediaFilesLocation . strtolower($name); } } else { $val = $mediaId; $crop = $exportMediaId; if (isset($mediaId) && -1 !== $mediaId) { $value = "mediaId__" . $val . "__cropId__" . (isset($crop) ? $crop : -1); } else { if ($isObject) { if (isset($media->linkData) && isset($media->linkData["link"])) { $value = $media->linkData["link"]; } else { $value = ""; } } else { $value = $media["link"]; } } if ($med) { if ($med->getWidth() > 0) { $value .= '" width="' . $med->getWidth() . 'px'; } if ($med->getHeight() > 0) { $value .= '" height="' . $med->getHeight() . 'px'; } } } return $value; } /** * * @return string [description] */ private function addBreakLine(): string { return "BreakLine"; } /** * @param [type] $value [description] * @param array $rules [description] * * @return string [description] */ private function txtToXml($value, array $rules = []): string { $BALISE_SUP = $rules['sup'] ?? null; $BALISE_STRONG = $rules['strong'] ?? 'TEXTE_BOLD'; $BALISE_EM = $rules['em'] ?? 'TEXTE_ITAL'; $BALISE_STRONGEM = $rules['strongem'] ?? 'TEXTE_BOLD_ITAL'; $BALISE_EURO = array_key_exists('euro', $rules) ? $rules['euro'] : 'COM_EURO'; $BALISE_FORCED_BREAK_COLUMN = array_key_exists('forced_break_column_balise', $rules) ? $rules['forced_break_column_balise'] : false; // $TYPO = array_key_exists('typo', $rules) ? $rules['typo'] : true; $FORCE_LINEBREAK = array_key_exists('force_linebreak', $rules) ? $rules['force_linebreak'] : true; $value = $this->trim($value); $value = $this->fixStrongEm($value); $value = preg_replace_callback('/( | |\()([VIX]{1,5})(e)( | |\.|\))/', function ($mathches) { return $mathches[1] . ']]>', '
', '
', "'"," "], [' ', '', '', '
', '
', "\n", '’'," "], $value); //SPECIAL CHAR $insecable = "]]>SpecialCharacters.nonbreakingSpaceSpecialCharacters.forcedLineBreak\nSpecialCharacters.indentHereTab', '', '', '
'], [']]><' . $BALISE_STRONGEM . '><' . $BALISE_STRONGEM . '>', ''], [']]><' . $BALISE_STRONG . '>', ''], [']]><' . $BALISE_EM . '>', ''], [']]><' . $BALISE_SUP . '><' . $BALISE_EURO . '>SpecialCharacters.columnBreak<' . $BALISE_FORCED_BREAK_COLUMN . '>SpecialCharacters.columnBreakSpecialCharacters.forcedLineBreaktrim($value); $value = ''; return $value; } // Place the value in the twig private function txtToHtml($values, int $templateId, array $elementGabValues) { $template = $this->templateRepo->findById($templateId); $numberElementGabValues = $template->getNbElement(); if (!empty($template)) { $twig = $template->getTwig()->getContent(); $indexElement = 0; foreach ($values["products"] as $id => $product) { if ($id !== 0) { $indexElement = array_search($id, array_column($elementGabValues['values'], 'id')); } if (is_int($indexElement) && $indexElement >= 0) { $elementGabValue = $elementGabValues['values'][$indexElement]; $mediasGabValue = $elementGabValue->medias; $medias = $template->getMedias(); if (!$medias->isEmpty()) { foreach ($medias as $media) { $valueFilter = array_filter($mediasGabValue, function ($med) use ($media) { return $med->position === $media->getPosition(); }); if (!empty($valueFilter)) { $val = reset($valueFilter)->mediaId; $crop = reset($valueFilter)->exportMediaId; $values['products'][$id][$media->getMarker()] = "mediaId__" . $val . "__cropId__" . $crop; if ($media->getWidth() > 0) { $values['products'][$id][$media->getMarker()] .= '" width="' . $media->getWidth() . 'px'; } if ($media->getHeight() > 0) { $values['products'][$id][$media->getMarker()] .= '" height="' . $media->getHeight() . 'px'; } } } } } } // if ($numberElementGabValues <= 1) { // $values = reset($values['products']); // } $headers = $this->dedicatedFile->findByTemplateIdAndExtension($templateId, 'html'); if (!empty($headers)) { foreach ($headers as $header) { file_put_contents($this->params->get('arborescence_base') . '/templates/' . $header->getOriginalName(), $header->getFile()); } } file_put_contents($this->params->get('arborescence_base') . '/templates/test.html.twig', $twig); $this->twig->enableAutoReload(); //@TODO : Only clean cache when templated is updated try { $twig = $this->twig->render('test.html.twig', $values); } catch (Exception $e) { $this->logService->setLog($e->getMessage()); throw new UnprocessableEntityMaestroException($e->getMessage(), "twig", "render", false); } unlink($this->params->get('arborescence_base') . '/templates/test.html.twig'); } return $twig; } /** * @param [type] $str [description] * * @return [type] [description] */ private function trim($str) { $str = preg_replace('/^((?:\s*)| |\s|\\n)+|(((?:\s*)| |\s|\\n)+$)/is', '', $str); return $str; } /** * @param [type] $str [description] * * @return [type] [description] */ private function fixStrongEm($str) { $str = str_replace(['
', "\r", "\n"], ['
', '', ''], $str); $lenStrong = strlen(''); $lenEm = strlen(''); $lenTxt = strlen($str); $flagStrong = 0; $flagEm = 0; $return = null; for ($i = 0; $i < $lenTxt;) { if ('' === substr($str, $i, $lenStrong)) { if (0 === $flagEm) { $return .= ''; } else { $return .= ''; } ++$flagStrong; $i += $lenStrong; } elseif ('' === substr($str, $i, ($lenStrong + 1))) { if (0 === $flagEm) { $return .= ''; } else { $return .= ''; } --$flagStrong; $i += $lenStrong + 1; } elseif ('' === substr($str, $i, $lenEm)) { if (0 === $flagStrong) { $return .= ''; } else { $return .= ''; } ++$flagEm; $i += $lenEm; } elseif ('' === substr($str, $i, ($lenEm + 1))) { if (0 === $flagStrong) { $return .= ''; } else { $return .= ''; } --$flagEm; $i += $lenEm + 1; } else { $return .= substr($str, $i, 1); ++$i; } } $array_txt = explode('
', $return); $return = null; $flag_open_strong = 0; $flag_open_em = 0; foreach ($array_txt as $string) { $nb_debut_strong = substr_count($string, ''); $nb_fin_strong = substr_count($string, ''); $nb_debut_em = substr_count($string, ''); $nb_fin_em = substr_count($string, ''); if ($flag_open_em) { for ($i = 0; $i < $flag_open_em; ++$i) { $return .= ''; ++$nb_debut_em; } $flag_open_em = 0; } if ($flag_open_strong) { for ($i = 0; $i < $flag_open_strong; ++$i) { $return .= ''; ++$nb_debut_strong; } $flag_open_strong = 0; } $return .= $string; if ($nb_debut_strong > $nb_fin_strong) { $not_closed = $nb_debut_strong - $nb_fin_strong; for ($i = 0; $i < $not_closed; ++$i) { $return .= ''; ++$flag_open_strong; } } if ($nb_debut_em > $nb_fin_em) { $not_closed = $nb_debut_em - $nb_fin_em; for ($i = 0; $i < $not_closed; ++$i) { $return .= ''; ++$flag_open_em; } } $return .= '
'; } $return = substr($return, 0, -6); $return = str_replace(['', '', ''], '', $return); return $return; } private function getMediaInformation($media, $allMedias, $positionMedia) { $crop = [ "width" => $media->width, "height" => $media->height, "x" => $media->x, "y" => $media->y, "rotate" => $media->rotate, "flipVertical" => $media->flipVertical, "flipHorizontal" => $media->flipHorizontal, ]; $meds = array_filter($allMedias, function ($med) use ($media) { return $med['mediaId'] == $media->mediaId; }); if (!empty($meds)) { $meds = reset($meds); if (isset($meds['fileBd']) && $meds['fileBd']) { $file = isset($meds['file']) ? $meds['file'] : ""; $fileBD = $meds['fileBd']; $isBase64 = true; } else { $file = isset($meds['filePath']) ? $meds['filePath'] : ""; $fileBD = isset($meds['fileBdPath']) ? $meds['fileBdPath'] : ""; $isBase64 = false; } if (!$isBase64) { $damUrl = $this->params->get('domain_dam'); try { $fileStream = fopen($damUrl . $file, 'r'); $imageBlob = stream_get_contents($fileStream); fclose($fileStream); } catch (\Throwable $th) { $fileStream = fopen($damUrl . "/default/default.png", 'r'); $file = "/default/default.png"; $imageBlob = stream_get_contents($fileStream); fclose($fileStream); } if(empty($imageBlob)) { throw new Exception("Invalide file path: " . $damUrl . $file); } } else { $imageBlob = base64_decode($file); } $obj_imagick = new Imagick(); if (strrpos($meds["originalName"], '.ai', -3) && $imageBlob) { $obj_imagick->setResolution(72, 72); // $obj_imagick->setResolution(300,300); $obj_imagick->readImageBlob($imageBlob); $obj_imagick->setImageFormat('jpeg'); } else { $obj_imagick->readImageBlob($imageBlob); } if (!$isBase64) { if ($fileBD) { if ($fileStream = fopen($damUrl . $fileBD, 'r')) { $imageBdBlob = stream_get_contents($fileStream); fclose($fileStream); } else { throw new Exception("Invalide file path: " . $damUrl . $fileBD); } } else { $imageBdBlob = ""; } } else { $imageBdBlob = base64_decode($fileBD); } $original_imagick = new Imagick(); if ($imageBdBlob) { if (strrpos($meds["originalName"], '.ai', -3)) { $original_imagick->setResolution(72, 72); // $original_imagick->setResolution(300,300); $original_imagick->readImageBlob($imageBdBlob); $original_imagick->setImageFormat('jpeg'); } else { $original_imagick->readImageBlob($imageBdBlob); } } else { $original_imagick = null; } $cadrage = false; if (null !== $original_imagick) { if ($crop['width'] > 0 && $crop['height'] > 0 && $positionMedia->getWidth() > 0 && $positionMedia->getHeight() > 0) { $ratioWidth = $obj_imagick->getImageWidth() / $original_imagick->getImageWidth(); $ratioHeight = $obj_imagick->getImageHeight() / $original_imagick->getImageHeight(); $width = $ratioWidth * $crop['width']; $height = $ratioHeight * $crop['height']; $x = $crop['x'] * $ratioWidth; $y = $crop['y'] * $ratioHeight; // Rotation et miroir // if ($crop['rotate'] !== 0) { // $obj_imagick->rotateImage(new ImagickPixel('#00000000'), $crop['rotate']); // } // if ($crop['flipHorizontal']) { // $obj_imagick->flopImage(); // } // if ($crop['flipVertical']) { // $obj_imagick->flipImage(); // } $cadrage = true; } else { $width = $original_imagick->getImageWidth(); $height = $original_imagick->getImageHeight(); $x = $original_imagick->getImageWidth(); $y = $original_imagick->getImageHeight(); } } else { $width = $obj_imagick->getImageWidth(); $height = $obj_imagick->getImageHeight(); $x = $obj_imagick->getImageWidth(); $y = $obj_imagick->getImageHeight(); } } return ["name" => $file, "width" => $width, "height" => $height, "x" => $x != 0 ? -$x : $x, "y" => $y != 0 ? -$y : $y, "rotation" => $crop['rotate'], "miroir" => !empty($crop['flipHorizontal']) && $crop['flipHorizontal'] ? true : false, "cadrage" => $cadrage]; } /** * {@inheritDoc} */ public function validateContent(string $content, bool $isXml = true): array { $err = []; $doc = false; $prev = libxml_use_internal_errors(true); if (!$isXml) { $start = strpos($content, '<'); $end = strrpos($content, '>', $start); $len = strlen($content); if ($end !== false) { $contentEdited = substr($content, $start); } else { $contentEdited = substr($content, $start, $len - $start); } simplexml_load_string($contentEdited); $errors = libxml_get_errors(); if (!empty($errors)) { libxml_clear_errors(); $dom = new \DOMDocument(); $dom->loadHTML($content); $errors = libxml_get_errors(); // Give more accurate errors } } else { $doc = simplexml_load_string($content); $errors = libxml_get_errors(); } libxml_clear_errors(); libxml_use_internal_errors($prev); foreach ($errors as $error) { $err[] = $error->message; } if (false === $doc && $isXml) { $err[] = "XML string to object conversion error"; } return $err; } /** * {@inheritDoc} */ public function isHtml(string $content): bool { return false !== stripos($content, ''); } /** * {@inheritDoc} */ public function getValidationErrorList(array $errors): string { $errorList = ""; foreach ($errors as $error) { $errorList .= "- $error
"; } return $errorList; } }