[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.3

Image Upload Errors using  $_FILE

Asked by birwin in PHP Scripting Language, Images & Photo Software

I have the function below that uploads and resizes image files. I am using the $_FILES superglobal to supply information about the upload. It does not appear to want to supply the ['size'] or ['type'] for anything but jpeg files, although it always supplies the ['name']. Without that information the function throws up errors. The lack of data occurs if I try to upload any .bmp files and wih many, but not all,  .gif and .png files. (note I stripped out the imagecreatewbmp() function since it always failed)

Is there any way to get the $_FILE data to reliably work with user input .gif, png and bmp images?

PS: I know I should be using image_photo[] to upload multiple files, but for historical compatibility, I am handling this as shown.

PPS: Please excuse the lousy formatting, but the EE snippet reproduces differently than my page.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
<$php
function upload_resize_images($xxx,$imageSize, $st_imgfile,$destroy_original)
{
 
$abpath = "/home/goldlogs/public_html/".$st_imgfile; //Absolute path to where images are uploaded. No trailing slash
$sizelim = "no"; 
$size = "2500000"; 
//all image types to upload
$cert[1]="image/pjpeg"; //Jpeg type 1
$cert[2]="image/jpeg"; //Jpeg type 2
$cert[3]="image/gif"; //Gif type
$cert[5]="image/png"; //Png type
$cert[18]="image/jpg";
$cert[19]="image/x-png";
$log .= ($_FILES[$xxx]['name'] == "") ?  "No file selected for upload $xxx<br>":"";
				
if ($_FILES[$xxx]['name'] != "") 
{
		//checks if file exists
		if (file_exists("$abpath/".$_FILES[$xxx]['name']) AND $destroy_original=="no") 
		{ $log .= "File $xxx already existed<br>";
		}
		else 
		{
		//checks if files to big
			if ($sizelim == "yes") 	{
$log .= ($_FILES[$xxx]['size'] > $size) ?  "File $xxx was too big<br>":"";
			}
set_time_limit(90);  // resets timeout to 60 seconds on each send
//Checks if file is an image
if (array_search($_FILES[$xxx]['type'], $cert)) 
{
$imgfilename=str_replace(" ","_",$_FILES[$xxx]['name']);
if($destroy_original=="no"){
@move_uploaded_file($_FILES[$xxx]['tmp_name'],$abpath."/".$imgfilename) or $log .= "Couldn't copy image $xxx to server<br>";
}
 
 $log .=  (file_exists("$abpath/".$imgfilename)) ? "File $xxx was uploaded<br>" : "";
// create thumbnail and resize
$OriginalImage = null;
if ($_FILES[$xxx]['type']== $cert[1] OR $_FILES[$xxx]['type']== $cert[2] OR $_FILES[$xxx]['type']== $cert[18] ) {
$OriginalImage = imagecreatefromjpeg("$abpath/".$imgfilename);
} else if ($_FILES[$xxx]['type']== $cert[5] OR $_FILES[$xxx]['type']== $cert[19]  ) {
  $OriginalImage = imagecreatefrompng("$abpath/".$imgfilename);			
} else if ($_FILES[$xxx]['type']== $cert[3] ) {   $OriginalImage = imagecreatefromgif("$abpath/".$imgfilename);
} else if ($_FILES[$xxx]['type']== $cert[7] OR $_FILES[$xxx]['type']== $cert[8]  ) {
$OriginalImage = imagecreatefromwbmp("$abpath/".$imgfilename);
	} 
	else{
	$image_failure_flag=1;
	}
							
if(!$image_failure_flag){
 $OriginalSize = getimagesize("$abpath/".$imgfilename);
$OriginalWidth = $OriginalSize[0];
$OriginalHeight = $OriginalSize[1];
$scale1 = min($imageSize/$OriginalWidth, $imageSize/$OriginalHeight);
$new_width1= floor($scale1*$OriginalWidth);
# Create a new temporary image
$tmp_img1 = imagecreatetruecolor($new_width1, $new_height1); 
  // change the background to white for transparent images
  $ImgWhite1=imagecolorallocate($tmp_img1,255,255,255);
//   $ImgWhite1=imagecolorallocate($tmp_img1,$img_bkgd1[r],$img_bkgd1[g],$img_bkgd1[b]);
imagefill($tmp_img1, 0,0, $ImgWhite1);// create white background
    # Copy and resize old image into new image
 imagecopyresampled($tmp_img1,  $OriginalImage, 0, 0, 0, 0, ($new_width1+1), ($new_height1+1), $OriginalWidth, $OriginalHeight);
 // NOTE: the -2 makes the holding image slightly smaller than the transferred image to eliminate black borders on the smaller image.
 imagedestroy($OriginalImage);
$ThumbImg1 = $tmp_img1;       
 $tempName=explode(".",$imgfilename);
  $newName1=$tempName[0]."_$imageSize.jpg"; 
  $ThumbFileName1="$abpath/$newName1";
  imagejpeg($ThumbImg1, $ThumbFileName1,75);  
imagedestroy($ThumbImg1); 
if($destroy_original=="yes") {
unlink("$abpath/".$imgfilename);// destroy the originally uploaded file.} 
 
}
else{
$log.=" <br>Image NOT created - contact webmaster <br>";
} else {
$log .= $_FILES[$xxx]['name']." is not a supported file type. Its file type is ".$_FILES[$xxx]['type']."<br>";
}
}
}
 
return $log;
}/// end of function
 
 
$log=upload_resize_images("store_photo1",150,"experience_thumbs","no");
$log.=upload_resize_images("store_photo1",450,"experience_thumbs","yes");
 
$log.=upload_resize_images("store_photo2",150,"experience_thumbs","no");
$log.=upload_resize_images("store_photo2",450,"experience_thumbs","yes");
?>
 
<form name="frm"  action="post.php" method="post" enctype="multipart/form-data">
 
<?
for($i=1;$i<=2;$i++)
{
echo"<input type=\"file\" name=\"store_photo$i\" size=60   id=\"uploadfile1\"><br>
<input style=\"font-size: 12px;\" type=\"text\" name=\"caption$i\" value=\"Enter Caption\" size=\"50\" onclick=\"this.value='';\" /><br /><br />";
}
?>
 
  <input type="submit" name="button" id="button" value="Post Experience" />
[+][-]02/01/09 01:30 PM, ID: 23523120Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02/01/09 01:35 PM, ID: 23523139Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: PHP Scripting Language, Images & Photo Software
Sign Up Now!
Solution Provided By: Ray_Paseur
Participating Experts: 1
Solution Grade: A
 
[+][-]02/02/09 12:12 PM, ID: 23530833Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-89 - Hierarchy / EE_QW_3_20080625