java 剪裁六边形_在网页中将图像裁剪为六边形

因为我需要使用多个大小和缓存我需要它在PHP(好吧,服务器端:)):

// doge.jpg is a squared pic

$raw = imagecreatefromjpeg('doge.jpg');

/* Simple math here

A_____F

/ \

B/ \E

\ /

C\_____/D

*/

$w = imagesx($raw);

$points = array(

.25 * $w, .067 * $w, // A

0, .5 * $w, // B

.25 * $w, .933 * $w, // C

.75 * $w, .933 * $w, // D

$w, .5 * $w, // E

.75 * $w, .067 * $w // F

);

// Create the mask

$mask = imagecreatetruecolor($w, $w);

imagefilledpolygon($mask, $points, 6, imagecolorallocate($mask, 255, 0, 0));

// Create the new image with a transparent bg

$image = imagecreatetruecolor($w, $w);

$transparent = imagecolorallocatealpha($image, 0, 0, 0, 127);

imagealphablending($image, false);

imagesavealpha($image, true);

imagefill($image, 0, 0, $transparent);

// Iterate over the mask's pixels, only copy them when its red.

// Note that you could have semi-transparent colors by simply using the mask's

// red channel as the original color's alpha.

for($x = 0; $x < $w; $x++) {

for ($y=0; $y < $w; $y++) {

$m = imagecolorsforindex($mask, imagecolorat($mask, $x, $y));

if($m['red']) {

$color = imagecolorsforindex($raw, imagecolorat($raw, $x, $y));

imagesetpixel($image, $x, $y, imagecolorallocatealpha($image,

$color['red'], $color['green'],

$color['blue'], $color['alpha']));

}

}

}

// Display the result

header('Content-type: image/png');

imagepng($image);

imagedestroy($image);你应该得到类似的东西:

d846aa37ddb28978a546453add441f2d.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值