Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Mar 2011
    Posts
    148
    Thanks
    0
    Thanked 20 Times in 20 Posts

    Image in PHP with background of two colors

    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.

  • #2
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    723
    Thanks
    20
    Thanked 84 Times in 84 Posts
    you could create 2 objects instead being 2 colors
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #3
    Regular Coder
    Join Date
    Mar 2011
    Posts
    148
    Thanks
    0
    Thanked 20 Times in 20 Posts
    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:
    Code:
    <?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);


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •