php
Imal Perera  

Work with Images Like a Boss ( PHP )

Spread the love

Working with image using Php GD library is a nightmare unless you have some kind of experience, It waste lot of our time just to do basic resizing of an image, crop polygon shape from an image or write text to an image etc. 

To rescue all of you and save your valuable time Treinetic (pvt) ltd has released an awesome small library called ImageArtist which makes handling of images insanely easy for even a beginner developer

Lets see it in action, our target is to fully code this image and see how many code lines and how hard it is 


Here goes the code



/*
Our first task is to crate two triangles and merge them
*/
 
$tr1 = new Triangle("./city.jpg");
$tr1->scale(60);
$tr1->setPointA(0,0,true);
$tr1->setPointB(100,0,true);
$tr1->setPointC(100,100,true);
$tr1->build();
 
$tr2 = new Triangle("./morning.jpeg");
$tr2->scale(60);
$tr2->setPointA(0,0,true);
$tr2->setPointB(0,100,true);
$tr2->setPointC(100,100,true);
$tr2->build();
 
$tr1->resize($tr1->getWidth(),$tr2->getHeight());
 
//we have merged the images to create the background
$img = $tr1->merge($tr2,0,0);
$img->scale(70);
 
/* Let's add an overlay to this */
$overlay = new Overlay($img->getWidth(),$img->getHeight(),new Color(0,0,0,80));
$img->merge($overlay,0,0);
/* hmmm.. lets add a photo */
$circle = new CircularShape("./person.jpg");
$circle->build();
$img = $img->merge($circle,($img->getWidth()-$circle->getWidth())/2,($img->getHeight() - $circle->getHeight())/2);
/* I think I should add some Text */
$textBox = new TextBox(310,40);
$textBox->setColor(Color::getColor(Color::$WHITE));
$textBox->setFont(Font::getFont(Font::$NOTOSERIF_REGULAR));
$textBox->setSize(20);
$textBox->setMargin(2);
$textBox->setText("We Are Team Treinetic");
$img->setTextBox($textBox,($img->getWidth()-$textBox->getWidth())/2,$img->getHeight()* (5/7));
 
$img->dump(); //development purposes only

See how easy it is to create any image Art you want, now for your imagination is the only barrier

Leave A Comment