static function renderPreviewSVG($AppearanceID) { global $CGPath, $CGDb; $OutputPath = str_replace('#', $AppearanceID, self::PREVIEW_SVG_PATH); $FileRelPath = "{$CGPath}/v/{$AppearanceID}p.svg"; if (file_exists($OutputPath)) { Image::outputSVG(null, $OutputPath, $FileRelPath); } $SVG = ''; $colors = array(); $ColorQuery = $CGDb->rawQuery('SELECT c.hex FROM colors c LEFT JOIN colorgroups cg ON c.groupid = cg.groupid WHERE cg.ponyid = ? ORDER BY cg."order" ASC, c."order" ASC LIMIT 4', array($AppearanceID)); if (empty($ColorQuery)) { CoreUtils::notFound(); } usort($ColorQuery, function ($a, $b) { return CoreUtils::yiq($b['hex']) <=> CoreUtils::yiq($a['hex']); }); switch (count($ColorQuery)) { case 1: $SVG .= "<rect x='0' y='0' width='2' height='2' fill='{$ColorQuery[0]['hex']}'/>"; break; case 3: $SVG .= <<<XML <rect x='0' y='0' width='2' height='2' fill='{$ColorQuery[0]['hex']}'/> <rect x='0' y='1' width='1' height='1' fill='{$ColorQuery[1]['hex']}'/> <rect x='1' y='1' width='1' height='1' fill='{$ColorQuery[2]['hex']}'/> XML; break; case 2: case 4: $x = 0; $y = 0; foreach ($ColorQuery as $c) { $w = $x % 2 == 0 ? 2 : 1; $h = $y % 2 == 0 ? 2 : 1; $SVG .= "<rect x='{$x}' y='{$y}' width='{$w}' height='{$h}' fill='{$c['hex']}'/>"; $x++; if ($x > 1) { $x = 0; $y = 1; } } break; } $SVG = "<svg version='1.1' xmlns='http://www.w3.org/2000/svg' viewBox='0 0 2 2' enable-background='new 0 0 2 2' xml:space='preserve' preserveAspectRatio='xMidYMid slice'>{$SVG}</svg>"; Image::outputSVG($SVG, $OutputPath, $FileRelPath); }