Hi,
There are a number of small glitches in your code (e.g. your trimming of strings is not going right, the $_POST array doesn't contain the file etcetc. THerefore I post part of an upload script I wrote. Hope it helps you. You need to set most of the variables, such as newfilename and output sizes, since they get set in different parts of the file. But this way you also have an -in principle- working resize script.
$original_picture = $file_array['name']; // original filename
$tmp_picture = $file_array['tmp_name']; // Filname in upload folder
$new_picture = $destination; // full path filename
// ****************** Test variables / files for correct input *******************************
if (!file_exists($tmp_picture) ) // Check wether filesource exists
{
return("File doesn't exist in temporary folder on server!");
// The script has died here
}
if(file_exists($new_picture)) // Remove existsing file
{
$delete = @unlink($new_picture);
}
// ***************** Get file details **************************
$size = GetImageSize($tmp_picture);
$aspectRat = (float)($size[1] / $size[0]);
// ***************** Calculate new x/y size, maintaining original aspect ratio
if( ($size[0] <= $new_max_width) && ($size[1] <= $new_max_width) ) // Image < max size
{
copy($tmp_picture, $new_picture);
$newX = $size[0];
$newY = $size[1];
}
else
{
$width_ratio = $size[0] / $new_max_width;
$height_ratio = $size[1] / $new_max_height;
if($width_ratio >= $height_ratio) // width needs to be reduced more
{
$newY = $new_max_width * $aspectRat;
$newX = $new_max_width;
}
else
{
$newY = $new_max_height;
$newX = $new_max_height * (1/$aspectRat);
}
//create the destination image resource.
$destImg = ImageCreateTrueColor($newX, $newY );
//read the source image (original), use correct function depending on format
// Vervangen voor een switch tzt.
if ($size[2] == 1)
{
$sourceImg = ImageCreateFromGIF($tmp_picture);
}
if ($size[2] == 2)
{
$sourceImg = ImageCreateFromJPEG($tmp_picture);
}
else if ($size[2] == 3)
{
$sourceImg = ImageCreateFromPNG($tmp_picture);
}
else
{
return("This file cannot be used. It is not a jpg, gif or png file.<br>");
die(); // This is a double-trap. Return should have ended execution fo the scriopt already
}
// **************************** Create the new output file *******************
imagecopyresampled($destImg, $sourceImg, 0, 0, 0, 0, $newX, $newY, $size[0], $size[1]);
imagejpeg($destImg, $destination);
}
// *************************** Remove the temproary file *********************
if($remove_orig == 1)
{
$deletedfile = @unlink($tmp_picture);
}