Create Thumbnails from a folder of images using PHP
I have a f开发者_如何学Pythonolder of jpegs, and a MySQL database that I hope to use to manage them. The database has one table: 'images', and 7 fields: 'imgID', 'imgTitle', 'imgURL', 'imgDate', 'imgClass', 'imgFamily', & 'imgGender'. The primary key is 'imgID' and the Index Key is 'imgDate'.
What I would like to do is create a PHP file that will go through my folder of images (all jpegs), and create thumbnails of them, which can then be used when displaying links to the images on my webpage. As I will be adding more images to the folder in the future, it would be good to prevent the code from creating doubles of images it has already created thumbnails of.
All literature I have come across recommends using the gd images library to do this, but I'm open to suggestions.
As I'm new to both MySQL and PHP, I was hoping someone could help me with the code. Everything I have tried has failed.
the directory where the images are located is new_arrivals_img/ relative to the site root, and the thumbnails should be placed in new_arrivals_img/thumbnails/ also relative to the site root.
Right now I'm building the site, and thus using MAMP to host it locally. I have had some problem figuring out relative paths for my images. Is there a way to set new_arrivals_img/ as the root?
Supposedly ImageMagick is better on memory but I always have GD at my disposal and it has always done the job for me. Make sure you allocate enough memory in your php.ini
Then use a script like this:
<?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 );
// 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 );
}
// call createThumb function and pass to it as parameters the path
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width.
// We are assuming that the path will be a relative path working
// both in the filesystem, and through the web for links
createThumbs("new_arrivals_img/","new_arrivals_img/thumbnails/",100);
?>
http://www.webcheatsheet.com/php/create_thumbnail_images.php
I wouldn't use gd at all, i would use imagemagick. The thumbnails will look much better.
精彩评论