Skip to content

Commit d4b4826

Browse files
authored
Merge pull request #620 from kojoty/pju-imagegd-fix
#619: imagegd2 is no used as it is removed from latest libgd
2 parents 11df72b + 027878c commit d4b4826

File tree

1 file changed

+22
-39
lines changed

1 file changed

+22
-39
lines changed

okapi/services/caches/map/TileRenderer.php

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class TileRenderer
1515
* Changing this will affect all generated hashes. You should increment it
1616
* whenever you alter anything in the drawing algorithm.
1717
*/
18-
private static $VERSION = 61;
18+
private static $VERSION = 62;
1919

2020
/**
2121
* Should be always true. You may temporarily set it to false, when you're
@@ -115,13 +115,13 @@ private static function get_image($name, $opacity=1, $brightness=0,
115115
try
116116
{
117117
$cache_key = "tilesrc/".Okapi::getGitRevision()."/".self::$VERSION."/".$key;
118-
$gd2_path = self::$USE_STATIC_IMAGE_CACHE
118+
$img_path = self::$USE_STATIC_IMAGE_CACHE
119119
? FileCache::get_file_path($cache_key) : null;
120-
if ($gd2_path === null)
120+
if ($img_path === null)
121121
throw new Exception("Not in cache");
122122
# File cache hit. GD2 files are much faster to read than PNGs.
123123
# This can throw an Exception (see bug#160).
124-
$locmem_cache[$key] = imagecreatefromgd2($gd2_path);
124+
$locmem_cache[$key] = imagecreatefrompng($img_path);
125125
}
126126
catch (Exception $e)
127127
{
@@ -132,7 +132,12 @@ private static function get_image($name, $opacity=1, $brightness=0,
132132
# Apply all wanted effects.
133133

134134
if ($opacity != 1)
135-
self::change_opacity($locmem_cache[$key], $opacity);
135+
{
136+
imagealphablending($locmem_cache[$key], false);
137+
imagesavealpha($locmem_cache[$key], true);
138+
imagefilter($locmem_cache[$key], IMG_FILTER_COLORIZE, 0,0,0,127*$opacity);
139+
imagealphablending($locmem_cache[$key], true);
140+
}
136141
if ($contrast != 0)
137142
imagefilter($locmem_cache[$key], IMG_FILTER_CONTRAST, $contrast);
138143
if ($brightness != 0)
@@ -146,37 +151,15 @@ private static function get_image($name, $opacity=1, $brightness=0,
146151
# Cache the result.
147152

148153
ob_start();
149-
imagegd2($locmem_cache[$key]);
150-
$gd2 = ob_get_clean();
151-
FileCache::set($cache_key, $gd2);
154+
imagesavealpha($locmem_cache[$key], true);
155+
imagepng($locmem_cache[$key]);
156+
$png_img = ob_get_clean();
157+
FileCache::set($cache_key, $png_img);
152158
}
153159
}
154160
return $locmem_cache[$key];
155161
}
156162

157-
/**
158-
* Extremely slow! Remember to cache the result!
159-
*/
160-
private static function change_opacity($im, $ratio)
161-
{
162-
imagealphablending($im, false);
163-
164-
$w = imagesx($im);
165-
$h = imagesy($im);
166-
167-
for($x = 0; $x < $w; $x++)
168-
{
169-
for($y = 0; $y < $h; $y++)
170-
{
171-
$color = imagecolorat($im, $x, $y);
172-
$new_color = ((max(0, floor(127 - ((127 - (($color >> 24) & 0x7f)) * $ratio))) & 0x7f) << 24) | ($color & 0x80ffffff);
173-
imagesetpixel($im, $x, $y, $new_color);
174-
}
175-
}
176-
177-
imagealphablending($im, true);
178-
}
179-
180163
private function draw_cache(&$cache_struct)
181164
{
182165
$capt = ($cache_struct[6] & TileTree::$FLAG_DRAW_CAPTION);
@@ -345,8 +328,8 @@ private function get_caption($cache_id, $name_crc)
345328
# Check cache.
346329

347330
$cache_key = "tilecaption/".self::$VERSION."/".$cache_id."/".$name_crc;
348-
$gd2 = self::$USE_CAPTIONS_CACHE ? Cache::get($cache_key) : null;
349-
if ($gd2 === null)
331+
$caption_png = self::$USE_CAPTIONS_CACHE ? Cache::get($cache_key) : null;
332+
if ($caption_png === null)
350333
{
351334
# We'll work with 16x bigger image to get smoother interpolation.
352335

@@ -419,12 +402,13 @@ private function get_caption($cache_id, $name_crc)
419402
# Cache it!
420403

421404
ob_start();
422-
imagegd2($small);
423-
$gd2 = ob_get_clean();
424-
Cache::set_scored($cache_key, $gd2);
405+
imagesavealpha($small, true);
406+
imagepng($small);
407+
$caption_png = ob_get_clean();
408+
Cache::set_scored($cache_key, $caption_png);
425409
}
426410

427-
return imagecreatefromstring($gd2);
411+
return imagecreatefromstring($caption_png);
428412
}
429413

430414
private function draw_cache_medium(&$cache_struct)
@@ -564,8 +548,7 @@ private function draw_cache_tiny(&$cache_struct)
564548

565549
if (($status != 1) && ($count == 1))
566550
{
567-
$icon = self::get_image(($status == 2) ? "status_unavailable"
568-
: "status_archived");
551+
$icon = self::get_image(($status == 2) ? "status_unavailable" : "status_archived");
569552
imagecopy($this->im, $icon, $px - ($center_x - $markercenter_x) - 6,
570553
$py - ($center_y - $markercenter_y) - 8, 0, 0, 16, 16);
571554
}

0 commit comments

Comments
 (0)