php/flash image upload + thumbnail generation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jben.net
Forum Newbie
Posts: 5
Joined: Mon Apr 14, 2003 11:55 am

php/flash image upload + thumbnail generation

Post by jben.net »

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.

This bits working fine...


code:
------------------------------------------------------------------------
// read directory script... called using "read_directory.php?dir=thumbs" etc
<?php
if($dir == 'thumbs'){
if ($thumbs = opendir('./thumbs/')) {
while (false !== ($file = readdir($thumbs))) {
if ($file != "." && $file != "..") {
echo "$file\n";
}
}
closedir($thumbs);
}
}
else if ($dir == 'main')
if ($main = opendir('./main/')) {
while (false !== ($file = readdir($main))) {
if ($file != "." && $file != "..") {
echo "$file\n";
}
}
closedir($main);
}
?>
------------------------------------------------------------------------


Should I seperate the functions of the script, so that there's a script for uploading and a seperate script that generates all the thumbnails once the client has finished uploading (just by clicking a button on the upload page?)

Only the client will be able to add pictures (and thinking about it, should be able to remove them as well :-( ) so I don't think there's any need to generate thumbnails everytime the page is called so I'd rather go the 'semi-dynamic' route.

Does this seem like a viable solution to my requirements, or is there a better way ??

Thanks in advance,

Jon
jben.net
Forum Newbie
Posts: 5
Joined: Mon Apr 14, 2003 11:55 am

right, getting somewhere now...but!

Post by jben.net »

Right, I've found this script for resizing images and saving them in different directories, but when I try to incorporate it with the directory list function, I'm getting errors, could someone please take a look...

Code: Select all

<?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 )
&#123;
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&#1111;1]; //source hieght from $img_info
$orig_width = $img_info&#1111;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
&#123;	
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
&#125;
else
&#123;
return(3); //errors occured
&#125;
&#125;

function convertThumbs()&#123;
	if ($main = opendir('./main/')) &#123;
    	while (false !== ($file = readdir($main))) &#123; 
        	if ($file != "." && $file != "..") &#123; 
            	echo "$file\n";
            	image_resize_jpeg( $file, "../thumbs", "75", "75" );
        	&#125; 
    	&#125;
    	closedir($main);

convertThumbs();

?> // errors here, any ideas why ???
thanks again,

Jon
Post Reply