PHP Dynamic Image Generation - php forms - Phptpoint.com

PHP Dynamic Image Generation

Best PHP Training with Live Project in Noida Fee is only 7500/-Click here for FREE DEMO.

/ins>

PHP: Dynamic Image Generation

At first, have a look at the following script that creates CAPTCHA image.Next, we will explain each step in detail.

In the given above example we created an empty funcion createDynamic_image().
using rand(0,999) function generates a random number from 0 to 999.
pass rand(0,999) inside md5(random(0,999)) it generate a totally random string.
We don’t need a 32 character long string so we trim it down to 5 string using substr($md5, 10, 5) function.
if you want more than 5 character then increase the value of third parameter of substr(6).

Create the image resource using ImageCreate($width, $height) in which pass height and width of generated image.

Make three colors White, Black and Gray using ImageColorAllocate($image, 255, 255, 255), ImageColorAllocate($image, 0, 0, 0) and ImageColorAllocate($image, 204, 204, 204).

Make the background black using ImageFill($image, 0, 0, $black) function and Generated image white color using ImageString($image, 3, 30, 3, $pass, $white).

Create some lines to make it a little bit harder for any bots to break CAPTCHA easily.

Tell the browser what kind of file is come in withe the help of header(“Content-Type: image/jpeg”) function.

Display the new created image in JPEG format using ImageJpeg($image) function.

Use dynamically generated images on the other pages

Since the header can be sent only once and it is the only way to tell the browser that you are going to send image, it is not so easy to insert automatically generated images to the regular pages. There are three ways to do it:

  • The whole page can consist of the image.
  • You can save image in the file and then refer to it using <img> HTML descriptor.
  • You can put a script producing image in HTML descriptor, e.g.:

Keep Updating yourself