' . "\n"; $skipCache = false; // DEBUG if ($skipCache || !file_exists($cacheFileName) || filemtime($fileName) > filemtime($cacheFileName)) { // Cache file is outdated, recreate the data $ystep = 5; $yextra = $hspace / 2; $leftSide = $side == 'left'; #$t0 = microtime(true); $im = imagecreatefrompng($fileName); $divs = array(); $prevDiv = null; // For each block ow rows (group of $ystep rows) for ($basey = 0; $basey < $height; $basey += $ystep) { // Find the farest X position of transparency in all rows of this group $rowx = $leftSide ? 0 : $width - 1; for ($y = max(0, $basey - $yextra); $y < $height && $y < $basey + $ystep - 1 + $yextra; $y++) { if ($leftSide) { // For each row in the group for ($x = $width - 1; $x >= $rowx; $x--) { $color = imagecolorat($im, $x, $y); #$alpha = ($color & 0x7F000000) >> 24; // Only for 32-bit colour images $colors = imagecolorsforindex($im, $color); $alpha = $colors['alpha']; if ($alpha < 127) break; // 127 is the maximum transparency (PHP doesn't use opacity!) } $rowx = max($x, $rowx); } else { // For each row in the group for ($x = 0; $x <= $rowx; $x++) { $color = imagecolorat($im, $x, $y); #$alpha = ($color & 0x7F000000) >> 24; // Only for 32-bit colour images $colors = imagecolorsforindex($im, $color); $alpha = $colors['alpha']; if ($alpha < 127) break; // 127 is the maximum transparency (PHP doesn't use opacity!) } $rowx = min($x, $rowx); } } // Set the image shape div's size $w = $leftSide ? $rowx : $width - 1 - $rowx; if ($w > 0) $w += $hspace; $h = $ystep; // Compare the current div's size with the previous' if (isset($prevDiv) && abs($prevDiv[1] - $w) < 5) { // (Almost) same width, combine both divs in height to save HTML code $lastIndex = count($divs) - 1; $divs[$lastIndex][0] += $h; $divs[$lastIndex][1] = max($divs[$lastIndex][1], $w); } else { // Different width, add new div $prevDiv = array($h, $w); $divs[] = $prevDiv; } } #echo ' [' . round((microtime(true) - $t0) * 1000) . ' ms] '; // Regard the top and bottom margin for the first and last block $divs[0][0] += $top; $divs[count($divs) - 1][0] += $bottom; // Print out all divs $cache = ''; foreach ($divs as $div) { // Insert top margin before the first spacer element if (!strlen($cache) && $marginTop) { $cache .= '
' . "\n"; } list($h, $w) = $div; $cache .= '' . "\n"; } // Create cache directory and file if (!is_dir(dirname($cacheFileName))) mkdir(dirname($cacheFileName), 0777, true); file_put_contents($cacheFileName, $cache); // Print output echo $cache; } else { // Print cached file readfile($cacheFileName); } } ?>