PHP Create thumbnail instances Tutorial

Source: Internet
Author: User
Tags html page imagejpeg php script

This tutorial will explain how to create a flight thumbnail that uses PHP. In addition you will learn how to handle the entire folder of images and create your own thumbnails. Since this requires the GD library, you will need to have at least one Guangdong 2.0.1 PHP installation enabled.

Below we will create a PHP script that contains two functions. Any directory provided by the first scan. JPG images, and for each one, create a specified folder thumbnail using the GD image feature. The second function creates a script that contains some of the original images of the linked thumbnails in the same directory as all HTML files. This may be the foundation of Advanced Picture Library software.

The following code creates a function named Createthumbs that will get 3 arguments. The first and second is the corresponding directory, which contains the original image and the directory path where the thumbnail will be placed. The third parameter is the width you want for the thumbnail.


<?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 it 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);

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 looking through it to open the image and traverse the directory. JPG files. Next, we create a thumbnail of each image in the directory. To create a thumbnail, we read the use of the Imagecreatefromjpeg () function file and compute the new thumbnail size. The Imagesx () and Imagesy () functions return the width and height of the image. Next, we create a new image, with Imagecreatetruecolor (). Finally, we copy and resize the original file with the imagecopyresized () function and save the imagejpeg thumbnail ().

The second part of the code creates a function named Creategallery that gets two parameters (where pictures and thumbnails are stored in the directory), and creates an HTML page that contains all the relative paths of the thumbnail of the original image's links.


<?php
function Creategallery ($pathToImages, $pathToThumbs)
{
echo "Creating gallery.html <br/>";

$output = "$output. = "$output. = "<body>";
$output. = "<table cellspacing=" 0 "cellpadding=" 2 "width=" ">";
$output. = "<tr>";

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. = "<td valign=" middle "align=" center "><a href=" {$pathToImages} {$fname} ">";
$output. = "$output. = "</a></td>";

$counter + 1;
if ($counter% 4 = 0) {$output. = "</tr><tr>";}
}
}
Close the Directory
Closedir ($dir);

$output. = "</tr>";
$output. = "</table>";
$output. = "</body>";
$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.