473,396 Members | 2,093 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

php script for thumbnails

Hi,

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

Johan
Jul 17 '05 #1
2 3688
"Johan" <me@knoware.nl> 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
$picsize=getimagesize("$sourcefile");

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

return(0);

} // 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 */
$picsize=getimagesize("$sourcefile");
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,
0,0,0,0,
$dest_x,$dest_y,
$source_x,$source_y);

/* 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
__________________________________________________ _________________
John Murtari Software Workshop Inc.
jmurtari@following domain 315.635-1968(x-211) "TheBook.Com" (TM)
http://thebook.com/
Jul 17 '05 #2
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
called.

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.'
document.write(OutStr);
document.write('</object>');
</script>

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;
}
}
</script>

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:
-----------------------------
<?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)) -
1;
$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;
else
{
echo '</tr>';
$curr_col = 0;
}
}
if ($curr_col < $num_cols)
echo '</tr>';
echo '</table>';

?>

File MakeThumbnail.php:
-------------------------
<?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"];
else
$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" <me@knoware.nl> wrote in message
news:10*************@corp.supernews.com...
Hi,

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

Johan

Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Preston Crawford | last post by:
I'm looking to quickly get a photo album online. Very simple, thumbnails, a few pages, maybe a description, but hopefully a small script that's easy to edit and work into my existing site. I know...
3
by: dan | last post by:
does anyone know why this script doesnt work propertly the images on the right side can not be displayed here is the script i use function initSettings () { for ( var i = 1; i <= 50; i++){...
7
by: xiibweb | last post by:
Hi I am struggling to find a code meeting my requirements... I want to display 4 thumbnails of photos in a row (table 1X4). When any thumb is clicked the large image size shud appear just below...
9
by: K P S | last post by:
Hi. I'm looking for a small script that will take a .zip archive and pull the first .jpg from the archive and convert it to a .png. The reason for this is I want to have tuhmbnails for these...
2
by: ranger7419 | last post by:
I'm trying to figure out why this script will work in IE 6 but not Firefox, and so I need someone here with a far better grasp on javascript to explain this. Basically, I have a page with several...
4
by: J. Frank Parnell | last post by:
Hi there, I have a list of links which point to e.g. thescript.php?album=somePictures1 thescript.php?album=somePictures2 This list is about 3000 links. Each album may have 500 or more...
5
by: JJ | last post by:
I have a gallery-like application. (The gallery will be actually presented in Flash, but the management (cms) of the images will be in asp.net. ) My question is, is it ok to create Thumbnail...
1
by: sachinmanath | last post by:
i have a code which works in internet explorer but not in firefox. I have a page with several thumbnails. Above these thumbnails I placed a large picture. So, when you click a thumb, the large pic...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.