php photoupload - resizing Problem

Hi, I build a little Website fully in flash, which as one of many things enables the user to upload photos to the server to a specific direction, they get resized and their names are then changed to a number (thats why I connect to database, the table is chosen by flash, it tells me the number of the next picture) . This works great when I tried it out at my local server, as long as the pictures are not to big in size, or I think if they don’t have to many pixels. As soon as they get bigger, it either takes forever or just doesn’t work (I haven’t waited for more than an hour yet).

I don’t really want people to have a limit on file size though. Any idea where the problem in my code is?

I know its not well written and all, but its my first php script, I usually only do action script.

PHP:
[php]

<?php //connect to db $link = mysql_connect("somehost","user","pw"); mysql_select_db("somedb"); //define variables from POST and db $schirm = $_POST['schirm']; $table = strtolower($schirm)."img"; $query = 'SELECT * FROM '.$table.' ORDER BY id DESC LIMIT 1'; $results = mysql_query($query); while ($line = mysql_fetch_assoc($results)){ $id = $line["id"]+1; }; //get extension and file size as well as name etc. $filenameg = $_FILES['Filedata']['name']; $filename = substr_replace($filenameg,"IMG_".$id,0,strrpos($filenameg,".")); $filetmpname = $_FILES['Filedata']['tmp_name']; $fileType = $_FILES["Filedata"]["type"]; $fileSizeMB = ($_FILES["Filedata"]["size"] / 1024 / 1000); $imagesize = getimagesize($filetmpname); $str = $filenameg; $i = strrpos($str,"."); $l = strlen($str) - $i; $extension = strtolower(substr($str,$i+1,$l)); //change image size if its bigger than 800x600 if($imagesize[0]>800){ if($extension=="jpg" || $extension=="jpeg" ) { $src = imagecreatefromjpeg($filetmpname); } else if($extension=="png") { $src = imagecreatefrompng($filetmpname); } else { $src = imagecreatefromgif($filetmpname); } if($imagesize[0]>($imagesize[1]*4/3)) { $newwidth=800; $newheight=($imagesize[1]/$imagesize[0])*$newwidth; $tmp=imagecreatetruecolor($newwidth,$newheight); imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$imagesize[0],$imagesize[1]); imagejpeg($tmp,"images/".$table."/".$filename,100); imagedestroy($src); imagedestroy($tmp); } else{$newheight=600; $newwidth=($imagesize[0]/$imagesize[1])*$newheight; $tmp=imagecreatetruecolor($newwidth,$newheight); imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$imagesize[0],$imagesize[1]); imagejpeg($tmp,"images/".$table."/".$filename,100); imagedestroy($src); imagedestroy($tmp);}; } else if($imagesize[1]>600){ if($extension=="jpg" || $extension=="jpeg" ) { $src = imagecreatefromjpeg($uploadedfile); } else if($extension=="png") { $src = imagecreatefrompng($uploadedfile); } else { $src = imagecreatefromgif($uploadedfile); } $newheight=600; $newwidth=($imagesize[0]/$imagesize[1])*$newheight; $tmp=imagecreatetruecolor($newwidth,$newheight); imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$imagesize[0],$imagesize[1]); imagejpeg($tmp,"images/".$table."/".$filename,100); imagedestroy($src); imagedestroy($tmp); } else{$move = move_uploaded_file($_FILES['Filedata']['tmp_name'], "images/".$table."/".$filename);}; //enter data into db and close connection $entry = mysql_query("INSERT INTO $table(name,width,height)VALUES('$filename','$newwidth','$newheight')"); mysql_close($link); ?>

[/php]

Thank you already for taking a look at it.

Well, I am not sure about all of your code, but, here is how I upload files and resize them to 800x600…

[php]
$picture = $_FILES[‘ufile’][‘tmp_name’];
list($width, $height, $type, $attr) = getimagesize($picture); //Gets various info on picture…

if (($width > 800) || ($height > 600) {
// Picture is too big, let’s reduce it to 800x600…

if($type == “image/pjpeg” || $type == “image/jpeg”){
$new_img = imagecreatefromjpeg($picture);
}elseif($type == “image/x-png” || $type == “image/png”){
$new_img = imagecreatefrompng($picture);
}elseif($type == “image/gif”){
$new_img = imagecreatefromgif($picture);
}

//function for resize image.
$resized_img = imagecreatetruecolor(800, 600);

//the resizing is going on here!
imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, 800, 600, $width, $height);

//finally, save the image
ImageJpeg ($resized_img,"$path_to_pictures/Name_of_picture.jpg");
ImageDestroy ($resized_img);
ImageDestroy ($new_img);

move_uploaded_file ($file_tmp, “$path_to_pictures/Name_of_picture.jpg”);
[/php]

This code was actually combined from two of my routines, so it may not work as-is.
It is just to show you another way to resize pictures… Also, note that this is a flat 800x600 pix.
You may have to check for WIDTH/HEIGHT ratios so that the pix stays the same. You would do this by setting the height to 600, but, create the width based on the original pixes height/width or whatever…

Good luck, hope this helps…

Sponsor our Newsletter | Privacy Policy | Terms of Service