Downloading Images from a URL Happy Leap Year
Jan 28

Working with images in PHP is fun and at the same time hard for it requires a thorough understanding how certain libraries work especially PHP GD Library. GD Lib doesn’t come by default in your webhosting account. You have to ask your webhost’s tech support to compile and build it for you. You can check by using phpinfo();

What is the GD library?

GD is an open source code library for the dynamic creation of images by programmers. GD is written in C, and “wrappers” are available for Perl, PHP and other languages. GD creates PNG, JPEG and GIF images, among other formats. GD is commonly used to generate charts, graphics, thumbnails, and most anything else, on the fly. While not restricted to use on the web, the most common applications of GD involve website development.

I have done several sites that incorporates the use of GD libraries. Mostly, counter sites and captcha’s. Let me share this simple script which has been my basis for doing combined images in my projects:


<?php
/*
* This script opens and combine image1, image2 and image3 to create imageN
* The HTML displays the imageN
*/

$img1=imagecreatefromjpeg("image1.jpg");
$img2=imagecreatefromjpeg("image2.jpg");
$img3=imagecreatefromjpeg("image3.jpg");

$imgx=imagesx($img1);
$imgy=imagesy($img1);

$imgN=imagecreatetruecolor($imgx*3,$imgy);

//imagecopy ( resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y, int src_w, int src_h )
imagecopy($imgN,$img1,0,0,0,0,$imgx,$imgy);
imagecopy($imgN,$img2,$imgx*1,0,0,0,$imgx,$imgy);
imagecopy($imgN,$img3,$imgx*2,0,0,0,$imgx,$imgy);

imagejpeg($imgN,'imageN.jpg');

echo '<img src="imageN.jpg" mce_src="imageN.jpg">';

?>

Make sure that JPEG and GIF support is enabled in your GD Library.

Happy scripting.

, , , , , , , , ,


Related Posts:
  • Downloading Images from a URL
  • HTML Tables: Loosing Its Place in the Web

  • Leave a Reply

    *
    To prove you're a person (not a spam script), type the security word shown in the picture.
    Anti-Spam Image

    FireStats icon Powered by FireStats

    This site is protected with Urban Giraffe's plugin 'HTML Purified' and Edward Z. Yang's Powered by HTML Purifier. 39 items have been purified.