Thumbnails..
Posted: Mon Apr 09, 2012 12:23 pm
Hi..I am very new to php, but get the basic concepts....or so I thought..
I am trying to make a website...for the most part I am successful. I am having a couple of issues.
I am trying to make the images into thumbnails. I have found many different types of thumbnails online, but I don't understand how it works...
I do have the GD library.
I want to use a code similar to this:
Others from that site say they have it working just fine. How?
Do I just put the scripts into the images folder?
Do I attach it at the top of the page, say my products page?
I am using mysql, and the images I am trying to make into thumbnails are in tables. I want to be able to go to the products page, and then click on a 100px x 200 px thumbnail, which will then blow up to full size. I would assume that I would have to run the thumbnail script, and then place the small image into the table?
I just don't know how to implement the php script to get the thumbnails to be created.
Can someone with a lot more knowledge help me out with how to do this? My tables are made, and I have the images already. Just need to make the images smaller, and then bigger again.
I am sure I am missing something minor, and that someone who is not a newbie can help...
I am trying to make a website...for the most part I am successful. I am having a couple of issues.
I am trying to make the images into thumbnails. I have found many different types of thumbnails online, but I don't understand how it works...
I do have the GD library.
I want to use a code similar to this:
Code: Select all
<?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("upload/","upload/thumbs/",100);
?>
Code: Select all
<?php
function createGallery( $pathToImages, $pathToThumbs )
{
echo "Creating gallery.html <br />";
$output = "<html>";
$output .= "<head><title>Thumbnails</title></head>";
$output .= "<body>";
$output .= "<table cellspacing=\"0\" cellpadding=\"2\" width=\"500\">";
$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 .= "<img src=\"{$pathToThumbs}{$fname}\" border=\"0\" />";
$output .= "</a></td>";
$counter += 1;
if ( $counter % 4 == 0 ) { $output .= "</tr><tr>"; }
}
}
// close the directory
closedir( $dir );
$output .= "</tr>";
$output .= "</table>";
$output .= "</body>";
$output .= "</html>";
// open the file
$fhandle = fopen( "gallery.html", "w" );
// write the contents of the $output variable to the file
fwrite( $fhandle, $output );
// close the file
fclose( $fhandle );
}
// call createGallery function and pass to it as parameters the path
// to the directory that contains images and the path to the directory
// in which thumbnails will be placed. We are assuming that
// the path will be a relative path working
// both in the filesystem, and through the web for links
createGallery("upload/","upload/thumbs/");
?>Do I just put the scripts into the images folder?
Do I attach it at the top of the page, say my products page?
I am using mysql, and the images I am trying to make into thumbnails are in tables. I want to be able to go to the products page, and then click on a 100px x 200 px thumbnail, which will then blow up to full size. I would assume that I would have to run the thumbnail script, and then place the small image into the table?
I just don't know how to implement the php script to get the thumbnails to be created.
Can someone with a lot more knowledge help me out with how to do this? My tables are made, and I have the images already. Just need to make the images smaller, and then bigger again.
I am sure I am missing something minor, and that someone who is not a newbie can help...