SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Apr 2011
    Posts
    265
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    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.
    Free: Web Programming Courses HTML, CSS, Flash
    Web Programming: AJAX Course and PHP-MySQL Course video Lessons
    Good JavaScript and jQuery course for beginners

  2. #2
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,369
    Mentioned
    80 Post(s)
    Tagged
    3 Thread(s)
    I would guess you need to make the background first with two images of the different colours; save that then put on your top image.

  3. #3
    SitePoint Addict
    Join Date
    Apr 2011
    Posts
    265
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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);
    Free: Web Programming Courses HTML, CSS, Flash
    Web Programming: AJAX Course and PHP-MySQL Course video Lessons
    Good JavaScript and jQuery course for beginners


Bookmarks

Posting Permissions

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