Jump to content

Irregular image outline with GD


Nameless One

Recommended Posts

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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.