1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Image in PHP with background of two colors

Discussion in 'PHP' started by MarPlo, Jul 15, 2013.

  1. #1
    Hi,
    Is it possible to create an image in php with background of two colors? For example top side with a color and the lower half of the image with another background color.

    The imagecolorallocate() sets background with a single color.
     
    MarPlo, Jul 15, 2013 IP
  2. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #2
    Set it as a background image rather then with imagecolorallocate?
     
    HuggyStudios, Jul 15, 2013 IP
  3. MarPlo

    MarPlo Member

    Messages:
    97
    Likes Received:
    2
    Best Answers:
    2
    Trophy Points:
    48
    #3
    The solution i found is to create the image with a fill color, imagefill(), then to draw a rectangle with another color in the lower half of the image; then to add text and other things.
    Here is the code, if someone needs it:
    <?php
    // Create a 160x80 image
    $width = 160;
    $height = 80;
    $im = imagecreatetruecolor($width, $height);
    
    // sets background to red
    $red = imagecolorallocate($im, 255, 0, 0);
    imagefill($im, 0, 0, $red);
    
    // sets and draw a white rectangle
    $white = imagecolorallocate($im, 255, 255, 255);
    imagefilledrectangle($im, 0, $height/2, $width, $width/2, $white);
    
    // sets and adds a text
    $text = 'CoursesWeb.net';
    $text_color = imagecolorallocate($im, 0, 1, 255);
    imagestring($im, 5, 12, $height/3, $text, $text_color);
    
    // Display directly the image
    header('Content-Type: image/png');
    imagepng($im);
    
    Code (markup):
     
    MarPlo, Jul 15, 2013 IP
    ryan_uk likes this.