Tutorial on creating a thumbnail instance in php-PHP source code

Source: Internet
Author: User
Ec (2); this tutorial describes how to create flight thumbnails using PHP. In addition, you will learn how to process the entire image folder and create your own thumbnails. Because this requires the GD library, you will need at least one Guangdong 2.0.1PHP installation to enable. Next we will create a PHP script, which contains two features. The first scan provides any directory. JPG image, and create a specified folder thumbnail for each image using the GD image function. Create a script in the second function, which contains the link of some original images to scale down script ec (2); script

This tutorial describes how to create flight thumbnails using PHP. In addition, you will learn how to process the entire image folder and create your own thumbnails. Because this requires the GD library, you will need at least one Guangdong 2.0.1 PHP installation to enable.

Next we will create a PHP script, which contains two features. The first scan provides any directory. JPG image, and create a specified folder thumbnail for each image using the GD image function. The second function creates a script that contains thumbnails of links to some original images and all HTML files in the same directory. This may be the basis of advanced image library software.

The following code creates a function named createThumbs and obtains three parameters. The first and second are the corresponding directories, including the original image and the directory path where the thumbnail will be placed. The third parameter is what the width is for the thumbnail.


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}
";

// 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 );

// 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 }");
}
}
// Close the directory
Closedir ($ dir );
}
CreateThumbs ("upload/", "upload/thumbs/", 100 );
?>

First, we are searching for the images we open and directory traversal. JPG file. Next, we will create a thumbnail of each image in the directory. To create a thumbnail, read the imagecreatefromjpeg () function file and calculate the new thumbnail size. The width and height returned by the imagesx () and imagesy () functions are displayed respectively. Next, we will create a new image and use imagecreatetruecolor (). Finally, we copy and resize the original file with the imagecopyresized () function and save the imagejpeg Thumbnail ().

In the second part of the code, create a function named createGallery that obtains two parameters (the storage location of images and thumbnails in the directory) and create an HTML webpage, the thumbnail contains all relative paths of links to some original images.


Function createGallery ($ pathToImages, $ pathToThumbs)
{
Echo "Creating gallery.html
";

$ Output ="";
$ Output. ="Thumbnails";
$ Output. ="";
$ Output. ="


// Open the directory
$ Dir = opendir ($ pathToThumbs );

$ Counter = 0;
// Loop through the directory
While (false! ==( $ Fname = readdir ($ dir )))
{
// Strip the. and... entries out
If ($ fname! = '.' & $ Fname! = '..')
{
$ Output. ="

$ Counter + = 1;
If ($ counter % 4 = 0) {$ output. ="





$ Output. ="


";$ Output. =" "; "; ";}}}// Close the directoryClosedir ($ dir ); ";$ Output. ="
";
$ Output. = "";
$ Output. ="
";
$ Output. ="";
$ Output. ="";


$ Fhandle = fopen ("gallery.html", "w ");

Fwrite ($ fhandle, $ output );

Fclose ($ fhandle );
}

CreateGallery ("upload/", "upload/thumbs /");
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.