PHP Tutorial: Creating A MemeGenerator / Cheezburger Clone - Part 4
|May 14, 2012||Posted by Greg Bulmash under PHP|
Part IV: Signing And Saving Your Masterpiece
In part 3 of this tutorial, I showed you how to make your text fit and wrap it if necessary. Now all we need to do is add your site signature and save the file to disk.
Why save the file to disk when the user can easily just right click on it and save it? Well, if you're going to offer a gallery of images or any other service where people can view it again, delivering a static file from disk uses a lot less server than generating the file dynamically every time.
Saving is this simple...
$img->setImageFormat('jpeg'); $img->setImageCompressionQuality(70); $img->writeImage('path/to/your/photo.jpg');
Set the format.
For a JPEG, you should set the compression ratio (I've found 70 to be a good mix of size savings and good image quality).
And write the file to whatever new place and name you're giving it.
But you see the lovely label at the bottom? How did we add that black stripe and that text?
Well, the text you should know how to do by now. The black stripe, however comes from two more lines...
With the first we set the stripe color to the same color we used for the stroke... black. If you're letting users choose colors, you might want to hard code it to your preferred stripe color.
Earlier on, I used
$imgSize = $img->getImageGeometry(); to get the size of the photo. Now I extend it with
extentImage(), using the same width, the height plus 20, and I set top left of the original image at 0,0 so the image is at the top.
Then I can use the drawing functions for text that we've been exploring to add my label with a thinner font in a smaller size.
And that's it. You now have the basics of how to create a clone of MemeGenerator or the Cheezburger sites.