5 more advanced GD examples to manipulate images with PHP
Onelinerhub
Posted on October 13, 2022
1. How to draw pie chart
<?php
$data = [50, 90, 150, 360];
$im = imagecreatetruecolor(400, 300);
$c_black = imageColorAllocate($im, 0,0,0);
foreach ( $data as $i => $v ) {
$c = imageColorAllocate(
$im, 46 + $i * 30,204 - $i * 30, 64 + $i * 30
);
imagefilledarc(
$im, 200, 150,
100 + $i * 10, 100 + $i * 10, $i ? $data[$i-1] : 0,
$v, $c, IMG_ARC_PIE
);
}
imagePng($im, '/tmp/image.png');
-
$data
- sample data to draw pie chart based on, -
imagecreatetruecolor
- creates true color GD image object with specified width & height, -
imageColorAllocate
- creates color object to use for pie (we use $i value to randomize color), -
imagefilledarc
- draw next pie, -
200, 150
- pie center coordinates, -
imagePng
- saves image in PNG format to the given path.
Open original or edit on Github.
2. How to flip an image
<?php
$file = '/var/www/examples/heroine.png';
$im = imagecreatefrompng($file);
imageflip( $im, IMG_FLIP_VERTICAL );
imagePng($im, '/tmp/image.png');
-
$file
- image to rotate, -
imagecreatefrompng
- creates GD image object from given PNG image, -
imageflip
- flips given image in a specified direction, -
IMG_FLIP_VERTICAL
- flips image vertically, explore other options, -
imagePng
- saves image in PNG format to the given path.
Open original or edit on Github.
3. How to get pixel color
$file = '/var/www/examples/heroine.png';
$im = imagecreatefrompng($file);
$c = imagecolorat($im, 100, 100);
list($r, $g, $b) = [($c >> 16) & 0xFF, ($c >> 8) & 0xFF, $c & 0xFF];
-
$file
- sample image to work with, -
imagecreatefrompng
- creates GD image object from given PNG image, -
imagecolorat
- returns color at a given position as an integer, -
100, 100
- we're getting color on 100 pixels from top and 100 pixels from left, -
$r, $g, $b
- variables will contain decoded values for R, G and B color channels.
Open original or edit on Github.
4. How to rotate an image
<?php
$file = '/var/www/examples/heroine.png';
$im = imagecreatefrompng($file);
$c_black = imageColorAllocate($im, 0,0,0);
$imr = imagerotate( $im, 90, $c_black );
imagePng($imr, '/tmp/image.png');
-
$file
- image to rotate, -
imagecreatefrompng
- creates GD image object from given PNG image, -
imageColorAllocate
- creates color object to later use in image, -
imagerotate
- rotates given image and returns new gd image object, -
90
- rotate image by 90 degrees, -
$c_black
- color to use for background (if necessary), -
imagePng
- saves image in PNG format to the given path.
Open original or edit on Github.
5. How to set line thickness
<?php
$im = imagecreatetruecolor(400, 300);
$c_black = imageColorAllocate($im, 0,0,0);
$c_green = imageColorAllocate($im, 46,204,64);
for ( $thick = 5; $thick > 0; $thick-- ) {
imageline($im, 50, 50 + $thick, 350, 250 + $thick, $c_green);
}
imagePng($im, '/tmp/image.png');
-
imagecreatetruecolor
- creates true color GD image object with specified width & height, -
imageColorAllocate
- creates color object to later use in image, -
imageline
- draws line using given coordinates and color, -
$thick = 5
- resulting line thickness (5 in our case), -
imagePng
- saves image in PNG format to the given path.
💖 💪 🙅 🚩
Onelinerhub
Posted on October 13, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.