Jump to content

Cannot find code to reduce image file size on uploads


Recommended Posts

Hi. I am a newbie at php an have been scratching my head to work out how to reduce the file size of multiple image uploads through an HTML form. The following HTML form looks like this: -

 

<table width="500" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form action="multiple_upload_ac.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td><p><strong>Multiple Files Upload</strong></p></td>
</tr>
<tr>
<td>Company
<input type="text" name="company" id="company" size="50">
</tr>
<tr>
<td>Select file 
<input name="ufile[]" type="file" id="ufile[]" size="50" /></td>
</tr>
<tr>
<td>Select file
<input name="ufile[]" type="file" id="ufile[]" size="50" /></td>
</tr>
<tr>
<td>Select file
<input name="ufile[]" type="file" id="ufile[]" size="50" /></td>
</tr>
<tr>
<td>Select file
<input name="ufile[]" type="file" id="ufile[]" size="50" /></td>
</tr>
<tr>
<td>Select file
<input name="ufile[]" type="file" id="ufile[]" size="50" /></td>
</tr>
<tr>
<td>Select file
<input name="ufile[]" type="file" id="ufile[]" size="50" /></td>
</tr>
<tr>
<td>Select file
<input name="ufile[]" type="file" id="ufile[]" size="50" /></td>
</tr>
<tr>
<td>Select file
<input name="ufile[]" type="file" id="ufile[]" size="50" /></td>
</tr>
<tr>
<td>Select file
<input name="ufile[]" type="file" id="ufile[]" size="50" /></td>
</tr>
<tr>
<td>Select file
<input name="ufile[]" type="file" id="ufile[]" size="50" /></td>
</tr>
<tr>
<td>Select file
<input name="ufile[]" type="file" id="ufile[]" size="50" /></td>
</tr>
<tr>
<td>Select file
<input name="ufile[]" type="file" id="ufile[]" size="50" /></td>
</tr>
<tr>
<td align="center"><input type="submit" name="Submit" value="Upload" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>

 

My PHP script so far is successfully creating a new directory, saving images in to that new directory and outputting the results to the user in their browsers for every image which has finished uploading. I have tried some code from this site and others to reduce the file size of each image uploaded but have had no luck to get it to work. My script named "multiple_upload_ac.php" is as follows: -

 

<?php
//get company name from html form
$dirName = $_POST["company"];

//make directory of the company name
mkdir($dirName, 0777);

//for each image uploaded, move image from temp directory to created company name directory
foreach ($_FILES["ufile"]["error"] as $key => $error) 
{
    	if ($error == UPLOAD_ERR_OK) 
	{
        	$tmp_name = $_FILES["ufile"]["tmp_name"][$key];//temporary image uploaded
        	$name = $_FILES["ufile"]["name"][$key];//prefix file name with "small"
		//$smallPic = imagejpeg($name, null, 25)
    	$path = move_uploaded_file($tmp_name, "$dirName/$name");//move temporary file to named directory. Temporary image automatically destroyed

		//for each file, show successful upload, file size and show thumbnail of image
		echo "File $name successfully uploaded"."<br>";
		echo "Size of file is ". filesize("$dirName/$name"). " bytes". "<br>"; 
		echo "<img src=\"$dirName/$name\" width=\"150\" height=\"150\">". "<br><br>";
	}
}
?>

 

I apologise if this thread has been posted before and appreciate any help, links to tutorials or any other sites that can help me develop my skills further. Thanking everyone in advance.

First of all, your upload is wrong

 

http://www.w3schools.com/PHP/php_file_upload.asp

 

Also, you're not resizing the image at all

 

http://fr2.php.net/manual/en/function.imagecopyresized.php

 

And once it's resized, don't forget to save it

 

http://au2.php.net/function.imagejpeg

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.