From 052ae063c2937fc5ff2aea6313bc0e09714fefc2 Mon Sep 17 00:00:00 2001 From: "Javier S. Pedro" Date: Fri, 1 Jul 2011 02:24:59 +0200 Subject: fix shadows --- index.php | 60 +++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 15 deletions(-) (limited to 'index.php') diff --git a/index.php b/index.php index a57606d..8c8f2e3 100644 --- a/index.php +++ b/index.php @@ -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; -- cgit v1.2.3