Nameless One Posted December 7, 2009 Share Posted December 7, 2009 I have a PNG image that depicts a single item of irregular shape, much like the phpfreaks logo. I would like to add a 1px-2px thick white outline around that shape with GD. Is there any way to do this? ImageMagick is not an option. Link to comment https://forums.phpfreaks.com/topic/184274-irregular-image-outline-with-gd/ Share on other sites More sharing options...
Nameless One Posted December 8, 2009 Author Share Posted December 8, 2009 Here's the code I came up with: function _create_slideshow_image($image, $src_width, $src_height, $outline_ratio, $outline_transparency = 0) { $width = 300; $height = 400; $outline_width = round($width*($outline_ratio + 1)); $outline_height = round($height*($outline_ratio + 1)); $slideshow_image_temp = $this->_resize_image($image, $src_width, $src_height, $outline_width, $outline_height); $outline_width = imagesx($slideshow_image_temp); $outline_height = imagesy($slideshow_image_temp); $slideshow_image_outline = imagecreatetruecolor($outline_width, $outline_height); imageantialias($slideshow_image_outline, TRUE); imagealphablending($slideshow_image_outline, FALSE); imagesavealpha($slideshow_image_outline, TRUE); for ($i=1; $i<=$outline_width; $i++) for ($j=1; $j<=$outline_height; $j++) { $color = imagecolorat($slideshow_image_temp, $i, $j); $transparency = ($color & 0x7F000000) >> 24; $white = imagecolorallocatealpha($slideshow_image_outline, 255, 255, 255, $outline_transparency); $transparent = imagecolorallocatealpha($slideshow_image_outline, 0, 0, 0, 127); if ($transparency > 100) imagesetpixel($slideshow_image_outline, $i, $j, $transparent); else imagesetpixel($slideshow_image_outline, $i, $j, $white); } imagecolortransparent($slideshow_image_outline, $transparent); $slideshow_image_temp_reduced = $this->_resize_image($image, $src_width, $src_height, $width, $height); $width = imagesx($slideshow_image_temp_reduced); $height = imagesy($slideshow_image_temp_reduced); $reduction_x = round(($outline_width - $width)/2); $reduction_y = round(($outline_height - $height)/2); imagealphablending($slideshow_image_outline, TRUE); imagealphablending($slideshow_image_temp_reduced, TRUE); imagecopy($slideshow_image_outline, $slideshow_image_temp_reduced, $reduction_x, $reduction_y, 0, 0, $width, $height); return $slideshow_image_outline; } It creates a white outline by creating a white silhouette of the item on the source image which is slightly larger than the target size of the item, then pastes the items itself (resized to target size) over the silhouette. However, my pixel by pixel colour setting leaves much to be desired in the terms of quality. The edges are jagged, as you can see in the example I attached, and applying filters with imageconvolution() breaks the transparency since it probably processes the transparent parts of the image as well. Any suggestions for improving this piece of code? [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/184274-irregular-image-outline-with-gd/#findComment-973711 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.