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 (34)
  SEO (9)
  Visual Basic (12)
  CSS (13)
  SSI (5)
  XML (12)
  C# (14)

  Developer News

May 17, 2008
Learn HTML forms
About
 
May 16, 2008
Who Are Your Top Friends on Facebook?
About
 
May 15, 2008
Reader Question - Would you host your client's work on your website?
About
 
May 15, 2008
How to Create an Ajax Autocomplete Text Field: Part 6
WebReference.com
 
May 14, 2008
Poll: Are the browser safe colors still needed?
About
 
May 14, 2008
Google Doctype launched
About
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /PHP

Thumbnails with PHP part I 

  Views:    3039
  Votes:    2
by Oskar Syahbana 12/16/03 Rating: 

Synopsis:

This is the first part of making thumbnailing-script with PHP.
Pages: 
The Article

Depending upon how it's done, displaying a page of thumbnail images can be very cumbersome. Allowing the browser to resize images requires the client browser to download the entire, full-size image, then clumsily resize the image to a specified size. This causes the page to load very slowly and creates unavoidable distortion in the resulting images. The other option is to make separate thumbnail images for each individual image. This is fine if you only plan to display a few images, but becomes unrealistic on a large scale or a site involving dynamic images. Fortunately there are ways around these problems using the GD library in PHP.

This tutorial will outline how to write a PHP script to create thumbnail images on the fly. Since this requires the GD library, you will need an installation of PHP with at least GD 2.0.1 enabled (usually host nowadays have this installed already).

The script we will be writing receives one parameter on the query string which corresponds to the relative path of the full-size image. This address is to be placed as the src field of an image tag. An example image tag might look like this: < img src = "/thumbnail.php?dir/image.png" >. With no further ado let us procede to the source code.

Source Code
PHP:
<?PHP
# Constants
define(IMAGE_BASE, '/var/www/html/ozzie/images');
define(MAX_WIDTH, 150);
define(MAX_HEIGHT, 150);

# Get image location
$image_file = str_replace('..', '', $_SERVER['QUERY_STRING']);
$image_path = IMAGE_BASE . "/$image_file";

# Load image
$img = null;
$ext = strtolower(end(explode('.', $image_path)));
if (
$ext == 'jpg' || $ext == 'jpeg') {
    
$img = @imagecreatefromjpeg($image_path);
} else if (
$ext == 'png') {
    
$img = @imagecreatefrompng($image_path);
# Only if your version of GD includes GIF support
} else if ($ext == 'gif') {
    
$img = @imagecreatefrompng($image_path);
}

# If an image was successfully loaded, test the image for size
if ($img) {

    
# Get image size and scale ratio
    
$width = imagesx($img);
    
$height = imagesy($img);
    
$scale = min(MAX_WIDTH/$width, MAX_HEIGHT/$height);

    
# If the image is larger than the max shrink it
    
if ($scale < 1) {
        
$new_width = floor($scale*$width);
        
$new_height = floor($scale*$height);

        
# Create a new temporary image
        
$tmp_img = imagecreatetruecolor($new_width, $new_height);

        
# Copy and resize old image into new image
        
imagecopyresized($tmp_img, $img, 0, 0, 0, 0,
                         
$new_width, $new_height, $width, $height);
        
imagedestroy($img);
        
$img = $tmp_img;
    }
}

# Create error image if necessary
if (!$img) {
    
$img = imagecreate(MAX_WIDTH, MAX_HEIGHT);
    
imagecolorallocate($img,0,0,0);
    
$c = imagecolorallocate($img,70,70,70);
    
imageline($img,0,0,MAX_WIDTH,MAX_HEIGHT,$c2);
    
imageline($img,MAX_WIDTH,0,0,MAX_HEIGHT,$c2);
}

# Display the image
header("Content-type: image/jpeg");
imagejpeg($img);
?>  

Pages: 

Similar/related articles:


 
  Sponsors