png afbeeldingen worden zwart
Ik heb het onderstaande script gesprokkeld wat van een plaatje wat al online staat een thumb maakt. Alles werkt prima, alleen bij de png en de gif maakt hij de thumb zwart. Kan iemand zien wat ik verkeerd doe?
<div class="msg" id="msg">
<p>Thumbnail <b>created</b>.</p>
</div>
Code (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
if (isset($_POST['make_thumb'])){
$in_filename = $_POST['thumb_name'];
$in_filename =str_replace("_thumb","",$in_filename);
$ext = end(explode('.', $in_filename));
copy($dir.$in_filename,$dir.$in_filename = str_replace(".","_thumb.",$in_filename));
if($ext=="jpg" || $ext=="jpeg" )
{
$image = imagecreatefromjpeg($dir.$in_filename);
}
else if($ext=="png")
{
$image = imagecreatefrompng($dir.$in_filename);
}
else
{
$image = imagecreatefromgif($dir.$in_filename);
}
list($width, $height) = getimagesize($dir.$in_filename);
if ($height > $width){
$newwidth=$thumb_width;
$newheight=($newwidth*$height)/$width;
if($newheight < $thumb_height){
$newheight=$thumb_height;
$newwidth=($newheight*$width)/$height;
}
}
if($width == $height){
$newwidth=$thumb_width;
$newheight=$thumb_height;
}
if ($height < $width){
$newheight=$thumb_height;
$newwidth=($newheight*$width)/$height;
if($newwidth < $thumb_width){
$newwidth=$thumb_width;
$newheight=($newwidth*$height)/$width;
}
}
//hier wordt gekeken wat het type van het bestand is, jpg, jpeg, png of gif
$new_image = imagecreatetruecolor($newwidth, $newheight);
//hier worden de afmetingen van de thumb daadwerkelijk aangepast
imagecopyresampled($new_image,$image,0,0,0,0,$newwidth,$newheight,$width,$height);
/* Uncomment in case you want it also outputted
header('Content-Type: image/jpeg');
imagejpeg($new_image);
*/
imagejpeg($new_image, $dir.$in_filename,100);
?>
if (isset($_POST['make_thumb'])){
$in_filename = $_POST['thumb_name'];
$in_filename =str_replace("_thumb","",$in_filename);
$ext = end(explode('.', $in_filename));
copy($dir.$in_filename,$dir.$in_filename = str_replace(".","_thumb.",$in_filename));
if($ext=="jpg" || $ext=="jpeg" )
{
$image = imagecreatefromjpeg($dir.$in_filename);
}
else if($ext=="png")
{
$image = imagecreatefrompng($dir.$in_filename);
}
else
{
$image = imagecreatefromgif($dir.$in_filename);
}
list($width, $height) = getimagesize($dir.$in_filename);
if ($height > $width){
$newwidth=$thumb_width;
$newheight=($newwidth*$height)/$width;
if($newheight < $thumb_height){
$newheight=$thumb_height;
$newwidth=($newheight*$width)/$height;
}
}
if($width == $height){
$newwidth=$thumb_width;
$newheight=$thumb_height;
}
if ($height < $width){
$newheight=$thumb_height;
$newwidth=($newheight*$width)/$height;
if($newwidth < $thumb_width){
$newwidth=$thumb_width;
$newheight=($newwidth*$height)/$width;
}
}
//hier wordt gekeken wat het type van het bestand is, jpg, jpeg, png of gif
$new_image = imagecreatetruecolor($newwidth, $newheight);
//hier worden de afmetingen van de thumb daadwerkelijk aangepast
imagecopyresampled($new_image,$image,0,0,0,0,$newwidth,$newheight,$width,$height);
/* Uncomment in case you want it also outputted
header('Content-Type: image/jpeg');
imagejpeg($new_image);
*/
imagejpeg($new_image, $dir.$in_filename,100);
?>
<p>Thumbnail <b>created</b>.</p>
</div>
Gewijzigd op 17/04/2012 15:23:42 door Ruben Koops
Oplossing: http://www.akemapa.com/2008/07/10/php-gd-resize-transparent-image-png-gif/
Quote:
By default, you will get black background if you resize a transparent image. To fix it, you need set alpha channel imagecolorallocatealpha to 127.
With imagecolorallocatealpha, it will allocate a color for an image.
With imagecolorallocatealpha, it will allocate a color for an image.
Mebus VG
Thanks for the quick reaction, I tried to implement your sollution. Now i get a completely transparant picture, this is my code:
Thanks for the quick reaction, I tried to implement your sollution. Now i get a completely transparant picture, this is my code:
Code (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
if (isset($_POST['make_thumb'])){
$in_filename = $_POST['thumb_name'];
$in_filename =str_replace("_thumb","",$in_filename);
$ext = end(explode('.', $in_filename));
copy($dir.$in_filename,$dir.$in_filename = str_replace(".","_thumb.",$in_filename));
list($width, $height) = getimagesize($dir.$in_filename);
if ($height > $width){
$newwidth=$thumb_width;
$newheight=($newwidth*$height)/$width;
if($newheight < $thumb_height){
$newheight=$thumb_height;
$newwidth=($newheight*$width)/$height;
}
}
if($width == $height){
if($thumb_width > $thumb_height){
$newwidth=$thumb_width;
$newheight=$thumb_width;
}else{
$newwidth=$thumb_height;
$newheight=$thumb_height;
}}
if ($height < $width){
$newheight=$thumb_height;
$newwidth=($newheight*$width)/$height;
if($newwidth < $thumb_width){
$newwidth=$thumb_width;
$newheight=($newwidth*$height)/$width;
}
}
$thumb = imagecreatetruecolor($newwidth, $newheight);
//hier worden de afmetingen van de thumb daadwerkelijk aangepast
imagealphablending($thumb, false);
imagesavealpha($thumb, true);
$transparent = imagecolorallocatealpha($thumb, 255, 255, 255, 127);
imagefilledrectangle($thumb, 0, 0, $newwidth, $newheight, $transparent);
switch($ext){
case 'jpeg':
case 'jpg':
$image = imagecreatefromjpeg($dir.$in_filename);
break;
case 'gif':
$image = imagecreatefromgif($dir.$in_filename);
break;
case 'png':
$image = imagecreatefrompng($dir.$in_filename);
break;
}
imagealphablending($image, true);
imagecopyresampled($thumb, $image,0,0,0,0,$newwidth,$newheight,$width,$height);
/* Uncomment in case you want it also outputted
header('Content-Type: image/jpeg');
imagejpeg($new_image);
*/
switch($ext){
case 'jpeg':
case 'jpg':
imagejpeg($thumb, $dir.$in_filename);
break;
case 'gif':
imagegif($thumb, $dir.$in_filename);
break;
case 'png':
imagepng($thumb, $dir.$in_filename);
break;
}?>
if (isset($_POST['make_thumb'])){
$in_filename = $_POST['thumb_name'];
$in_filename =str_replace("_thumb","",$in_filename);
$ext = end(explode('.', $in_filename));
copy($dir.$in_filename,$dir.$in_filename = str_replace(".","_thumb.",$in_filename));
list($width, $height) = getimagesize($dir.$in_filename);
if ($height > $width){
$newwidth=$thumb_width;
$newheight=($newwidth*$height)/$width;
if($newheight < $thumb_height){
$newheight=$thumb_height;
$newwidth=($newheight*$width)/$height;
}
}
if($width == $height){
if($thumb_width > $thumb_height){
$newwidth=$thumb_width;
$newheight=$thumb_width;
}else{
$newwidth=$thumb_height;
$newheight=$thumb_height;
}}
if ($height < $width){
$newheight=$thumb_height;
$newwidth=($newheight*$width)/$height;
if($newwidth < $thumb_width){
$newwidth=$thumb_width;
$newheight=($newwidth*$height)/$width;
}
}
$thumb = imagecreatetruecolor($newwidth, $newheight);
//hier worden de afmetingen van de thumb daadwerkelijk aangepast
imagealphablending($thumb, false);
imagesavealpha($thumb, true);
$transparent = imagecolorallocatealpha($thumb, 255, 255, 255, 127);
imagefilledrectangle($thumb, 0, 0, $newwidth, $newheight, $transparent);
switch($ext){
case 'jpeg':
case 'jpg':
$image = imagecreatefromjpeg($dir.$in_filename);
break;
case 'gif':
$image = imagecreatefromgif($dir.$in_filename);
break;
case 'png':
$image = imagecreatefrompng($dir.$in_filename);
break;
}
imagealphablending($image, true);
imagecopyresampled($thumb, $image,0,0,0,0,$newwidth,$newheight,$width,$height);
/* Uncomment in case you want it also outputted
header('Content-Type: image/jpeg');
imagejpeg($new_image);
*/
switch($ext){
case 'jpeg':
case 'jpg':
imagejpeg($thumb, $dir.$in_filename);
break;
case 'gif':
imagegif($thumb, $dir.$in_filename);
break;
case 'png':
imagepng($thumb, $dir.$in_filename);
break;
}?>




