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

September 7, 2008
Web Design Clinic - Art meets Webdesign
About
 
September 6, 2008
Browser safe colors - do we care?
About
 
September 6, 2008
Get a Daily CSS Tip
About
 
September 5, 2008
It's safe to use Google Chrome now
About
 
September 5, 2008
Add a comment
.net
 
September 5, 2008
The Partial Function Application in JavaScript
WebReference.com
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /PHP

PHP Automated Thumbnails 

  Views:    18440
  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: firstback2 3 4 forwardlast
The Article

The Idea

We would like to use the URL of the image to supply our wishes to the script. A possible URL could be:
http://www.example.com/img.php?f(3cb7f702a5967)+w(300) Using this URL, we would like to get the image identified by 3cb7f702a5967, resized to a width of 300 pixels and the corresponding height. The possibilities of our newly created script will be:

  • resize an image to a given width and/or height, possibly preserve the aspect ration
  • accept new image sizes as absolute values, and as percentages of the original size
  • resize an image when width or height exceed a given limit;
  • return the image as a PNG or JPEG-file.


What We Need

To build and use this script, you will need PHP compiled with the GD library and JPEG-support.

The Syntax

For telling our script what to return, we will use the following syntax. The arguments are delimited by one or more +-es. An example URL could be:
http://www.example.com/img.php?f(3cb7f702a5967)+w(300)

 Code    Meaning 
f(3cb7f702a5967)              The 13-character filename of the requested image. The images are saved without an extension, with the name defined by the current value of uniqid();. That way, new uploaded files will automatically have an unique id. 
w(123) or w(10%)  The wanted width of the returned image, either in absolute pixels or relative to the original size. 
h(123) or h(10%) Same as above, but this is for the height of the image. When just one of the size-commands are given, the other size is automatically calculated, so that de aspect ratio of the image stays the same.
x(123)  Defines the maximum width of the returned image. Resizing only takes place if the original width exceeds the given maximum. Only absolute values are accepted. 
y(123)  Same but for height. 
t(png|jpg) Defines the requested file type of the image. If no type is given, the image is returned as the original saved type. 
q(100)  Only for JPEG. Asks for a specific quality of the returned image. Quality can vary from 0 to 100. 

Pages: firstback2 3 4 forwardlast

Similar/related articles:


 
  Sponsors