回答編集履歴

2

追記

2017/01/07 00:00

投稿

popobot
popobot

スコア6586

test CHANGED
@@ -1,6 +1,4 @@
1
- たぶんトリミングの方でできた余白が黒くなっているんだと思います。そもそも単に画像をコピーすればいいだけなので、ImageCopyを使ってやればうまくいきましたよ。
1
+ たぶんトリミングの方でできた余白が黒くなっているんだと思います。そもそも単に画像をコピーすればいいだけなので、ImageCopyを使ってやればうまくいきましたよ。あと、image_outputの余白部分は、透明で塗りつぶしておく必要があるかと
2
-
3
- ※あと、image_outputの余白部分は、透明で塗りつぶしておく必要があるかと
4
2
 
5
3
 
6
4
 
@@ -30,6 +28,8 @@
30
28
 
31
29
 
32
30
 
31
+ //ブレンドモードを無効にする
32
+
33
33
  imagealphablending($image_trimming, false);
34
34
 
35
35
  imagealphablending($image_output, false);
@@ -41,6 +41,8 @@
41
41
  imagefill($image_output, 0, 0, $transparent); // 塗りつぶす
42
42
 
43
43
 
44
+
45
+ //完全なアルファチャネル情報を保存するフラグをonにする
44
46
 
45
47
  imagesavealpha($image_trimming, true);
46
48
 

1

追記

2017/01/07 00:00

投稿

popobot
popobot

スコア6586

test CHANGED
@@ -1,11 +1,67 @@
1
1
  たぶんトリミングの方でできた余白が黒くなっているんだと思います。そもそも単に画像をコピーすればいいだけなので、ImageCopyを使ってやればうまくいきましたよ。
2
+
3
+ ※あと、image_outputの余白部分は、透明で塗りつぶしておく必要があるかと
2
4
 
3
5
 
4
6
 
5
7
  ```php
6
8
 
9
+ <?php
10
+
11
+ $image_resource = imagecreatefrompng($outPathTrimming);
12
+
13
+ $width_resource = ImageSx($image_resource);
14
+
15
+ $height_resource = ImageSy($image_resource);
16
+
17
+ $width_trimming = ImageSx($image_resource) * $scale/100;
18
+
19
+ $height_trimming = ImageSy($image_resource) * $scale/100;
20
+
21
+ $width_output = 200;
22
+
23
+ $height_output = 200;
24
+
25
+
26
+
27
+ $image_trimming = ImageCreateTrueColor($width_trimming,$height_trimming);
28
+
29
+ $image_output = ImageCreateTrueColor($width_output,$height_output);
30
+
31
+
32
+
33
+ imagealphablending($image_trimming, false);
34
+
35
+ imagealphablending($image_output, false);
36
+
37
+
38
+
39
+ $transparent = imagecolorallocatealpha($image_output, 0, 0, 0, 127); // 透明度を持つ色を作成
40
+
41
+ imagefill($image_output, 0, 0, $transparent); // 塗りつぶす
42
+
43
+
44
+
45
+ imagesavealpha($image_trimming, true);
46
+
47
+ imagesavealpha($image_output, true);
48
+
49
+
50
+
51
+ //縮小イメージ作成
52
+
53
+ ImageCopyResampled($image_trimming, $image_resource,0,0,0,0, $width_trimming,$height_trimming,$width_resource,$height_resource);
54
+
55
+
56
+
7
57
  //トリミングイメージ作成
8
58
 
9
59
  ImageCopy($image_output, $image_trimming,($width_output - $width_trimming)/2,($height_output - $height_trimming)/2, 0,0, $width_trimming,$height_trimming);
10
60
 
61
+
62
+
63
+ //出力
64
+
65
+ imagepng($image_output, $outPath);
66
+
11
67
  ```