Hi,
I been looking for a tutorial online for this but haven't been able to find what I'm after, so I'm hoping a more experienced php coder might be able give me some pointers.
I'm currently developing a flash site, which I need to have lots of dynamic features. One of these is a gallery page. I'm ok with the flash (actionscript) side of things, but I'm a little stumped on the php side.
My initial thoughts are:
client uploads image to server via web. php then saves the original image in a folder called 'main' and a duplicate image, re-sized to 75x75 (or whatever) pixels in a folder called 'thumbs'. Then, I can call a simple php script which reads the directory structure, passes that data (probably as an array) into flash and flash loads the images in sequentially. Once all the thumbnails are loaded, it then calls the same simple php script which reads the directory structure for the folder 'main' and on reciept of that data, flash preceeds to load in the first image.
So far I've found a suitable resize script, which I'm trying to incorporate with a directory listing script, but I'm getting errors...
// script is in a folder called 'images', which has 2 folders, 'main' and 'thumbs'.
<?php
//I wrote a JPEG resize function that will allow you to spefify locations, quality, as well as determine aspect ration. I guess it could be good for thumbnailing...
//resizes a jpg and place in a desired destination
//keeps aspect ratio normal
//directory must have read/write priv
//requires GD1 or 2
/* RETURN KEY
1 = source file not found
2 = successfully completed
3 = errors occured
*/
function image_resize_jpeg( $img_source, $img_dest, $img_quality, $size_limit )
{
if ( !file_exists( $img_source ) )
return (1); //file not found return 1
//load the original image and put its hieght/width in $img_info
$img_info = getimagesize ( $img_source );
//$img_info ARRAY KEY
//1 = hieght
//2 = width
//3 = hight and width string
$orig_height = $img_info[1]; //source hieght from $img_info
$orig_width = $img_info[0]; //source width from $img_info
$jpegQuality = $img_quality; //quality of the JPG
if(($orig_width > $size_limit) || ($orig_height > $size_limit)) //make sure the image isnt already resized
{
if ( $orig_height > $orig_width )
$scaledown = $orig_height;
else
$scaledown = $orig_width;
$newscale = $size_limit / $scaledown; //set the new scale size
//calculate the new aspect ratio
$new_w = abs($orig_width * $newscale);
$new_h = abs($orig_height * $newscale);
//create the blank limited-palette image
$base_image = imageCreate($new_w, $new_h);
//convert and save it to temp.jpg
imagejpeg($base_image, './temp.jpg');
//get the image pointer to the temp jpeg
$image = imageCreateFromJpeg('./temp.jpg');
$dir = dir("./$img_source"); //directory path of the orig image
// get the image pointer to the original image
$imageToResize = imageCreateFromJpeg("./$img_source");
//resize the original image over temp.jpg
// NOTE: of you have GD2, you could also use imageCopyResampled
imageCopyResized($image, $imageToResize, 0, 0, 0, 0, $new_w, $new_h, $orig_width, $orig_height);
//create the resized image
imageJpeg($image, "./$img_dest", $jpegQuality); //image destination
unlink('./temp.jpg'); //del the temp image file
return (2); //completed successfully
}
else
{
return(3); //errors occured
}
}
function convertThumbs(){
if ($main = opendir('./main/')) {
while (false !== ($file = readdir($main))) {
if ($file != "." && $file != "..") {
echo "$file\n";
image_resize_jpeg( $file, "./thumbs", "75", "75" );
}
}
closedir($main);
convertThumbs(); // call function that lists all files in the "main" directory and processes then saves in "thumbs" directory
?>
Can anyone see any problems with this ?????????
Thanks in advance,
Jon