TIME NOW
World current time now,
CALENDAR
Calendar monthly, yearly
login GOLD PRICE, RATE
login HOROSCOPE
login DICTIONARIES, LISTS
login SCIENCE EDUCATION RELIGION
login FINANCE, BUSINESS
login WORLD COUNTRIES, WIKI

PHP scripts, code, examples, tutorial

Previous articlePage bottomNext article  ALL TOPICS

PHP. GD.Create thumbmails from images

 

#!/usr/local/bin/php
<?php

function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )
{
    // open the directory
    $dir = opendir( $pathToImages );
    
    // loop through it, looking for any/all JPG files:
    while ( false !== ( $fname = readdir( $dir ) ) ) {

        // parse path for the extension
        $info = pathinfo( $pathToImages . $fname );
        // continue only if this is a JPEG image
        if ( strtolower( $info[ 'extension' ] ) == 'jpg' ) {
            echo "Creating thumbnail for {$fname} <br />";
            
            // load image and get image size
            $img    = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
            $width  = imagesx( $img );
            $height = imagesy( $img );
            
            // calculate thumbnail size
            $new_width  = $thumbWidth;
            $new_height = floor( $height * ( $thumbWidth / $width ) );
            
            // create a new temporary image
            
            $tmp_img = imagecreatetruecolor( $new_width, $new_height );
            
            //$tmp_img = imagecreatetruecolor( $new_height, $new_width );
            
            // copy and resize old image into new image
            imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
            
            // save thumbnail into a file
            imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );

        } //strtolower( $info[ 'extension' ] ) == 'jpg'

    } //false !== ( $fname = readdir( $dir ) )
    // close the directory
    closedir( $dir );
}

createThumbs( "/var_www/main/gallery/", "/var_www/main/gallery/thumbs/", 150 );

?>

 
Previous articlePage topNext article  ALL TOPICS



 Use username: Guest, Anonymous, Programmer






QUOTES:
Existentialism is about being a saint without God; being your own hero, without all the sanction and support of religion or society.
Anita Brookner
He who lives by medical prescriptions lives miserably.
Proverb
Oh! too convincing -- dangerously dear -- In woman's eye the unanswerable tear!
Lord Byron