Hudzilla.org - the homepage of Paul Hudson
Contents > Multimedia > Images Wish List | Report Bug | About Me ]

11.2.10     Using brushes: imagesetbrush()

This is NOT the latest copy of this book; click here for the latest version.

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!







<< 11.2.9 Adding transparency: imagecolortransparent()   11.2.11 Basic image copying: imagecopy() and imagecopymerge() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is zero plus two?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow