MRMAN
01-13-2006, 01:53 PM
Hello,
I have create a sinple image upload function. This funciton will upload a image that the user will select from a form. Then, being an image, it will resize it to the correct size. I have made it so it works with .jpg .gif and .png
At the moment the usr has to select a large image and a thumbnail image the function then changes the two imges to the correct sizes.
This way they can have two different imges for each item. i.e on the thumb nail they can have a close up of a pattern and a large image then can have the overall object.
What i want is that if the user doens't select a thumbnail then the large image is used and is resized to make the large image and the thumb nail.
This is where i am getting a problem. I have tried checking to see if smallimage[name] is blank then setting smallimage = largeimage. but it doesn't work.
I know that the post method on the form uploads the file so i thought that would work.
if($action == "sendtoserver")
{
$filedir = './images/big/'; // the directory for the original image
$thumbdir = './images/thumb/'; // the directory for the thumbnail image
for($i = 1; $i<=2;$i++)
{
$fileinfobig = $_FILES["file".$i."_big"];
$fileinfothumb = $_FILES["file".$i."_thumb"];
if($fileinfobig["name"])
{
if($fileinfothumb["name"] == "")
{
$fileinfothumb = fileinfobig;
}
$big_image_width=150;
$big_image_height=300;
$thumb_image_height=200;
$thumb_image_width=100;
$prod_img = $filedir.$fileinfobig["name"];
$prod_img_thumb = $thumbdir.$fileinfothumb["name"];
$ratio_height = 0;
$ratio_width = 0;
list($width_orig, $height_orig) = getimagesize($fileinfobig['tmp_name']);
if($width_orig > $big_image_width)
{
$ratio_width = $width_orig / $big_image_width;
}
if($height_orig > $big_image_width)
{
$ratio_height = $height_orig / $big_image_height;
}
if($ratio_height > $ratio_width)
{
$width = ($width_orig / $ratio_height);
$height = $big_image_height;
}
else if($ratio_height < $ratio_width)
{
$width = $big_image_width;
$height = ($height_orig / $ratio_width);
}
else
{
$width =($width_orig / $ratio_width);
$height =($height_orig / $ratio_width);
}
resize($fileinfobig, $prod_img, "big", $width, $height, $width_orig, $height_orig);
}
}
}
function resize($fileinfo, $image_path, $type, $width, $height, $width_orig, $height_orig)
{
print $fileinfo ." _ " . $image_path ." _ " . $type ." _ " . $width ." _ " . $height ." _ " . $width_orig ." _ " . $height_orig;
$image_p = imagecreatetruecolor($width, $height);
$ext = explode('.',$fileinfo["name"]);
$ext = $ext[count($ext)-1];
print $ext;
if($ext=='gif')
{
$image = imagecreatefromgif($fileinfo['tmp_name']);
imagecopyresized($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagegif($image_p, $image_path);
}
}
note: i have taken the jpg and png out to save space.
anyone any ideas.
Thanks
I have create a sinple image upload function. This funciton will upload a image that the user will select from a form. Then, being an image, it will resize it to the correct size. I have made it so it works with .jpg .gif and .png
At the moment the usr has to select a large image and a thumbnail image the function then changes the two imges to the correct sizes.
This way they can have two different imges for each item. i.e on the thumb nail they can have a close up of a pattern and a large image then can have the overall object.
What i want is that if the user doens't select a thumbnail then the large image is used and is resized to make the large image and the thumb nail.
This is where i am getting a problem. I have tried checking to see if smallimage[name] is blank then setting smallimage = largeimage. but it doesn't work.
I know that the post method on the form uploads the file so i thought that would work.
if($action == "sendtoserver")
{
$filedir = './images/big/'; // the directory for the original image
$thumbdir = './images/thumb/'; // the directory for the thumbnail image
for($i = 1; $i<=2;$i++)
{
$fileinfobig = $_FILES["file".$i."_big"];
$fileinfothumb = $_FILES["file".$i."_thumb"];
if($fileinfobig["name"])
{
if($fileinfothumb["name"] == "")
{
$fileinfothumb = fileinfobig;
}
$big_image_width=150;
$big_image_height=300;
$thumb_image_height=200;
$thumb_image_width=100;
$prod_img = $filedir.$fileinfobig["name"];
$prod_img_thumb = $thumbdir.$fileinfothumb["name"];
$ratio_height = 0;
$ratio_width = 0;
list($width_orig, $height_orig) = getimagesize($fileinfobig['tmp_name']);
if($width_orig > $big_image_width)
{
$ratio_width = $width_orig / $big_image_width;
}
if($height_orig > $big_image_width)
{
$ratio_height = $height_orig / $big_image_height;
}
if($ratio_height > $ratio_width)
{
$width = ($width_orig / $ratio_height);
$height = $big_image_height;
}
else if($ratio_height < $ratio_width)
{
$width = $big_image_width;
$height = ($height_orig / $ratio_width);
}
else
{
$width =($width_orig / $ratio_width);
$height =($height_orig / $ratio_width);
}
resize($fileinfobig, $prod_img, "big", $width, $height, $width_orig, $height_orig);
}
}
}
function resize($fileinfo, $image_path, $type, $width, $height, $width_orig, $height_orig)
{
print $fileinfo ." _ " . $image_path ." _ " . $type ." _ " . $width ." _ " . $height ." _ " . $width_orig ." _ " . $height_orig;
$image_p = imagecreatetruecolor($width, $height);
$ext = explode('.',$fileinfo["name"]);
$ext = $ext[count($ext)-1];
print $ext;
if($ext=='gif')
{
$image = imagecreatefromgif($fileinfo['tmp_name']);
imagecopyresized($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagegif($image_p, $image_path);
}
}
note: i have taken the jpg and png out to save space.
anyone any ideas.
Thanks