diff options
author | Javier S. Pedro <maemo@javispedro.com> | 2011-07-01 02:24:59 +0200 |
---|---|---|
committer | Javier S. Pedro <maemo@javispedro.com> | 2011-07-01 02:24:59 +0200 |
commit | 052ae063c2937fc5ff2aea6313bc0e09714fefc2 (patch) | |
tree | 410e9755b18ec30361ca8eb570409e2f3d8a3c05 | |
parent | 2ec3f28f98391ee3a1844dd5999053e1e3976d40 (diff) | |
download | hicg-052ae063c2937fc5ff2aea6313bc0e09714fefc2.tar.gz hicg-052ae063c2937fc5ff2aea6313bc0e09714fefc2.zip |
fix shadows
-rw-r--r-- | index.php | 60 | ||||
-rw-r--r-- | template.svg | 6 |
2 files changed, 48 insertions, 18 deletions
@@ -30,7 +30,7 @@ function svg2png($svg, $w, $h, $png) $exitcode = -1; $cmd = sprintf("~/bin/rsvg-convert --format=png --width=%d --height=%d --output=%s %s", $w, $h, escapeshellarg($png), escapeshellarg($svg)); exec($cmd, $output, $exitcode); - return $exitcode == 0; + return $exitcode == 0 && file_exists($png); } function multiarray($d, $k, $n = 0) @@ -120,21 +120,29 @@ function extract_dominant_color($svg, $algo = 'average', $filter = 'grey') global $domalgos, $colfilters; $png = '/tmp/hicg-icon-' . uniqid() . '.png'; if (!in_array($algo, $domalgos)) { + warn('Invalid dominant color extraction algorithm'); return false; } if (!in_array($filter, $colfilters)) { + warn('Invalid colors to ignore'); return false; } if (!svg2png($svg, COLOR_EXTRACT_RENDER_SIZE, COLOR_EXTRACT_RENDER_SIZE, $png)) { + warn('Initial rasterization failed (not an .svg file?)'); return false; } $img = imagecreatefrompng($png); if (!$img) { + warn('Initial rasterization generated incorrect PNG'); return false; } $color = call_user_func('domcolor_'. $algo, $img, 'colfilter_'.$filter); imagedestroy($img); unlink($png); + if (!$color) { + warn("I failed to get a dominant color. Your icon might be too greyish, so I'm making it grey."); + return array(128, 128, 128); + } return $color; } @@ -244,7 +252,7 @@ function make_icon($src, $template, $dst, $color, $shadow) /* Add drop shadow filter if selected. */ if ($shadow) { - $g->setAttribute('filter', 'hicg_drop_shadow'); + $g->setAttribute('filter', 'url(#hicg_drop_shadow)'); } foreach ($srcNode->childNodes as $node) { @@ -255,16 +263,21 @@ function make_icon($src, $template, $dst, $color, $shadow) $doc->documentElement->appendChild($g); $doc->save($dst); + + return true; } function make_png($src, $dst) { if (!svg2png($src, HARMATTAN_ICON_SIZE, HARMATTAN_ICON_SIZE, $dst)) { warn("I failed to generate a 80x80 PNG."); + return false; } + return true; } $warnings = array(); +$loadsample = true; if (@isset($_POST['send'])) { $prefix = './files/' . uniqid() . '_'; @@ -279,28 +292,49 @@ if (@isset($_POST['send'])) { $pngfile = $prefix . 'icon.png'; $domcolor = extract_dominant_color($srcfile, $domalgo, $colfilter); - if (!$domcolor) { - warn("I failed to get a dominant color. Your icon might be too greyish."); - $domcolor = array(0, 0, 0); - $discolor = array(0, 0, 0); - $prscolor = array(0, 0, 0); + if ($domcolor) { + $loadsample = false; } else { $discolor = get_disabled_color($domcolor); $prscolor = get_pressed_color($domcolor); } - make_icon($srcfile, 'template.svg', $iconfile, $domcolor, $dropshadow); - make_png($iconfile, $pngfile); + if (!$loadsample) { + make_icon($srcfile, 'template.svg', $iconfile, $domcolor, $dropshadow); + make_png($iconfile, $pngfile); + } else { + /* Something went really wrong and we will show the sample, so delete uploaded SVG. */ + unlink($srcfile); + } $srcurl = $srcfile; $iconurl = $iconfile; $pngurl = $pngfile; -} else { +} + +if (php_sapi_name() == 'cli' && $argc >= 2) { + $loadsample = false; + $srcfile = $argv[1]; + $domalgo = 'mode'; + $dropshadow = true; + $colfilter = 'grey'; + $iconfile = 'cliicon.svg'; + $pngfile = 'cliicon.png'; + + $domcolor = extract_dominant_color($srcfile, $domalgo, $colfilter); + + make_icon($srcfile, 'template.svg', $iconfile, $domcolor, $dropshadow); + make_png($iconfile, $pngfile); + + exit(0); +} + +if ($loadsample) { /* Load some sample data. */ $srcfile = 'samplesrc.svg'; $domalgo = 'mode'; $dropshadow = true; - $colfilter = 'grey5'; + $colfilter = 'grey'; $iconfile = 'sampleicon.svg'; $pngfile = 'sampleicon.png'; @@ -308,10 +342,6 @@ if (@isset($_POST['send'])) { $discolor = get_disabled_color($domcolor); $prscolor = get_pressed_color($domcolor); - /* This should not get into production. ;) */ - make_icon($srcfile, 'template.svg', $iconfile, $domcolor, $dropshadow); - make_png($iconfile, $pngfile); - $srcurl = $srcfile; $iconurl = $iconfile; $pngurl = $pngfile; diff --git a/template.svg b/template.svg index 5bef241..a89ac21 100644 --- a/template.svg +++ b/template.svg @@ -5,9 +5,9 @@ <stop offset="0" style="stop-color:#FFFFFF"/> <stop offset="1" style="stop-color:#000000"/> </linearGradient> - <filter id="hicg_drop_shadow"> - <feOffset in="SourceGraphic" dx="0" dy="2" /> - <feGaussianBlur stdDeviation="5" /> + <filter id="hicg_drop_shadow" x="0%" y="0%" width="100%"> + <feOffset in="SourceAlpha" dx="0" dy="4" /> + <feGaussianBlur stdDeviation="4" /> <feColorMatrix type="matrix" values="1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0.5 0" result="shadow" /> <feBlend in="SourceGraphic" in2="shadow" mode="normal" /> </filter> |