PHP code example of easygithdev / easygd

1. Go to this page and download the library: Download easygithdev/easygd library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

easygithdev / easygd example snippets




asygd\Image;


$stream = 'https://www.php.net/images/logos/new-php-logo.png';

(new Image())->load($stream)->show();

<img src=" echo (new Image())->load($stream)->src() 

(new Image())->load($stream)->save('php.png');

(new Image())->load($stream)->save('php.png')->show();

(new Image())->load($stream)->save('php.png')
->setType(IMAGETYPE_GIF)->save('php.gif')
->setType(IMAGETYPE_JPEG)->save('php.jpg');

$dimension = (new Dimension())->create(300, 300);

// Create a color with hexadecimal code
$color = (new Color())->create('#83d01e');

// OR create a color from a preset
$color = (new Color())->create(Color::Yellow);

// OR create a preseted color 
$color = Color::Yellow();

$position = (new Position())->create(200, 125);

$text = (new Text())->create('Hello World');

(new Image())->create($dimension, $color)->show();

(new Image)->create((new Dimension)->create(300, 300), Color::Blue())
->addText(
(new Text())->create('Hello World')
    ->setColor(Color::Silver())
    ->setSize(3)
    ->setPosition((new Position)->create(200, 125))
)->show();

(new Image())->create((new Dimension())->create(200, 200))->addText(
	(new Text())->create('gd library')
	->setSize(5)
	->setColor(Color::White())
	->setDrawtype(Text::TEXT_DRAW_VERTICAL)
	->setPosition((new Position())->create(40, 100))
)->show();
    
(new Image)->create((new Dimension())->create(300, 300), Color::White())->addText(
	(new Text())->create('Alpha Color')
	->setColor(
		(new Color())->create('#FF0000')->setAlpha(95)
	)
	->setSize(5)
	->setPosition((new Position())->create(55, 35))
)
->show();

(new Image())->create((new Dimension())->create(300, 300), (new Color())->create('#f2f2f2'))
->addText(
	(new Text())->create('True Type')
		->setFontType(Text::TEXT_FONT_TRUETYPE)
		->setFontfile(Text::TEXT_UNIX_FONT_PATH . '/truetype/dejavu/DejaVuSans.ttf')
		->setColor(Color::Silver())
		->setSize(16)
		->setAngle(45)
		->setPosition((new Position())->create(100, 100))
)
->addText(
	(new Text())->create('Hello ' . PHP_EOL . 'Free Type')
		->setFontType(Text::TEXT_FONT_FREETYPE)
		->setFontfile(Text::TEXT_UNIX_FONT_PATH . '/truetype/dejavu/DejaVuSans.ttf')
		->setColor(Color::Silver())
		->setSize(16)
		->setAngle(45)
		->setPosition((new Position())->create(100, 175))
)
->addText(
	(new Text())->create('hello world')
		->setColor(Color::Maroon())
		->setPosition((new Position())->create(98, 173))
)
->show();

$infos = (new Image())->load($stream)->getInfos();
echo '<pre>', $infos, '</pre>';

$infos = (new Image())->load($stream)->getInfos()->toArray();
echo '<pre>', print_r($infos, true), '</pre>'; 

$image = (new Image())->load($stream);

echo 'TOP_LEFT:', $image->topLeft(), '<br/>';
echo 'TOP_CENTER:', $image->topCenter(), '<br/>';
echo 'TOP_RIGHT:', $image->topRight(), '<br/>';
echo 'MIDDLE_LEFT', $image->middleLeft(), '<br/>';
echo 'MIDDLE_CENTER', $image->middleCenter(), '<br/>';
echo 'MIDDLE_RIGHT', $image->middleRight(), '<br/>';
echo 'BOTTOM_LEFT', $image->bottomLeft(), '<br/>';
echo 'BOTTOM_CENTER', $image->bottomeCenter(), '<br/>';
echo 'BOTTOM_RIGHT', $image->bottomRight(), '<br/>';

(new Image())->load($stream)->setType(IMG_JPEG)->save('iptc.jpg');

$fileSrc = __DIR__ . '/iptc.jpg';
$fileDst = __DIR__ . '/iptc2.jpg';

$iptc = (new Iptc())->create($fileSrc, null);
$iptc->addTag(Iptc::IPTC_CITY, 'CHEVERNY')
    ->addTag(Iptc::IPTC_COUNTRY, 'FRANCE')
    ->addTag(Iptc::IPTC_CREATED_DATE, '2012-03-01')
    ->addTag(Iptc::IPTC_CATEGORY, 'JOURNEY');

if ($iptc->write($fileDst) === false) {
    throw new Exception('Error to write IPTC');
}
echo '<pre>', (new Image())->load($fileDst)->getInfos(), '</pre>';

(new Image)->load($stream)
->resizeByPercent(0.5)
->show();

(new Image)->load($stream)
->resizeByWidth(50)
->show();

(new Image)->load($stream)
->resizeByHeight(30)
->show();

(new Image)->load($stream)
->resizeAuto((new Dimension())->create(300, 200))
->show();

(new Image)->load($stream)
->thumbnail(140, Color::Silver())
->show();

(new Image)->load($stream)
->crop((new Position)->create(20, 13), (new Dimension())->create(80, 40))
->show();

(new Image)->load($stream)
->rotate(90)
->show();

$logo = (new Image)->load('http://www.php.net/images/logos/php-med-trans-light.gif')
->resizeByPercent(0.6);

(new Image)->load('http://static.php.net/www.php.net/images/logos/php-med-trans.png')
->inlay($logo, (new Position)->create(30, 20), 100)
->show();

$text = (new Text)->create('Copyright (C) 2020 Your Name')
	->setFontType(Text::TEXT_FONT_TRUETYPE)
	->setFontfile(Text::TEXT_UNIX_FONT_PATH . '/truetype/dejavu/DejaVuSans.ttf')
	->setSize(5)
	->setColor(Color::White())
	->setPosition((new Position())->create(40, 190));

$path = __DIR__ . '/images/original/';

foreach (new ImageFilterIterator(new DirectoryIterator($path)) as $file) {

	if (($image = (new Image)->load($file->getPathname())) === false) {
		continue;
	}

	$thumb1 = $image->thumbnail(100)->src();
	$thumb2 = $image->thumbnail(200)->addText($text)->src();
	$thumb3 = $image->thumbnail(400)->src();

// Preset filters
$src1 = Filter::negate()->process((new Image())->load($stream))->src();

// Convolution filters
$src2 = Filter::emboss()->process((new Image())->load($stream))->src();

// LookupTable filters
$src3 = Filter::thresholding()->process((new Image())->load($stream))->src();

(new PresetFilter())->create(PresetFunctions::negate())->process((new Image())->load($stream))->show();

(new ConvolutionFilter())->create(ConvolutionFunctions::CONVOLUTION_GAUSSIAN())->process((new Image())->load($stream))->show();

$matrix = [
-1, 7, -1,
0, 0, 0,
1, 7, 1
];

$convolution = (new Convolution())->create($matrix);;
$dataSrc = (new ConvolutionFilter())->create($convolution)->process((new Image())->load($stream))->src();

$closure = \Closure::fromCallable([new LookupTableFunctions(), 'LightnessGray']),
(new LookUpTableFilter())->create((new LookUpTable())->create($closure))->process((new Image())->load($stream))->src();

function personnal($rgb)
{
$r = ($rgb['red'] > 128) ? 255 : 128;
$g = ($rgb['green'] > 128) ? 255 : 128;
$b = ($rgb['blue'] > 128) ? 255 : 128;
return [$r, $g, $b];
}

$closure = \Closure::fromCallable('personnal');
$lut = (new LookUpTableFilter())->create((new LookUpTable())->create($closure))->process((new Image())->load($stream))->src();