473,385 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Please help me with this function

133 100+
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
8 1259
ronverdonk
4,258 Expert 4TB
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
adamjblakey
133 100+
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
4,258 Expert 4TB
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
adamjblakey
133 100+
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
4,258 Expert 4TB
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
adamjblakey
133 100+
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
6,050 Expert 4TB
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
adamjblakey
133 100+
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

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

Similar topics

35
by: wired | last post by:
Hi, I've just taught myself C++, so I haven't learnt much about style or the like from any single source, and I'm quite styleless as a result. But at the same time, I really want nice code and I...
6
by: James Walker | last post by:
Can some one help I get an error of 'checkIndate' is null or not an object can someone please help. I can't work out why Thanks in advance James <form> <td height="24" colspan="7"...
5
by: TrvlOrm | last post by:
Can any one please help me...I am new to JavaScript and I have been struggling with this code for days now and can't figure it out. I would like to get the Buttons to correspond with the action...
2
by: rked | last post by:
I get nameSPAN1 is undefined when I place cursor in comments box.. <%@ LANGUAGE="VBScript" %> <% DIM ipAddress ipAddress=Request.Servervariables("REMOTE_HOST") %> <html> <head> <meta...
7
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
22
by: Amali | last post by:
I'm newdie in c programming. this is my first project in programming. I have to write a program for a airline reservation. this is what i have done yet. but when it runs it shows the number of...
6
by: jenipriya | last post by:
Hi all... its very urgent.. please........i m a beginner in oracle.... Anyone please help me wit dese codes i hv tried... and correct the errors... The table structures i hav Employee (EmpID,...
1
by: theflyingminstrel | last post by:
Hi, I’m having some trouble with a Javascript code, and I was wondering if anyone can help: I am trying to build a price estimator that has multiple fields. I would like the first two fields to...
2
by: Unpopular | last post by:
void directory::modification()//??????????? { clrscr(); cout<< "\n\t @@@@@@ @@@@@ @@@@@ @@@@@@ @@@@@ @ @ @@@@@@ "; cout<< "\n\t=====@ @ @ @ @ @ @@...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.