473,776 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3706
"Johan" <me@knoware.n l> 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=getima gesize("$source file");

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($s ourcefile, $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=getima gesize("$source file");
if ($picsize == false) {
return("Could not get size on file: $sourcefile\n") ;
}

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

$source_id = imageCreateFrom JPEG("$sourcefi le");

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

/* Create a new image object (not neccessarily true colour) */
$target_id=imag ecreatetruecolo r($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=ima gecopyresampled ($target_id,$so urce_id,
0,0,0,0,
$dest_x,$dest_y ,
$source_x,$sour ce_y);

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

return 0;
} // end function ResizeToFile

--
John
_______________ _______________ _______________ _______________ _______
John Murtari Software Workshop Inc.
jmurtari@follow ing domain 315.635-1968(x-211) "TheBook.Co m" (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="JavaS cript1.2">
OutStr = '<object type="text/html" data="BuildThum bnailGrid.php?w in_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...opros tite.'
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="JavaS cript1.2">
function GetWinWidth()
{
if (navigator.appN ame == 'Netscape' && document.layers != null)
{
return WinWidth = window.innerWid th;
}
if (document.all != null)
{
return WinWidth = document.body.c lientWidth;
}
}
</script>

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

File BuildThumbnailG rid.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_wid th"];
$cell_pad = $_GET["cell_pad"];
$cell_space = $_GET["cell_space "];

// Calculate no. of columns that will fit window
$num_cols = floor(($win_wid th - $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="MakeThumbn ail.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.p hp:
-------------------------
<?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($f ilename);
$aspect_ratio = $height / $width;
$new_height = $new_width * $aspect_ratio;

// Resample
$output_image = imagecreatetrue color($new_widt h, $new_height);
$image = imagecreatefrom jpeg($filename) ;
imagecopyresamp led($output_ima ge, $image, 0, 0, 0, 0, $new_width,
$new_height, $width, $height);

// Output image
imagejpeg($outp ut_image, null, 100);
?>
"Johan" <me@knoware.n l> wrote in message
news:10******** *****@corp.supe rnews.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
3257
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 about hot scripts, etc. but I was wondering if any one could recommend one? Secondly, I also want to setup a journal. It's not really a "blog" although I guess blog software may work. But it isn't going to be a message board or anything like...
3
1760
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++){ removeMovieClip("thumbnails.thumbBox" + i) } thumbSpacing = 72 thumbsInRow = 5
7
1644
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 the 4 thumb aligned as center... along with description of photo. Anyone cn help me out plzz Regards,
9
3579
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 archives in nautilus under gnome. I would like something similar to the following code, which will pull a thumbnail from an openoffice.org (oasis) document. What I want is a little more involved, I guess, since I don't know the name of the file...
2
3566
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 thumbnails. Above these thumbnails I placed a large picture with text next to it to describe what the picture is about. So, when you click a thumb, the large pic AND text change dynamically to reflect the thumbnail -- see...
4
4415
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 pictures in it. the script looks in the specified dir, and creates thumbnails if they are not present. So, displaying a particular album often takes quite a while. I've set the proper php.ini stuff to accomodate the long script_execution's. Thats all
5
1993
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 images on the fly by resizing the original sending it to the output stream (i.e. Response.ContentType = "image/jpeg"; Response.BinaryWrite(imageContent) ), or best to actually save the thumbnails to disk. Would this method result in a much slower...
1
1259
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 changes dynamically to reflect the thumbnail See the live example here. http://www.automart-me.com/eng/automart_car_detail.aspx?vehicle_id=1834 here is my code im using this function in .aspx file <SCRIPT Language="JScript">
0
9628
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10289
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10120
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10061
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8952
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7471
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6722
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5367
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4031
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.