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 3 4 forwardlast
The Article

The Script

Now that we have defined the syntax of the script, it is time to take a closer look at the actual code.

Checking the arguments

First of all, the given arguments are read from the query string by a simple preg. The regular expression returns all possible tags. Then the tags have to be checked for incorrect values. Defining an array of possible tags and corresponding regular expressions, is an easy way to check the tags in a for-loop. The checked and correct tags are saved in an associative array for later use. As a last check, the script verifies that a filename is given and that that file really does exist.

// define the base image dir
$base_img_dir = \"./img/\";

// find tags
preg_match_all(\"/\\+*(([a-z])\\(([^\\)]+)\\))\\+*/\", $QUERY_STRING,
                $matches, PREG_SET_ORDER);

// empty array and set regular expressions for the check
$tags = array();
$check = array( \"f\" => \"[0-9a-zA-Z]{13}\",
                \"w\" => \"[0-9]+%?\",
                \"h\" => \"[0-9]+%?\",
                \"x\" => \"[0-9]+\",
                \"y\" => \"[0-9]+\",
                \"t\" => \"jpg|png\",
                \"q\" => \"1?[0-9]{1,2}\" );

// check tags and save correct values in array
for ($i=0; $i<count($matches); $i++) {
    if (isset($check[$matches[$i][2]])) {
        if (preg_match(\'/^(\'.$check[$matches[$i][2]].\')$/\',
               $matches[$i][3])) {
            $tags[$matches[$i][2]] = $matches[$i][3];
        }
    }
}

function notfound() {
    header(\"HTTP/1.0 404 Not Found\");
    exit;
}

// check that filename is given
if (!isset($tags[\"f\"])) {
    notfound();
}

// check if file exists
if (!file_exists($base_img_dir.$tags[\"f\"])) {
    notfound();
}

Pages: firstback1 3 4 forwardlast

Similar/related articles:


 
  Sponsors