Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryGenerators and Converters

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

PHP-generated Hatch Diamonds for Fun

Ready for some fun?

PHP can use the div tag to generate horizontal lines. The PHP script accompanying this article can generate diamond shapes of various designs.

Here are a few illustrations.

Some ideas where the diamonds could be used include as article section dividers, decorations on covers or in navigation areas, and as breaks for heavy text blocks.

When a pleasing diamond is generated, a screenshot can be made and the image used in various places, even as a web page or div background image.

How It Works

In the PHP Script, the minimum and maximum widths are specified as percentages. (100% would be the entire available width.)

The PHP script begins at the smallest width and generates a line. Then the width is incremented by the amount you specified and another line is generated.

That keeps going until the width is at the maximum.

Then, the width is decremented and lines generated until the width is at the minimum.

The result is a diamond shape or, depending on your specifications, something akin to it.

The line thickness, spacing between lines, and line color can all be specified.

The PHP Script

Here is the PHP script. It is designed to be put into your web page where you want to generate the hatch diamond. How-to information follows.

<?php
$WidthMinimumPercentage = 10;
$WidthMaximumPercentage = 50;
$WidthPercentageAdjustment = 20;
$HatchLineThickness = 3;
$HatchLineSpacing = 0;
$HatchLineColor = "#2F8CDB";
for( $i=$WidthMinimumPercentage; $i<=($WidthMaximumPercentage-$WidthPercentageAdjustment); $i+=$WidthPercentageAdjustment )
{
    echo <<<HATCHLINE
<div 
  style="height:{$HatchLineThickness}px; 
         width:${i}%; 
         background-color:$HatchLineColor; 
         margin:0 auto {$HatchLineSpacing}px auto;">
</div>
HATCHLINE;
}
for( ; $i>=$WidthMinimumPercentage; $i-=$WidthPercentageAdjustment )
{
    echo <<<HATCHLINE
<div 
  style="height:{$HatchLineThickness}px; 
         width:${i}%; 
         background-color:$HatchLineColor; 
         margin:0 auto {$HatchLineSpacing}px auto;">
</div>
HATCHLINE;
}
?>

How to Use the Script

Paste the script into the web page source code where you want to generate a hatch diamond. Then, customize 6 variables (the blue lines in the above PHP source code.

The first 3 lines to customize have to do with the width of the hatch diamond.

  1. $WidthMinimumPercentage = 10; is the percent of the available width for the shortest of the horizontal hatch lines. Specify only the number, no % character. The shortest line is at the top and the bottom of the hatch diamond. The number 0 may be specified.

  2. $WidthMaximumPercentage = 50; is the percent of the available width for the longest of the horizontal hatch lines. Specify only the number, no % character. The longest line is at the middle of the hatch diamond. The number 100 may be specified. This number determines the width of the hatch diamond.

  3. $WidthPercentageAdjustment = 20; is how much the percentage shall be adjusted during the course of making the diamond. This number determines the slant of the hatch diamond sides.

The last 3 lines have to do with the appearance of the hatch lines.

  1. $HatchLineThickness = 3; specifies the thickness of the hatch lines, in pixels. (Don't specify "px", just the number.) The number needs to be larger than 0.

  2. $HatchLineSpacing = 0; specifies how much space to put between the hatch lines. (Don't specify "px", just the number.) The number may be 0 or larger. If 0, the diamond will be a solid color.

  3. $HatchLineColor = "#2F8CDB"; specifies the color of the hatch lines. Any HTML color value may be specified here.

Adjust the various custom specifications to see what the results are. It's actually quite good fun.

The following section break is generated from the PHP script source code presented further above — no specifications changed.

For completeness, each of the hatch diamond illustrations at the beginning of this article are repeated here, followed by the specifications that created the illustration.

$WidthMinimumPercentage = 0;
$WidthMaximumPercentage = 10;
$WidthPercentageAdjustment = .5;
$HatchLineThickness = 1;
$HatchLineSpacing = 0;
$HatchLineColor = "#2F8CDB";
$WidthMinimumPercentage = 0;
$WidthMaximumPercentage = 50;
$WidthPercentageAdjustment = 5;
$HatchLineThickness = 1;
$HatchLineSpacing = 1;
$HatchLineColor = "#2F8CDB";
$WidthMinimumPercentage = 0;
$WidthMaximumPercentage = 10;
$WidthPercentageAdjustment = 1;
$HatchLineThickness = 4;
$HatchLineSpacing = 2;
$HatchLineColor = "#2F8CDB";
$WidthMinimumPercentage = 0;
$WidthMaximumPercentage = 50;
$WidthPercentageAdjustment = 20;
$HatchLineThickness = 3;
$HatchLineSpacing = 0;
$HatchLineColor = "#2F8CDB";

As you can see, changing the custom specifications can produce a wide variety of hatch diamond shapes.

If you like to make PHP dance and do things according to your direction, you'll have fun with this hatch diamond script.

See how many different pleasing hatch diamonds you can generate.

(This article first appeared with an issue of the Possibilities newsletter.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC