By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,066 Members | 1,841 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,066 IT Pros & Developers. It's quick & easy.

php script for thumbnails

P: n/a

Where to find a php script to upload jpg files and make thumbnails of the
jpg files ?

Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies

P: n/a
"Johan" <> writes:
Where to find a php script to upload jpg files and make thumbnails of the
jpg files ?

DOn't have the complete thing, but here a few functions to help:

# Function CreateThumbnail - creates the thumbnail version
# of a photo at a fixed size (81 high and correct width)
# INPUT - sourcefile name, targetfile name (thumbnail)
# OUTPUTS - new file in targetfile location
# RETURNS - 0 on success, message otherwise
function CreateThumbnail ($sourcefile, $targetfile) {
$desiredY = 81;

// Get the dimensions of the source picture

if ($picsize == false) { // failed
return("Could not get size on picture $sourcefile");

$source_x = $picsize[0];
$source_y = $picsize[1];

$ratio = $desiredY / $source_y;

$newX = (int) ($ratio * $source_x);
$newY = (int) ($ratio * $source_y);
if ($msg = ResizeToFile($sourcefile, $newX, $newY, $targetfile)) {
return("Resize failed to targetfile: $targetfile ($msg)");


} // end function CreateThumbnail

/* Function: resizeToFile resizes a picture and writes it to the harddisk
* $sourcefile = the filename of the picture that is going to be resized
* $dest_x = X-Size of the target picture in pixels
* $dest_y = Y-Size of the target picture in pixels
* $targetfile = The name under which the resized picture will be stored
* $jpegqual = The Compression-Rate that is to be used
function ResizeToFile ($sourcefile, $dest_x, $dest_y, $targetfile, $jpegqual=60)

/* Get the dimensions of the source picture */
if ($picsize == false) {
return("Could not get size on file: $sourcefile\n");

$source_x = $picsize[0];
$source_y = $picsize[1];

$source_id = imageCreateFromJPEG("$sourcefile");

if (! $source_id) {
return("Could not create image from jpeg file: $sourcefile\n");

/* Create a new image object (not neccessarily true colour) */
$target_id=imagecreatetruecolor($dest_x, $dest_y);

/* resize the original picture and copy it into the just created image
object. Because of the lack of space I had to wrap the parameters to
several lines. I recommend putting them in one line in order keep your
code clean and readable
$target_pic=imagecopyresampled($target_id,$source_ id,

/* Create a jpeg with the quality of "$jpegqual" out of the
image object "$target_pic".
This will be saved as $targetfile */
$stat = imagejpeg ($target_id,"$targetfile" ,$jpegqual);
if (! $stat) {
return("Failed to create new image file: $targetfile");

return 0;
} // end function ResizeToFile

__________________________________________________ _________________
John Murtari Software Workshop Inc.
jmurtari@following domain 315.635-1968(x-211) "TheBook.Com" (TM)
Jul 17 '05 #2

P: n/a
Here are some scripts I have written to dynamically build a HTML table of
thumbnails & insert it as an OBJECT element in the page from which it is

The top-level page contains JavaScript to create the OBJECT element:

<script LANGUAGE="JavaScript1.2">
OutStr = '<object type="text/html" data="BuildThumbnailGrid.php?win_width='
+ GetWinWidth() + '&img_width=100&border_width=0&cell_space=40" width="100%"
height="100%" border=0>If you are reading this your browser doesn’t
support Object element...oprostite.'

You can use this JS function to find out how wide your browser window is,
otherwise just enter no. of pixels:

<script LANGUAGE="JavaScript1.2">
function GetWinWidth()
if (navigator.appName == 'Netscape' && document.layers != null)
return WinWidth = window.innerWidth;
if (document.all != null)
return WinWidth = document.body.clientWidth;

The OBJECT element calls the BuildThumbnailGrid.php script, which in turn
finds all the *.JPG files in the current directory (or whichever you
specify) and calls MakeThumbnail.php to make a thumbnail on the fly.

File BuildThumbnailGrid.php:
// Pass available window width as win_width using HTTP GET method
// Pass thumbnail width as img_width HTTP GET method
// Pass table border width as border_width using HTTP GET method
// Pass cell padding as cell_pad using HTTP GET method
// Pass cell spacing width as cell_space using HTTP GET method

$win_width = $_GET["win_width"];
$img_width = $_GET["img_width"];
$border_width = $_GET["border_width"];
$cell_pad = $_GET["cell_pad"];
$cell_space = $_GET["cell_space"];

// Calculate no. of columns that will fit window
$num_cols = floor(($win_width - $cell_space)/($img_width + $cell_space)) -
$curr_col = 0; //Set column counter to zero

echo '<table border=' . $border_width . ' cellspacing=' . $cell_space .
foreach (glob("*.JPG") as $filename) /* Needs PHP 4 >= 4.3.0, PHP 5 to
work. Use readdir() to build array otherwise. */
if ($curr_col == 0)
echo '<tr>';
echo '<td>';
echo '<a href="' . $filename . '">';
echo '<img src="MakeThumbnail.php?in_file=' . $filename . '&in_width=' .
$img_width . '">';
echo '</a>';
echo '</td>';
if ($curr_col < $num_cols)
$curr_col += 1;
echo '</tr>';
$curr_col = 0;
if ($curr_col < $num_cols)
echo '</tr>';
echo '</table>';


File MakeThumbnail.php:
// Pass arguments with HTTP GET method
// Pass $filename as in_file
// Pass $new_width as in_width
$filename = $_GET["in_file"];
if (isset($_GET["in_width"]))
$new_width = $_GET["in_width"];
$new_width = 120;

// Set content type
header('Content-type: image/jpeg');

// Get image dimensions & resize
list($width, $height) = getimagesize($filename);
$aspect_ratio = $height / $width;
$new_height = $new_width * $aspect_ratio;

// Resample
$output_image = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($output_image, $image, 0, 0, 0, 0, $new_width,
$new_height, $width, $height);

// Output image
imagejpeg($output_image, null, 100);
"Johan" <> wrote in message

Where to find a php script to upload jpg files and make thumbnails of the
jpg files ?


Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.