I have created a PHP function which performs the standard 9-Slice scaling technique. This is extremely useful for thumbnail shadow scaling, and anything involving skinning. Feel free to pick apart and use
Note: instead of specifying margins, my 9-slicing routine uses a centered-rectangle concept... as input you provide the image (as a resource), the x and y coords of the rectangle, and the width and height of the rectangle.
The $src_im parameter should be an image resource. This script was written for 9-slicing translucent PNG images, and has only been tested with translucent PNG images, however it should work with other image types (possibly requiring some modification)
so if your source image was 400 x 400, you needed a 24 pixel margin on all sides, and your target size was 800 x 500, you would use the following parameters:
<?php
$im = NineSlice($im, 24, 24, 352, 352, 800, 500)
?>
<?php
function NineSlice($src_im, $rect_x, $rect_y, $rect_w, $rect_h, $target_w, $target_h)
{
$src_w = imagesx($src_im);
$src_h = imagesy($src_im);
$im = CreateBlankPNG($target_w, $target_h);
imagealphablending($im,true);
$left_w = $rect_x;
$right_w = $src_w - ($rect_x + $rect_w);
$left_src_y = ceil($rect_h / 2) - 1 + $rect_y;
$right_src_y = $left_src_y;
$left_src_x = 0;
$right_src_x = $left_w + $rect_w;
$top_src_x = ceil($rect_w / 2) - 1 + $rect_x;
$bottom_src_x = $top_src_x;
$bottom_src_y = $rect_y + $rect_h;
$bottom_h = $src_h - $bottom_src_y;
$left_tile = CreateBlankPNG($left_w, 1);
imagecopy($left_tile, $src_im, 0, 0, 0, $left_src_y, $left_w, 1);
$right_tile = CreateBlankPNG($right_w, 1);
imagecopy($right_tile, $src_im, 0, 0, $right_src_x, $right_src_y, $right_w, 1);
$top_tile = CreateBlankPNG(1, $rect_y);
imagecopy($top_tile, $src_im, 0, 0, $top_src_x, 0, 1, $rect_y);
$bottom_tile = CreateBlankPNG(1, $bottom_h);
imagecopy($bottom_tile, $src_im, 0, 0, $bottom_src_x, $bottom_src_y, 1, $bottom_h);
$inner_tile = CreateBlankPNG(4, 4);
imagecopy($inner_tile, $src_im, 0, 0, ceil($src_w / 2) - 1, ceil($src_h / 2) - 1, 4, 4);
imagecopy($im, $src_im, 0, 0, 0, 0, $left_w, $rect_y);
imagecopy($im, $src_im, 0, $target_h - $bottom_h, 0, $bottom_src_y, $rect_x, $bottom_h);
imagecopy($im, $src_im, $target_w - $right_w, 0, $right_src_x, 0, $right_w, $rect_y);
imagecopy($im, $src_im, $target_w - $right_w, $target_h - $bottom_h, $src_w - $right_w, $bottom_src_y, $right_w, $bottom_h);
imagesettile($im, $top_tile);
imagefilledrectangle($im, $left_w, 0, $target_w - $right_w - 1, $rect_y, IMG_COLOR_TILED);
imagesettile($im, $left_tile);
imagefilledrectangle($im, 0, $rect_y, $left_w, $target_h - $bottom_h - 1, IMG_COLOR_TILED);
$right_side = CreateBlankPNG($right_w, $target_h - $rect_y - $bottom_h);
imagesettile($right_side, $right_tile);
imagefilledrectangle($right_side, 0, 0, $right_w, $target_h - $rect_y - $bottom_h, IMG_COLOR_TILED);
imagecopy($im, $right_side, $target_w - $right_w, $rect_y, 0, 0, $right_w, $target_h - $rect_y - $bottom_h);
$bottom_side = CreateBlankPNG($target_w - $right_w - $left_w, $bottom_h);
imagesettile($bottom_side, $bottom_tile);
imagefilledrectangle($bottom_side, 0, 0, $target_w - $right_w - $left_w, $bottom_h, IMG_COLOR_TILED);
imagecopy($im, $bottom_side, $right_w, $target_h - $bottom_h, 0, 0, $target_w - $right_w - $left_w, $bottom_h);
imagedestroy($left_tile);
imagedestroy($right_tile);
imagedestroy($top_tile);
imagedestroy($bottom_tile);
imagedestroy($inner_tile);
imagedestroy($right_side);
imagedestroy($bottom_side);
return $im;
}
function CreateBlankPNG($w, $h)
{
$im = imagecreatetruecolor($w, $h);
imagesavealpha($im, true);
$transparent = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagefill($im, 0, 0, $transparent);
return $im;
}
?>