Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

SitePoint PHP Blog:
Simple Captchas with PHP and GD
Mar 07, 2014 @ 13:05:38

On the SitePoint PHP blog there's a new post by Mahul Jain looking at a simple way to create CAPTCHA images in PHP using the built-in GD libraries.

By now, we’ve all encountered captcha images in online forms. Captchas are a necessary evil, and this article will teach you how they’re made. Please note that while there are better, automatic third party solutions for captchas out there such as ReCaptcha, this tutorial aims merely to explain and demonstrate how such technology actually works. We won’t be explaining what captchas actually are, as it’s assumed to be common knowledge and already covered in greater detail elsewhere.

He walks you through the whole process of making the sample CAPTCHA (like this one):

  • Display an empty image on the browser
  • Create a shape
  • Generate random lines
  • Generate random dots
  • Generate random text

There's screenshots all along the way too for reference as to what your result should look like. You can download the full code over on GitHub.

tagged: captcha gd tutorial introduction

Link: http://www.sitepoint.com/simple-captchas-php-gd/

SitePoint PHP Blog:
Imagick vs GD
Nov 28, 2013 @ 19:16:34

In a new post to the SitePoint PHP blog Jacek Barecki has written up a comparison of two of the most widely used PHP image manipulation libraries - Imagick and GD.

If you want to create a thumbnail, apply a filter to an image or transform it in any other way, you will have to employ an image processing library in your PHP application. It means that you will probably choose GD or ImageMagick. But which one supports a wider range of image formats? Maybe one of them is slower than the other? What other criteria should be taken under consideration when choosing the right library?

He compares them on a few different aspects:

  • Availability
  • Supported file types
  • Functionality
  • Performance
  • Coding style
  • Popularity

He also provides three alternatives to using GD or Imagick, most involving outside services or software.

tagged: imagick gd image manipulation library tutorial

Link: http://www.sitepoint.com/imagick-vs-gd/

ASCII Art Generator
Aug 07, 2013 @ 10:52:18

In the "something completely different" category today, anonwhat has created an image-to-ASCII art tutorial that takes in an image and, using PHP's GD functionality and a bit of logic, transforms it.

In this tutorial, we are going to create a simple web-based ASCII Art Generator. The concepts found in this tutorial can be applied to other languages. [It is] a simple ASCII Art Generator takes in an image, grayscales it, pixelates it, then prints out a character for each block on the pixelated image based on how “dark” the block is.

The tutorial walks you through the steps needed to make the simple script. It analyzes the image looking at the "blackness" (a combination of the values from imagecolorat and imagecolorsforindex). ACSII characters are then substituted for different "blackness" ranges with the result looking something like this.

tagged: ascii art image tranform blackness gd tutorial

Link: http://fuzzicode.com/create/ascii-art-generator

Charting with pChart
Dec 05, 2011 @ 09:50:07

On PHPMaster.com today there's a new post highlighting the use of pChart to graph out the data from your PHP application. pChart provides an easy interface to draw graphs with GD, supporting alpha transparency, shadowing, spline/cubic curves and much more.

Created and maintained by Jean-Damien Pogolotti, a systems engineer based in Toulouse, France, pChart is a library that creates anti-aliased charts and graphs using PHP. It's object-oriented code has been recently redesigned and makes it easy to add beautiful, eye-catching data. The library is free for non-profit use and for inclusion in GPL distributed software; licensing plans for other uses start at just 50 Euro. In this article I'll take you through installing pChart and using it to generate a basic chart, line chart and plot graph.

He walks you through the download and installation of the tool as well as showing the code you'll need to include to get started with a first script. Their examples show how to create both single- and three-series charts with some sample data and some screenshots of output.

tagged: pchart tutorial library gd graph


Creating a Smooth Curve Graphs with PHP and GD
Oct 25, 2011 @ 12:27:51

On Script-Tutorials.com there's a new tutorial posted showing you how you can get better curves on your graphs with PHP and GD (the key is in the "CubicSplines" class, providing a few extra calculations to make "splines", a mathematical method for smoothing out values).

Today I have new article for PHP. I will tell you about drawing graphs with GD. Plus – we will smooth our graph with curve lines using cubic splines interpolation method. You can read more about method at Wikipedia.

All of the code is included in the post, ready for cop and pasting - the HTML, CSS plus the PHP to generate the graph, plot the points and apply the cubic splines values. You can check out a live demo of it in action or just download the code and get started on your own.

tagged: smooth curve graph gd tutorial cubic splines


.Net Magazine:
Create an SVG data visualization with PHP
Sep 13, 2011 @ 13:13:25

On the .net magazine site (yes, really) there's a recent post from Brian Suda about creating SVG images with PHP thanks to the GD functionality that's included in every release.

If you’re working online, then canvas springs to mind. It allows you to draw raster graphics quickly and easily. If you want, there are also plenty of image code libraries that can generate GIFs, JPEGs and PNGs on request. But what if your target isn't always online? What if you’re aiming for print? Then you could use a raster graphic, but it would need to be pretty large. A better solution is to create a vector-based image format from your code. This is where SVG (Scalable Vector Graphics) steps in.

He starts by explaining a bit about SVG and what it is for those that haven't worked with it before. He givesn an example of a poster from Wired showing circles with colors from their covers over the last 15 years. He shows how to do something similar with images of the .net magazine covers, pulling out the color frequency with the imagecolorat function. Toss in a bit of markup and some formatting of the color output and you'll get circles with concentric rings with the most prominent colors ending up the largest. You can see his result here.

tagged: svg tutorial gd image color frequency


Watermark processing on images using PHP and GD
Sep 09, 2011 @ 13:36:06

On the Script-Tutorials blog today there's a new tutorial posted showing you how to easily add watermarks to your images dynamically, either as a part of a cache generation or on upload.

I will show you how to use GD library. And, main task today is adding watermark to image and generate result as PNG image into browser. We will using PHP and GD library. This is nice library to work with images at server side. Also (as additional tasks) I will draw little frame over image and will draw some text. Between, you can use this method (of adding watermarks in realtime) to protect original photos.

Full code for the script is included in the post, showing the combining of two images into the one finished product. The code is also available for download if you'd like to get hacking right away.

tagged: watermark image gd combine tutorial


Philip Norton's Blog:
Desaturate Images With PHP
Sep 07, 2011 @ 13:51:26

In a new post to his blog Philip Norton shows how to desturate images with PHP using the already built-in GD libraries and the imagecopymergegray function.

To desaturate an image means to remove all non-greyscale colours from it, essentially creating a black and white version. To do this you can use the function imagecopymergegray(), which is part of the GD image library. This function is a little difficult to figure out, but what is does is sample one image into another and optionally changing the amount of colours that are kept during the sampling process. The function can be made to sample just a section of the image, or the whole thing.

He also offers an alternative method that uses imagefilter instead, passing it the IMG_FILTER_GRAYSCALE flag. Included is a full block of code for a method he recently included in a WordPress site to dynamically create thumbnails.

tagged: desaturate image grayscale gd tutorial imagecopymergegray imagefilter


Sameer Borate's Blog:
Generating a color palette from a image in php
Jun 21, 2011 @ 14:09:38

Sameer Borate has posted a simple method for creating a color palette from a given image using just PHP and GD.

In the previous post we saw how we can generate a screenshot from a url. In this post we will see how we can create a color palette of the screenshot (or any other image for that matter) using PHP and GD. This can be useful if you need to quickly get the color scheme of a website or a image. Below are a few example images and their palettes generated using the program.

With the help of his code it's as easy as a call on a GetMostCommonColors object (get_color) to return the hex codes for the values found most. Also included in the post is a sample snippet showing how to pull the details from the file and output it in a grid similar to this.

tagged: gd image process color palette popular tutorial


Smashing Magazine:
Image Manipulation With jQuery and PHP GD
Apr 05, 2011 @ 13:02:30

Smashing Magazine has a new post today about using jQuery and GD to manipulate images to upload an image, crop it and save that version to the remote server.

One of the numerous advantages brought about by the explosion of jQuery and other JavaScript libraries is the ease with which you can create interactive tools for your site. When combined with server-side technologies such as PHP, this puts a serious amount of power at your finger tips. [...] Sure, there are plugins out there that you can use to do this; but this article aims to show you what’s behind the process. You can download the source files for reference.

They walk you through the little bit of setup you'll need before things will be working and then move right into the upload form and PHP handling that goes with it. The Javascript that goes with it is all home-grown (not a plugin) and cleverly uses a DIV to simulate the bounding box for the crop.

tagged: tutorial image manipulate jquery gd crop