我建立了一個執行標準 9 切片縮放技術的 PHP 函式。這對於縮圖陰影縮放以及任何涉及面板設計都非常有用。歡迎參考及使用
注意:我的 9 切片程式碼沒有指定邊距,而是使用居中矩形的概念... 輸入提供圖片(作為資源)、矩形的 x 和 y 座標,以及矩形的寬度和高度。
$src_im 參數應為圖片資源。此程式碼是用於 9 切片半透明 PNG 圖片,並且僅使用半透明 PNG 圖片進行過測試,但它應該適用於其他圖片類型(可能需要一些修改)
因此,如果您的來源圖片為 400 x 400,所有邊都需要 24 像素的邊距,而目標大小為 800 x 500,則應使用以下參數
<?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;
}
?>