imagecolorallocate pakt 2 kleuren
Hallo ik heb gisteren een script gemaakt om een thumbs te creéren.
Alleen toen ik de achtergrond kleur naar grijs wou veranderen of wit, bleek dat de rechterhelft alsnog zwart blijft.
Heeft iemand een idee hoe dat komt??
Alleen toen ik de achtergrond kleur naar grijs wou veranderen of wit, bleek dat de rechterhelft alsnog zwart blijft.
Heeft iemand een idee hoe dat komt??
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
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
<?php
function place_on($file, $fileName, $width, $height ) {
move_uploaded_file($file['tmp_name'], 'tmp.jpg');
$image = imageCreateFromJpeg('tmp.jpg');
$image_width = imagesx( $image );
$image_height = imagesy( $image );
if($image_width > $image_height) {
$widthScale = $image_width / $width;
$newWidth = $width;
$newHeight = floor($image_height / $widthScale);
}elseif($image_height > $image_width){
$heightScale = $image_height / $height;
$newHeight = $height;
$newWidth = floor($image_width / $heightScale);
}
$thumb = imagecreatetruecolor($width, $height);
imagecopyresized ( $thumb, $image, 0, 0, 0, 0, $newWidth, $newHeight, $image_width, $image_height);
//imagefilledrectangle()
$newimage = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($newimage, 255,255,255);
imagefill($newimage, 0, 0, $white);
// Startposities opzoeken
$x = ( $width - $newWidth ) / 2;
$y = ( $height - $newHeight ) / 2;
if ( $x < 0 ) { $x = 0; }
if ( $y < 0 ) { $y = 0; }
// Afbeeldingen samenvoegen
imagecopy( $newimage, $thumb, $x, $y, 0, 0, $image_width, $image_height);
// De nieuwe afbeelding opslaan
imagejpeg($newimage, $fileName);
@imagedestroy('tmp.jpg');
echo '<img style="float:left; border: 1px solid #CCC; padding: 10px;" src="'.$fileName.'" />';
echo '<fieldset style="width:200; float:left; margin: 0 20;">
<legend>Variables</legend>
<b>Old img width: </b>'.$image_width.'<br />
<b>Old img height: </b>'.$image_height.'<br />
<br />
<b>New img height: </b>'.$newWidth.'<br />
<b>New img height: </b>'.$newHeight.'<br />
</fieldset>';
}
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$file = $_FILES['img'];
$fileName = $_FILES['img']['name'];
place_on($file,$fileName, 200, 200);
}
?>
<form method="POST" action="" enctype="multipart/form-data">
<input type="file" name="img" /><br />
<input type="submit" value="Verzend" />
</form>
function place_on($file, $fileName, $width, $height ) {
move_uploaded_file($file['tmp_name'], 'tmp.jpg');
$image = imageCreateFromJpeg('tmp.jpg');
$image_width = imagesx( $image );
$image_height = imagesy( $image );
if($image_width > $image_height) {
$widthScale = $image_width / $width;
$newWidth = $width;
$newHeight = floor($image_height / $widthScale);
}elseif($image_height > $image_width){
$heightScale = $image_height / $height;
$newHeight = $height;
$newWidth = floor($image_width / $heightScale);
}
$thumb = imagecreatetruecolor($width, $height);
imagecopyresized ( $thumb, $image, 0, 0, 0, 0, $newWidth, $newHeight, $image_width, $image_height);
//imagefilledrectangle()
$newimage = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($newimage, 255,255,255);
imagefill($newimage, 0, 0, $white);
// Startposities opzoeken
$x = ( $width - $newWidth ) / 2;
$y = ( $height - $newHeight ) / 2;
if ( $x < 0 ) { $x = 0; }
if ( $y < 0 ) { $y = 0; }
// Afbeeldingen samenvoegen
imagecopy( $newimage, $thumb, $x, $y, 0, 0, $image_width, $image_height);
// De nieuwe afbeelding opslaan
imagejpeg($newimage, $fileName);
@imagedestroy('tmp.jpg');
echo '<img style="float:left; border: 1px solid #CCC; padding: 10px;" src="'.$fileName.'" />';
echo '<fieldset style="width:200; float:left; margin: 0 20;">
<legend>Variables</legend>
<b>Old img width: </b>'.$image_width.'<br />
<b>Old img height: </b>'.$image_height.'<br />
<br />
<b>New img height: </b>'.$newWidth.'<br />
<b>New img height: </b>'.$newHeight.'<br />
</fieldset>';
}
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$file = $_FILES['img'];
$fileName = $_FILES['img']['name'];
place_on($file,$fileName, 200, 200);
}
?>
<form method="POST" action="" enctype="multipart/form-data">
<input type="file" name="img" /><br />
<input type="submit" value="Verzend" />
</form>
Zwart is voor zover ik weet de standaard kleur in GD. Dus als een deel wit wordt een een deel zwart, komt het omdat het witte element niet de volledige grootte van de afbeelding heeft.
Gaat er niet iets mis met de variabelen die je her en der gebruikt? ($width, $image_width, $newWidth)
Gaat er niet iets mis met de variabelen die je her en der gebruikt? ($width, $image_width, $newWidth)
Elwin Fratsloos op 29/09/2011 15:01:55:
Zwart is voor zover ik weet de standaard kleur in GD.
Sterker nog: in grafische termen is zwart geen kleur.
Maar ik denk dat Elwin wel gelijk heeft: je gaat waarschijnlijk met die variabelen ergens de fout in.
Toevoeging op 29/09/2011 15:51:45:
en wellicht een adviesje:
als je aan het testen bent met een script, test het stap voor stap
Maar in dit gedeelte hieronder geef ik width en height mee en beide zijn altijd 200 px.
Toevoeging op 29/09/2011 21:12:16:
Als een plaatje landscape is vult hij het onderste deel aan met zwart.
Als een plaatje portret is vult hij het rechter deel aan met zwart.
Het vierkantje zelf is gewoon wit. Dus ik snap er echt niks van :(
Hierbij de volledige code
Toevoeging op 29/09/2011 21:21:07:
Ik weet waar het probleem zit.
$thumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
Maakt een zwarte image aan. Ik maak deze aan zodat ik hier de thumb in kan gooien.
Maar deze moet dan dus ook wit worden. Of heeft iemand een beter idee?
Code (php)
1
2
3
4
2
3
4
//imagefilledrectangle()
$newimage = imagecreatetruecolor($, $height);
$white = imagecolorallocate($newimage, 255,255,255);
imagefill($newimage, 0, 0, $white);
$newimage = imagecreatetruecolor($, $height);
$white = imagecolorallocate($newimage, 255,255,255);
imagefill($newimage, 0, 0, $white);
Toevoeging op 29/09/2011 21:12:16:
Als een plaatje landscape is vult hij het onderste deel aan met zwart.
Als een plaatje portret is vult hij het rechter deel aan met zwart.
Het vierkantje zelf is gewoon wit. Dus ik snap er echt niks van :(
Hierbij de volledige 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
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
<?php
function place_on($file, $fileName, $thumbWidth, $thumbHeight ) {
move_uploaded_file($file['tmp_name'], 'tmp.jpg');
$image = imageCreateFromJpeg('tmp.jpg');
unlink('tmp.jpg');
$imageWidth = imagesx( $image );
$imageHeight = imagesy( $image );
if($imageWidth > $imageHeight) {
$widthScale = $imageWidth / $thumbWidth;
$newWidth = $thumbWidth;
$newHeight = floor($imageHeight / $widthScale);
}elseif($imageHeight > $imageWidth){
$heightScale = $imageHeight / $thumbHeight;
$newHeight = $thumbHeight;
$newWidth = floor($imageWidth / $heightScale);
}
$thumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
imagecopyresized ( $thumb, $image, 0, 0, 0, 0, $newWidth, $newHeight, $imageWidth, $imageHeight);
//imagefilledrectangle()
$newimage = imagecreatetruecolor($thumbWidth, $thumbHeight);
$white = imagecolorallocate($newimage, 255,255,255);
imagefill($newimage, 0, 0, $white);
// Startposities opzoeken
$x = ( $thumbWidth - $newWidth ) / 2;
$y = ( $thumbHeight - $newHeight ) / 2;
if ( $x < 0 ) { $x = 0; }
if ( $y < 0 ) { $y = 0; }
// Afbeeldingen samenvoegen
imagecopy( $newimage, $thumb, $x, $y, 0, 0, $imageWidth, $imageHeight);
// De nieuwe afbeelding opslaan
imagejpeg($newimage, $fileName, 100);
echo '<img style="float:left; border: 1px solid #CCC; padding: 10px;" src="'.$fileName.'" />';
// echo '<fieldset style="width:200; float:left; margin: 0 20;">
// <legend>Variables</legend>
// <b>Old img width: </b>'.$imageWidth.'<br />
// <b>Old img height: </b>'.$imageHeight.'<br />
// <br />
// <b>X: </b>'.$x.'<br />
// <b>Y: </b>'.$y.'<br />
// <br />
// <b>New img height: </b>'.$newWidth.'<br />
// <b>New img height: </b>'.$newHeight.'<br />
// </fieldset>';
}
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$file = $_FILES['img'];
$fileName = $_FILES['img']['name'];
place_on($file, $fileName, 200, 200);
}
?>
<form method="POST" action="" enctype="multipart/form-data">
<input type="file" name="img" /><br />
<input type="submit" value="Verzend" />
</form>
function place_on($file, $fileName, $thumbWidth, $thumbHeight ) {
move_uploaded_file($file['tmp_name'], 'tmp.jpg');
$image = imageCreateFromJpeg('tmp.jpg');
unlink('tmp.jpg');
$imageWidth = imagesx( $image );
$imageHeight = imagesy( $image );
if($imageWidth > $imageHeight) {
$widthScale = $imageWidth / $thumbWidth;
$newWidth = $thumbWidth;
$newHeight = floor($imageHeight / $widthScale);
}elseif($imageHeight > $imageWidth){
$heightScale = $imageHeight / $thumbHeight;
$newHeight = $thumbHeight;
$newWidth = floor($imageWidth / $heightScale);
}
$thumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
imagecopyresized ( $thumb, $image, 0, 0, 0, 0, $newWidth, $newHeight, $imageWidth, $imageHeight);
//imagefilledrectangle()
$newimage = imagecreatetruecolor($thumbWidth, $thumbHeight);
$white = imagecolorallocate($newimage, 255,255,255);
imagefill($newimage, 0, 0, $white);
// Startposities opzoeken
$x = ( $thumbWidth - $newWidth ) / 2;
$y = ( $thumbHeight - $newHeight ) / 2;
if ( $x < 0 ) { $x = 0; }
if ( $y < 0 ) { $y = 0; }
// Afbeeldingen samenvoegen
imagecopy( $newimage, $thumb, $x, $y, 0, 0, $imageWidth, $imageHeight);
// De nieuwe afbeelding opslaan
imagejpeg($newimage, $fileName, 100);
echo '<img style="float:left; border: 1px solid #CCC; padding: 10px;" src="'.$fileName.'" />';
// echo '<fieldset style="width:200; float:left; margin: 0 20;">
// <legend>Variables</legend>
// <b>Old img width: </b>'.$imageWidth.'<br />
// <b>Old img height: </b>'.$imageHeight.'<br />
// <br />
// <b>X: </b>'.$x.'<br />
// <b>Y: </b>'.$y.'<br />
// <br />
// <b>New img height: </b>'.$newWidth.'<br />
// <b>New img height: </b>'.$newHeight.'<br />
// </fieldset>';
}
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$file = $_FILES['img'];
$fileName = $_FILES['img']['name'];
place_on($file, $fileName, 200, 200);
}
?>
<form method="POST" action="" enctype="multipart/form-data">
<input type="file" name="img" /><br />
<input type="submit" value="Verzend" />
</form>
Toevoeging op 29/09/2011 21:21:07:
Ik weet waar het probleem zit.
$thumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
Maakt een zwarte image aan. Ik maak deze aan zodat ik hier de thumb in kan gooien.
Maar deze moet dan dus ook wit worden. Of heeft iemand een beter idee?
Gewijzigd op 29/09/2011 21:14:49 door Jos Verra




