note 93690 added to function.imagecopy

From: Date: Wed, 23 Sep 2009 02:29:55 +0000
Subject: note 93690 added to function.imagecopy
Groups: php.notes 
Request: Send a blank email to php-notes+get-160963@lists.php.net to get a copy of this message
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 /* 9-Slice Image Slicing Script by Sam Kelly (DuroSoft: http://www.durosoft.com) */ 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; } ?> ---- Server IP: 64.71.164.2 Probable Submitter: 69.120.12.34 ---- Manual Page -- http://www.php.net/manual/en/function.imagecopy.php Edit -- https://master.php.net/note/edit/93690 Del: integrated -- https://master.php.net/note/delete/93690/integrated Del: useless -- https://master.php.net/note/delete/93690/useless Del: bad code -- https://master.php.net/note/delete/93690/bad+code Del: spam -- https://master.php.net/note/delete/93690/spam Del: non-english -- https://master.php.net/note/delete/93690/non-english Del: in docs -- https://master.php.net/note/delete/93690/in+docs Del: other reasons-- https://master.php.net/note/delete/93690 Reject -- https://master.php.net/note/reject/93690 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#160963) next »