473,788 Members | 2,856 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Please help me with this function

133 New Member
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($va lue){


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

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

$extlimit = "yes"; //Do you want to limit the extensions of files uploaded (yes/no)
//allowed Extensions
$limitedext = array(".gif",". jpg",".png",".j peg",".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_n ame,'.');
$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,99999999 9);
//get the new width variable.
$ThumbWidth = $img_thumb_widt h;

//keep image type
if($file_size){
if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
$new_img = imagecreatefrom jpeg($file_tmp) ;
}elseif($file_t ype == "image/x-png" || $file_type == "image/png"){
$new_img = imagecreatefrom png($file_tmp);
}elseif($file_t ype == "image/gif"){
$new_img = imagecreatefrom gif($file_tmp);
}
//list width and height and keep height ratio.
list($width, $height) = getimagesize($f ile_tmp);
$imgratio=$widt h/$height;
if ($imgratio>1){
$newwidth = $ThumbWidth;
$newheight = $ThumbWidth/$imgratio;
}else{
$newheight = $ThumbWidth;
$newwidth = $ThumbWidth*$im gratio;
}
imagecopyresize d($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
//save image
ImageJpeg ($resized_img," $path_thumbs/$rand_name.$fil e_ext");
ImageDestroy ($resized_img);
ImageDestroy ($new_img);

//upload the big image
move_uploaded_f ile ($file_tmp, "$path_big/$rand_name.$fil e_ext");
}

$value = "$rand_name.$fi le_ext";

return $value;

}[/PHP]

Thanks in advanced.
Adam
Feb 20 '08 #1
8 1274
ronverdonk
4,258 Recognized Expert Specialist
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 New Member
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 Recognized Expert Specialist
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 New Member
I just assumed it was the function.

I am calling the function by:

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

Is this correct?
Feb 20 '08 #5
ronverdonk
4,258 Recognized Expert Specialist
I just assumed it was the function.

I am calling the function by:

[PHP]$image = uploadimage($_F ILES['image']);
$image2 = uploadimage($_F ILES['image2']);
$image3 = uploadimage($_F ILES['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 New Member
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 Recognized Expert Expert
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 New Member
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($va lue){

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

//check file extension
$ext = strrchr($file_n ame,'.');
$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,99999999 9);
//get the new width variable.
$ThumbWidth = 150;

//keep image type
if($file_size){
if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
$new_img = imagecreatefrom jpeg($file_tmp) ;
}elseif($file_t ype == "image/x-png" || $file_type == "image/png"){
$new_img = imagecreatefrom png($file_tmp);
}elseif($file_t ype == "image/gif"){
$new_img = imagecreatefrom gif($file_tmp);
}
//list width and height and keep height ratio.
list($width, $height) = getimagesize($f ile_tmp);
$imgratio=$widt h/$height;
if ($imgratio>1){
$newwidth = $ThumbWidth;
$newheight = $ThumbWidth/$imgratio;
}else{
$newheight = $ThumbWidth;
$newwidth = $ThumbWidth*$im gratio;
}
imagecopyresize d($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
//save image
ImageJpeg ($resized_img," uploads/thumbs/$rand_name.$fil e_ext");
ImageDestroy ($resized_img);
ImageDestroy ($new_img);

//upload the big image
move_uploaded_f ile ($file_tmp, "uploads/big/$rand_name.$fil e_ext");
}

$value = "$rand_name.$fi le_ext";

return $value;

}


if ($_POST){

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

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

}

?>

<form action="" method="post" enctype="multip art/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
4555
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 go to great lengths to restructure my code just to look concise and make it more manageable. When I say this, I'm also referring to the way I write my functions. It seems to me sometimes that I shouldn't have many void functions accepting...
6
1804
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" valign="top"><form name="booknow"><select
5
2294
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 to either a) generate numbers b) Prompts a user to locate a web page c) go to previous page in history list d) Loads next page in history list e) Promps the user for a URL and loads the web page in a new window f) and Re-Sizes the window. ...
2
2381
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 http-equiv="Content-Type" content="text/html;
7
3615
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> </head> <style type="text/css">
22
3279
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 seats as 0 and the flight no. is also repeating. If any can tell why is this please help me. #include<stdio.h> #include<ctype.h> #include<conio.h>
6
3328
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, EmpName,DeptID,DateOfJoin, Sal, Addr) Finance (EmpID, Sal) Club (Clubname, EmpID, Fee, DateOfJoin) Leave (EmpID, Date) Department (DeptID, DeptName, NoOfEmployees)...
1
1849
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 have a price value based on a quantity price, so for example 1-10 qantity equals $30, 20-30 quantity equals $40 in the “Total” field). The rest of the fields in the following code work as intended whereas they just add based on the price to the right....
2
1931
by: Unpopular | last post by:
void directory::modification()//??????????? { clrscr(); cout<< "\n\t @@@@@@ @@@@@ @@@@@ @@@@@@ @@@@@ @ @ @@@@@@ "; cout<< "\n\t=====@ @ @ @ @ @ @@ @ @ ====="; cout<< "\n\t=====@@@@@@ @ @ @ @ @ @ @ @ @ @@@ =====";
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10373
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10118
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7519
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5403
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2897
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.