iNET Interactive - Online Advertising Agency
          
   Home    Authors    About    Login    Contact Us
   Search:   
Advanced Search     
  Articles

  ASP (26)
  ASP.NET (19)
  C and C++ (4)
  CFML (2)
  CGI and Perl (16)
  Flash (2)
  Java (7)
  JavaScript (28)
  PHP (92)
  MySQL (13)
  MSSQL (3)
  HTML (35)
  SEO (9)
  Visual Basic (12)
  CSS (13)
  SSI (5)
  XML (12)
  C# (14)

  Developer News

July 20, 2008
Wiki: A Solution to the Web Design Problem
About
 
July 19, 2008
Top 10 Social Networking Sites
About
 
July 19, 2008
Plurk or Twitter: Which one is Better?
About
 
July 19, 2008
Create Menus with Lists and CSS
About
 
July 17, 2008
Poll: What do you do when you can't design?
About
 
July 17, 2008
22 Reasons to Plurk
About
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /PHP

PHP Automated Thumbnails 

  Views:    18166
  Votes:    9
by Gijs van Tulder 4/30/04 Rating: 

Synopsis:

The idea is simple, yet powerful: supply an image once; retrieve it in all possible formats, sizes and file types. We will build a PHP script that will automate these actions for us.
Pages: firstback1 2 3 forwardlast
The Article

Returning the image

The last step in our script is the actual returning of the image. The image and corresponding headers are returned as set in the query string. If the wanted type is not given, the image is returned as the original type of the saved file.

// check for a given jpeg-quality, otherwise set to default
if (!isset($tags[\"q\"])) {
    $tags[\"q\"] = 75;
}

// returning the image
switch ($tags[\"t\"]) {
    case \"jpg\":
        header(\"Content-type: image/jpeg\");
        imagejpeg($img_out, \"\", $tags[\"q\"]);
        exit;
    case \"png\":
        header(\"Content-type: image/png\");
        imagepng($img_out);
        exit;
   default:
        notfound();
}


What We Did

Now we have a script that fulfils the tasks we described above. Images are uploaded once and can be resized to whatever you want, without manual action. For the editors, the users of your CMS, uploading images is very simple. They upload the file in the size and type they want, and the script takes care of the resizing and converting. Without more work, your site can show thumbnails in many different sizes. If you, in the near future, would like to redesign your site, it is easy to get a new image size, without having to resize your whole archive.

The complete script discussed above, is available for download here. A demo of the script is running here, change the arguments in the query string to try the different commands like w() and t().

Where to go from here

Some thoughts about possible extensions of this script:

Enabling GIF-output. Using the command-line pngtopnm and pmmtogif programs, you can convert PNG-images to GIF on the fly. That way, you do not have to worry about older browsers not supporting the PNG-format.
Uploading of files in more formats. For the users of your CMS, it would be very easy if they could upload images as GIF, Windows bitmaps and TIFF. Using open source conversion utilities, you can still save these images as PNG of JPEG after uploading.
Using normal filenames for the images. Filenames generated with uniqid(); are easy and unique, but not very descriptive.
Saving file information like default ALT-tags in a database. You can easily find images for use in articles.

Pages: firstback1 2 3 forwardlast

Similar/related articles:


 
  Sponsors