最近作到相關的項目,因爲項目使用html2canvas,可是不支持css mask屬性,故,利用php後臺來裁剪。php
準備兩張圖片,一張是鏤空PNG圖案,一張是任意純色圖片。css
便可以在純色圖片上裁剪出鏤空的圖案爲PNG文件。html
見下圖。ajax
首先兩張PNG圖片:編程
生成圖片canvas
JS片斷:小程序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
html2canvas($(
".head1pic"
), {
onrendered:
function
(canvas) {
url = canvas.toDataURL(
"image/png"
, 1.0);
sourcePic =
"assets/images/demo.png"
;
maskPic =
"assets/images/jinmao.png"
;
cropPicName =
"cropDog1"
;
// ajax php截圖
$.ajax({
type:
'post'
,
url:
'getpicture'
,
data: {
"sourcePic"
: sourcePic,
"maskPic"
: maskPic,
"cropPicName"
: cropPicName
},
success:
function
(data) {
$(
".page2Bg"
)[0].setAttribute(
"src"
,
"assets/images/crop/cropDog1.png"
);
},
error:
function
(data) {
console.log(data)
}
});
}
});
|
PHP的片斷:微信小程序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
public
function
actionGetpicture()
{
$request
= Yii::
$app
->request;
$sourcePic
=
$request
->post(
'sourcePic'
);
$maskPic
=
$request
->post(
'maskPic'
);
$cropPicName
=
$request
->post(
'cropPicName'
);
// $sourcePic="http://bings.local.com/bi_ngs2_2/assets/images/yinpian1/page2Bg4.png";
// $maskPic="http://bings.local.com/bi_ngs2_2/assets/images/jinmao.png";
$source
= imagecreatefrompng(
$sourcePic
);
$mask
= imagecreatefrompng(
$maskPic
);
// Apply mask to source
// imagealphamask( $source, $mask );
$this
->imagealphamask (
$source
,
$mask
);
// Output
header(
"Content-type: image/png"
);
// 生成截取後的圖片並保存在本地
imagepng(
$source
,
"assets/images/crop/"
.
$cropPicName
.
".png"
);
//銷燬圖片內存
imagedestroy(
$source
);
}
public
function
imagealphamask( &
$picture
,
$mask
) {
// Get sizes and set up new picture
$xSize
= imagesx(
$picture
);
$ySize
= imagesy(
$picture
);
$newPicture
= imagecreatetruecolor(
$xSize
,
$ySize
);
imagesavealpha(
$newPicture
, true );
imagefill(
$newPicture
, 0, 0, imagecolorallocatealpha(
$newPicture
, 100, 100, 0, 127 ) );
// Resize mask if necessary
// if( $xSize != imagesx( $mask ) || $ySize != imagesy( $mask ) ) {
// $tempPic = imagecreatetruecolor( $xSize, $ySize );
// imagecopyresampled( $tempPic, $mask, 0, 0, 0, 0, $xSize, $ySize, imagesx( $mask ), imagesy( $mask ) );
// imagedestroy( $mask );
// $mask = $tempPic;
// }
// Perform pixel-based alpha map application
for
(
$x
= 0;
$x
<
$xSize
;
$x
++ ) {
for
(
$y
= 0;
$y
<
$ySize
;
$y
++ ) {
$alpha
= imagecolorsforindex(
$mask
, imagecolorat(
$mask
,
$x
,
$y
) );
//small mod to extract alpha, if using a black(transparent) and white
//mask file instead change the following line back to Jules's original:
// $alpha = 127 - floor($alpha['black'] / 2);
//or a white(transparent) and black mask file:
// $alpha = floor($alpha['black'] / 2);
$alpha
=
$alpha
[
'alpha'
];
$color
= imagecolorsforindex(
$picture
, imagecolorat(
$picture
,
$x
,
$y
) );
//preserve alpha by comparing the two values
if
(
$color
[
'alpha'
] >
$alpha
)
$alpha
=
$color
[
'alpha'
];
//kill data for fully transparent pixels
if
(
$alpha
== 127) {
$color
[
'red'
] = 0;
$color
[
'blue'
] = 0;
$color
[
'green'
] = 0;
}
imagesetpixel(
$newPicture
,
$x
,
$y
, imagecolorallocatealpha(
$newPicture
,
$color
[
'red'
],
$color
[
'green'
],
$color
[
'blue'
],
$alpha
) );
}
}
// Copy back to original picture
imagedestroy(
$picture
);
$picture
=
$newPicture
;
}
|