Creating Image Patterns Using GD Library with PHP
- Digital Engineering
Creating Image Patterns Using GD Library with PHP
There are several ways to do this, but I find GD Library of PHP pretty useful for this.
To install this library in Ubuntu use following command
1 |
sudo apt-get install php5-gd |
After installation restart your apache/lampp server.
Image Patterns can be created using image merging concept. First create a blank image and start merging your pattern on it at some particular position.
imagecopymerge() can be used for merging concepts.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
bool imagecopymerge ($dst_im ,$src_im ,$dst_x ,$dst_y ,$src_x ,$src_y ,$src_w , $src_h ,$pct ) dst_im Destination image link resource. src_im Source image link resource. dst_x x-coordinate of destination point. dst_y y-coordinate of destination point. src_x x-coordinate of source point. src_y y-coordinate of source point. src_w Source width. src_h Source height. pct The two images will be merged according to pct which can range from 0 to 100. When pct = 0, no action is taken, when 100 this function behaves identically to imagecopy() for pallete images, except for ignoring alpha components, while it implements alpha transparency for true colour images. |
A small sample code with this concept is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?php // Assume height width of blank image $imageWidth=1000; $imageHeight=1000; // location of pattern image $src="images/R.jpg"; // create a new image 1000x1000 $im = @imagecreatetruecolor($imageWidth,$imageHeight) or die('Cannot Initialize new GD image stream'); // getting height width of pattern image list($patternWidth, $patternHeight, $type, $attr) = getimagesize($src); // insert pattern image in blank image at 150,150 position $dest=imagecreatefromjpeg($src); imagecopymerge($im, $dest, 150, 150, 0, 0, $patternWidth, $patternHeight, 100); // insert one more pattern image in blank image at 500,600 position $dest=imagecreatefromjpeg($src); imagecopymerge($im, $dest, 500, 600, 0, 0, $patternWidth, $patternHeight, 100); // save image $loc="downloadedPattern.png"; imagepng($im,$loc); imagedestroy($im); ?> . |
Output of this code is as follows:
For more such examples, download attached zip
Related content
Auriga: Leveling Up for Enterprise Growth!
Auriga’s journey began in 2010 crafting products for India’s