Miscellaneous

  Home arrow Miscellaneous arrow Thumbnails in PHP
MISCELLANEOUS

Thumbnails in PHP
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 17
    2003-04-30

    Table of Contents:
  • Thumbnails in PHP
  • Explanation
  • Conclusion

  •  
     

    SEARCH CODEWALKERS

    Thumbnails in PHP


    (Page 1 of 3 )

    Join Michael as he demonstrates how PHP, along with the GD library, can create thumbnails of all your images.

    By : Michael Bailey

    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 (you can use older versions with a slight change which I will explain).

    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
    # Constants
    define(IMAGE_BASE'/var/www/html/mbailey/images');
    define(MAX_WIDTH150);
    define(MAX_HEIGHT150);

    # 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/$widthMAX_HEIGHT/$height);

        
    # If the image is larger than the max shrink it
        
    if ($scale &lt1) {
            
    $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$img0000,
                             
    $new_width$new_height$width$height);
            
    imagedestroy($img);
            
    $img $tmp_img;
        }
    }

    # Create error image if necessary
    if (!$img) {
        
    $img imagecreate(MAX_WIDTHMAX_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);
    ?>

    More Miscellaneous Articles
    More By Codewalkers

    blog comments powered by Disqus
    escort BursaBursa escortescort BursaBursa escortescort BursaAntalya escort bayan Antalya eskort Antalya eskort Antalya escort bayanlar Antalya bayan escort

    MISCELLANEOUS ARTICLES

    - Attention: Forum and Site Maintenance
    - Oracle Database XE: Indexes and Sequences
    - Modifying Tables in Oracle Database XE
    - Oracle Database XE: Tables and Constraints
    - More on Oracle Databases and Datatypes
    - Oracle Database XE Datatypes: Datetime and L...
    - Oracle Database XE Datatypes: Character and ...
    - From Databases to Datatypes
    - Firefox 3.6.6 Released with Improved Plug-in...
    - Attention Bloggers: WordPress 3.0 Now Releas...
    - Reflection in PHP 5
    - Inheritance and Other Advanced OOP Features
    - Advanced OOP Features
    - Linux from Scratch V.6.6 Review
    - Linux Gaining in Strength

    Developer Shed Affiliates

     



    © 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap