imagecolorallocate pakt 2 kleuren

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Jos Verra

Jos Verra

29/09/2011 09:16:55
Quote Anchor link
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??

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<?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>
 
PHP hulp

PHP hulp

08/05/2024 06:22:59
 
Elwin - Fratsloos

Elwin - Fratsloos

29/09/2011 15:01:55
Quote Anchor link
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)
 
Ger van Steenderen
Tutorial mod

Ger van Steenderen

29/09/2011 15:40:31
Quote Anchor link
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
 
Jos Verra

Jos Verra

29/09/2011 20:04:16
Quote Anchor link
Maar in dit gedeelte hieronder geef ik width en height mee en beide zijn altijd 200 px.

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
//imagefilledrectangle()
$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)
PHP script in nieuw venster Selecteer het PHP script
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
<?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>


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
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.