By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,216 Members | 1,032 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,216 IT Pros & Developers. It's quick & easy.

Please help me with this function

100+
P: 133
Hi,

I am trying to build a function and from a script but it does not seem to be working. Can anyone see anything wrong with this?

Also i need it to convert the large image to a specific size also, like the thumbnail so i need to put something in there so i can enter a width of the large image and this will be resized also.

Here is the function:

[PHP]function uploadimage($value){


$path_thumbs = "uploads/thumbs";
$path_big = "uploads/big";

//the new width of the resized image.
$img_thumb_width = 150; // in pixcel

$extlimit = "yes"; //Do you want to limit the extensions of files uploaded (yes/no)
//allowed Extensions
$limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");


$file_type = $_FILES['image']['type'];
$file_name = $_FILES['image']['name'];
$file_size = $_FILES['image']['size'];
$file_tmp = $_FILES['image']['tmp_name'];

//check file extension
$ext = strrchr($file_name,'.');
$ext = strtolower($ext);
if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
echo "Wrong file extension. <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
exit();
}

//get the file extension.
$getExt = explode ('.', $file_name);
$file_ext = $getExt[count($getExt)-1];

//create a random file name
$rand_name = md5(time());
$rand_name= rand(0,999999999);
//get the new width variable.
$ThumbWidth = $img_thumb_width;

//keep image type
if($file_size){
if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
$new_img = imagecreatefromjpeg($file_tmp);
}elseif($file_type == "image/x-png" || $file_type == "image/png"){
$new_img = imagecreatefrompng($file_tmp);
}elseif($file_type == "image/gif"){
$new_img = imagecreatefromgif($file_tmp);
}
//list width and height and keep height ratio.
list($width, $height) = getimagesize($file_tmp);
$imgratio=$width/$height;
if ($imgratio>1){
$newwidth = $ThumbWidth;
$newheight = $ThumbWidth/$imgratio;
}else{
$newheight = $ThumbWidth;
$newwidth = $ThumbWidth*$imgratio;
}
imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
//save image
ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext");
ImageDestroy ($resized_img);
ImageDestroy ($new_img);

//upload the big image
move_uploaded_file ($file_tmp, "$path_big/$rand_name.$file_ext");
}

$value = "$rand_name.$file_ext";

return $value;

}[/PHP]

Thanks in advanced.
Adam
Feb 20 '08 #1
Share this Question
Share on Google+
8 Replies


ronverdonk
Expert 2.5K+
P: 4,258
Don't let us guess. What exactly IS working and what IS NOT working? Have you put any echoes in there to see where it goes wrong?

Ronald
Feb 20 '08 #2

100+
P: 133
Sorry about that i thought i put in the problem.

It simply goes to a blank screen when it has been posted.

Where should i put in the echo's?
Feb 20 '08 #3

ronverdonk
Expert 2.5K+
P: 4,258
I can't see it. But how do you call this function from your code?
How are you sure that the problem it is in this function and not in the code leading up to, or executing, the call to this function?

Ronald
Feb 20 '08 #4

100+
P: 133
I just assumed it was the function.

I am calling the function by:

[PHP]$image = uploadimage($_FILES['image']);
$image2 = uploadimage($_FILES['image2']);
$image3 = uploadimage($_FILES['image3']);[/PHP]

Is this correct?
Feb 20 '08 #5

ronverdonk
Expert 2.5K+
P: 4,258
I just assumed it was the function.

I am calling the function by:

[PHP]$image = uploadimage($_FILES['image']);
$image2 = uploadimage($_FILES['image2']);
$image3 = uploadimage($_FILES['image3']);[/PHP]

Is this correct?
What I actually wanted to see is the html code that leads up to the function, i.e. the form where you specify the 'type=file'.

Some remarks:
1. you pass a $_FILES['image'], [ image2'] and ['image3'] array key to the function but the $_FILES you actually upload are always in $_FILES['image'].

2. Function parameter variable $value is not used in the function except another $value in the return. Be aware that the variable $value you pass to the function is not the same variable $value you return from the function.

Ronald
Feb 20 '08 #6

100+
P: 133
This is the form:

Expand|Select|Wrap|Line Numbers
  1. <form action="" method="post" enctype="multipart/form-data">
  2.   <table width="100%" border="0" cellspacing="0" cellpadding="3">
  3.     <tr>
  4.       <td><input name="image" type="file" id="image" /></td>
  5.     </tr>
  6.     <tr>
  7.       <td><input name="image2" type="file" id="image2" /></td>
  8.     </tr>
  9.     <tr>
  10.       <td><input name="image3" type="file" id="image3" /></td>
  11.     </tr>
  12.     <tr>
  13.       <td><input type="submit" name="Submit" value="Submit" /></td>
  14.     </tr>
  15.   </table>
  16. </form>
What would i need to change the function to so that it is not specified to just image?
Feb 20 '08 #7

Markus
Expert 5K+
P: 6,050
This is the form:

Expand|Select|Wrap|Line Numbers
  1. <form action="" method="post" enctype="multipart/form-data">
  2.   <table width="100%" border="0" cellspacing="0" cellpadding="3">
  3.     <tr>
  4.       <td><input name="image" type="file" id="image" /></td>
  5.     </tr>
  6.     <tr>
  7.       <td><input name="image2" type="file" id="image2" /></td>
  8.     </tr>
  9.     <tr>
  10.       <td><input name="image3" type="file" id="image3" /></td>
  11.     </tr>
  12.     <tr>
  13.       <td><input type="submit" name="Submit" value="Submit" /></td>
  14.     </tr>
  15.   </table>
  16. </form>
What would i need to change the function to so that it is not specified to just image?
Not sure what you mean..
but you'd have to allow more extensions
Feb 20 '08 #8

100+
P: 133
Not sure what you mean..
but you'd have to allow more extensions
Right i have built this to test out the function.

It is uploading the normal image now but is not uploading the thumbs.

[PHP]<?php

function uploadimage($value){

$file_type = $value['type'];
$file_name = $value['name'];
$file_size = $value['size'];
$file_tmp = $value['tmp_name'];

//check file extension
$ext = strrchr($file_name,'.');
$ext = strtolower($ext);


//get the file extension.
$getExt = explode ('.', $file_name);
$file_ext = $getExt[count($getExt)-1];

//create a random file name
$rand_name = md5(time());
$rand_name= rand(0,999999999);
//get the new width variable.
$ThumbWidth = 150;

//keep image type
if($file_size){
if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
$new_img = imagecreatefromjpeg($file_tmp);
}elseif($file_type == "image/x-png" || $file_type == "image/png"){
$new_img = imagecreatefrompng($file_tmp);
}elseif($file_type == "image/gif"){
$new_img = imagecreatefromgif($file_tmp);
}
//list width and height and keep height ratio.
list($width, $height) = getimagesize($file_tmp);
$imgratio=$width/$height;
if ($imgratio>1){
$newwidth = $ThumbWidth;
$newheight = $ThumbWidth/$imgratio;
}else{
$newheight = $ThumbWidth;
$newwidth = $ThumbWidth*$imgratio;
}
imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
//save image
ImageJpeg ($resized_img,"uploads/thumbs/$rand_name.$file_ext");
ImageDestroy ($resized_img);
ImageDestroy ($new_img);

//upload the big image
move_uploaded_file ($file_tmp, "uploads/big/$rand_name.$file_ext");
}

$value = "$rand_name.$file_ext";

return $value;

}


if ($_POST){

$image = uploadimage($_FILES['image']);
$image2 = uploadimage($_FILES['image2']);
$image3 = uploadimage($_FILES['image3']);

print "Image 1: <a href='uploads/big/$image'>$image</a>";
print "<br>";
print "Image 2: <a href='uploads/big/$image3'>$image2</a>";
print "<br>";
print "Image 3: <a href='uploads/big/$image2'>$image3</a>";

}

?>

<form action="" method="post" enctype="multipart/form-data">
<table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td><input name="image" type="file" id="image" /></td>
</tr>
<tr>
<td><input name="image2" type="file" id="image2" /></td>
</tr>
<tr>
<td><input name="image3" type="file" id="image3" /></td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Submit" /></td>
</tr>
</table>
</form>[/PHP]
Feb 20 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.