WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Dynamic thumbnails using the GD image library
Categories : PHP, GD image library, Graphics Click here to Update Your Picture
Ben Barnett
Date : Jun 08th 2004
Grade : 2 of 5 (graded 8 times)
Viewed : 6960
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Ben Barnett
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 


<?php
function thumb_makethumb() {

    switch(
$_FILES['image_file']['type']){
        case
'image/png':
        case
'image/x-png':
           
$type = 'png';
            break;
       
        case
'image/jpeg':
        case
'image/pjpeg':
           
$type = 'jpg';
            break;

        case
'image/gif':
           
$type = 'gif';
            break;
           
       
//case 'image/tiff':
            //$type = 'tif';
            //break;
       
        //case 'image/x-photoshop':
        //    $type = 'psd';
        //    break;
           
       
default:
           
$type = 'none';
            break;
    }   


   
// Make temporary file name and location a variable
   
$temp_file_name = '$_FILES['image_file']['name'];
   
    // Get image attributes of temporary file
    $image_attribs = getimagesize($temp_file_name);
   
    // Make temporary file a resource
    if($type == '
png')
        {
        $im_temp = imageCreateFromPNG($temp_file_name);
        }
    elseif($type == '
jpg')
        {
        $im_temp = imageCreateFromJPEG($temp_file_name);
        }
    elseif($type == '
gif')
        {
        $im_temp = imageCreateFromGIF($temp_file_name);
        }
    // Set max width and height. Largest dimension will be set the this size either by
    // shrinking or enlarging the image
    $th_max_width = 150;
    $th_max_height = 150;
   
    // Find ration of dimensions
    $ratio = ($image_attribs[0] > $image_attribs[1]) ? $th_max_width/$image_attribs[0] : $th_max_height/$image_attribs[1];
   
    // Scale to max width/height using ratio
    $th_width = $image_attribs[0] * $ratio;
    $th_height = $image_attribs[1] * $ratio;
   
    // Create a true colour image and use anti-aliasing
    $im_new = imagecreatetruecolor($th_width,$th_height);
    imageAntiAlias($im_new,true);

    // Set thumb folder and file name
    $th_file_name = '
thumbs/' . $_FILES['image_file']['name'];
   
    // And copy the original image resized onto the new thumbnail.
    imageCopyResampled($im_new,$im_temp,0,0,0,0,$th_width,$th_height, $image_attribs[0], $image_attribs[1]);

    // Output to file/browser as a PNG. Can also output as JPEG using the imageJPEG() function.
       header("content-type: image/png");
        imagePNG($im_new);
    // Clean up image resource
    imagedestroy($im_new);
}
?>


This function can be called to act on an uploaded image and output a thumbnail of it to the browser. Can only operate on GIF, PNG and JPEG files as these are the only filetypes the GD library can work with. Requires the GD library to be installed on the webserver.



Create Thumbnails - resize an image - jpeg, jpg, gif, png to the specifed width and height in proportion without loosing out on pixcel quality.
Categories : PHP, GD image library, Graphics
Display a bar chart based on random values.
Categories : Graphics, PHP, GD image library, Charts and Graphs
Advanced Image WaterMarker
Categories : PHP, PHP Classes, GD image library, Graphics, Object Oriented
Line graphics generation library written in PHP + GD library (spanish comments)
Categories : PHP, Graphics, GD image library
A captcha image allows you to prevent spam posting when users reload the page and stop bots from submitting forms automatically. This version allows you to use your own fonts (.ttf) to show the text.
Categories : PHP, Security, Graphics, GD image library
Simple PHP Bar Graph using GD library
Categories : PHP, GD image library, Graphics, Arrays
CAPTCHA[Image verification]
Categories : PHP, Security, GD image library, Graphics, Sessions
Simple class that uses GD to draw pie charts. After the class definition there's some sample code to demonstrate how you use the class.
Categories : Graphics, PHP, PHP Classes, GD image library, Charts and Graphs
imageMarker v 3.00 with new advanced features
Categories : PHP, PHP Classes, Graphics, GD image library
Generate image with random number (CAPTCHA)
Categories : PHP, GD image library, Graphics, Security
Image Generation Class ( PNG Format )
Categories : PHP, GD image library, PHP Classes, Graphics
PHP Image Compression using GD library
Categories : PHP, Compression, GD image library, Graphics
a simple pie-chart in php3 (with gd)
Categories : PHP, Graphics, GD image library, Charts and Graphs
crop and resize image class using gd library function
Categories : PHP, PHP Classes, GD image library, Graphics
PHP Email image generator - hide your email from bots - using the GD Library
Categories : PHP, Graphics, GD image library, Beginner Guides
 Tim Johnson wrote : 1125
Knowing *very* little about PHP (yet) it would be nice to have a practical example of using the code.
 
 Sander van der Linden wrote : 1130
there`s a typo in:

 // Make temporary file name and location a variable 
    $temp_file_name = `$_FILES[`image_file`][`name`]; 

should be

 // Make temporary file name and location a variable 
    $temp_file_name = `$_FILES[`image_file`][`name`]`; 

as far as I know, but Im a php noob :)
 
 lucas seiden wrote :1637
i ran this script and the first thing that came to me was an error on line 34. 

as a user already commented out but gave a wrong correction. the right way to use it would be:

    // Make temporary file name and location a variable
    $temp_file_name = `$_FILES[image_file][name]`;