Using brushes

int imagesetbrush ( resource image, resource brush)

In the same way that imagesettile() allows you to use a picture for filling, imagesetbrush() allows you to do use a picture for the outline. While this could be a pre-made picture you've just loaded, you can get very nice effects by using hand-made pictures that are swept around basic shapes.

Take a look at the picture below:


As you can see, it's just a lot of dots ranging in colour from red to yellow - not very interesting, but great for using as a brush!

Those dots were created with this script:

<?php
  $brush
= imagecreate(100,100);

  
$brushtrans = imagecolorallocate($brush, 0, 0, 0);
  
imagecolortransparent($brush, $brushtrans);

  for (
$k = 1; $k < 18; ++$k) {
    
$color = imagecolorallocate($brush, 255, $k * 15, 0);
    
imagefilledellipse($brush, $k * 5, $k * 5, 5, 5, $color);
  }

  
imagepng($brush);
  
imagedestroy($brush);
?>

The next step is to create a larger image, recreate that brush, and use it as the outline for a shape. Here's the code:

<?php

  $pic
= imagecreatetruecolor(600,600);
  
$brush = imagecreate(100,100);

  
$brushtrans = imagecolorallocate($brush, 0, 0, 0);
  
imagecolortransparent($brush, $brushtrans);

  for (
$k = 1; $k < 18; ++$k) {
    
$color = imagecolorallocate($brush, 255, $k * 15, 0);
    
imagefilledellipse($brush, $k * 5, $k * 5, 5, 5, $color);
  }

  
imagesetbrush($pic, $brush);
  
imageellipse($pic, 300, 300, 350, 350, IMG_COLOR_BRUSHED);

  
imagepng($pic);
  
imagedestroy($pic);
  
imagedestroy($brush);
?>

The new line in there is the call to imagesetbrush() - note that it takes the image you're changing as the first parameter, and the brush to use as the second. To actually use the brush that has been set, we need to pass the special constant IMG_COLOR_BRUSHED as the colour parameter for our shape.

That's pretty much it. The only other thing is the call to imagecolortransparent(), which is there so that the black parts of the brush (most of it!) doesn't overlay itself.

The result of that script is shown below - not bad for such a simple script, particularly as only one ellipse is actually drawn in the code.


Once you've used your brush you can go ahead and change it for something else, and do so as many times as you want. This next picture shows our ellipses drawn several times in different colours by recreating the brush as necessary:

<?php
  $pic
= imagecreatetruecolor(400,400);

  
$bluecol = 0;

  for (
$i = -10; $i < 410; $i += 80) {
    for (
$j = -10; $j < 410; $j += 80) {
      
$brush = imagecreate(100,100);

      
$brushtrans = imagecolorallocate($brush, 0, 0, 0);
      
imagecolortransparent($brush, $brushtrans);

      for (
$k = 1; $k < 18; ++$k) {
        
$color = imagecolorallocate($brush, 255, $k * 15, $bluecol);
        
imagefilledellipse($brush, $k * 2, $k * 2, 1, 1, $color);
      }

      
imagesetbrush($pic, $brush);
      
imageellipse($pic, $i, $j, 50, 50, IMG_COLOR_BRUSHED);

      
imagedestroy($brush);
    }

    
$bluecol += 40;
  }

  
imagepng($pic);
  
imagedestroy($pic);
?>

If you're really into complex graphic design, you can even put your finished picture onto an even larger picture, but please do ensure you have a fast computer!

 

Next chapter: Basic image copying >>

Previous chapter: Adding transparency

Jump to:

 

Home: Table of Contents

Follow us on Identi.ca or Twitter

Username:   Password:
Create Account | About TuxRadar