Jump to content

Help adapting this code


supanoob

Recommended Posts

could someone please help me adapt the following code to make it resize the image before uploading?

 

Any help would be appreciated, thanks

<?php
if($_GET['submit'] == 'yes') 
{

	$image = $_FILES['image']['name'];
    
	if ($image) 
	{

		$filename = stripslashes($_FILES['image']['name']);

  		$extension = getExtension($filename);
		  $extension = strtolower($extension);

if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
		{

			echo 'Unknown extension!';
			$errors=1;
		}
		else
		{
$size=filesize($_FILES['image']['tmp_name']);

if ($size > MAX_SIZE*1024)
{
echo 'You have exceeded the size limit! the limit is 500KB';
$errors=1;
}

$rand = rand(0,9999999);
$date = DATE("d.m.y");
$image_name=$date.$id.$rand.'.'.$extension;
$newname="./images/$gender/".$image_name;
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied) 
{
echo 'Copy unsuccessfull!';
$errors=1;
}}}}
?>

 

Link to comment
Share on other sites

You can't resize it before uploading. Until the image has been uploaded the server doesn't have access to it. (OK, technically you could create create a Java application (not JavaScript) to resize on the clients computer before uploading, but that opens up bigger issues).

 

You will want to upload the image and then resize it. There are a ton of resizing scripts available.

Link to comment
Share on other sites

You can't resize it before uploading. Until the image has been uploaded the server doesn't have access to it. (OK, technically you could create create a Java application (not JavaScript) to resize on the clients computer before uploading, but that opens up bigger issues).

 

You will want to upload the image and then resize it. There are a ton of resizing scripts available.

 

What about using a temp image? care to share a script? i cant seem to find any :(

Link to comment
Share on other sites

This script resize an Image into two 60px and 25px. Take a look at $newwidth you have to modify size values.

<?php define ("MAX_SIZE","400");$errors=0;if($_SERVER["REQUEST_METHOD"] == "POST"){        $image =$_FILES["file"]["name"];$uploadedfile = $_FILES['file']['tmp_name'];  if ($image)   {  $filename = stripslashes($_FILES['file']['name']);        $extension = getExtension($filename);  $extension = strtolower($extension);if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))   {echo ' Unknown Image extension ';$errors=1;  }else{   $size=filesize($_FILES['file']['tmp_name']);if ($size > MAX_SIZE*1024){echo "You have exceeded the size limit";$errors=1;}if($extension=="jpg" || $extension=="jpeg" ){$uploadedfile = $_FILES['file']['tmp_name'];$src = imagecreatefromjpeg($uploadedfile);}else if($extension=="png"){$uploadedfile = $_FILES['file']['tmp_name'];$src = imagecreatefrompng($uploadedfile);}else {$src = imagecreatefromgif($uploadedfile);}list($width,$height)=getimagesize($uploadedfile);$newwidth=60;$newheight=($height/$width)*$newwidth;$tmp=imagecreatetruecolor($newwidth,$newheight);$newwidth1=25;$newheight1=($height/$width)*$newwidth1;$tmp1=imagecreatetruecolor($newwidth1,$newheight1);imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1, $width,$height);$filename = "images/". $_FILES['file']['name'];$filename1 = "images/small". $_FILES['file']['name'];imagejpeg($tmp,$filename,100);imagejpeg($tmp1,$filename1,100);imagedestroy($src);imagedestroy($tmp);imagedestroy($tmp1);}}}//If no errors registred, print the success messageif(isset($_POST['Submit']) && !$errors) {   // mysql_query("update SQL statement ");  echo "Image Uploaded Successfully!";}function getExtension($str) {         $i = strrpos($str,".");         if (!$i) { return ""; }          $l = strlen($str) - $i;         $ext = substr($str,$i+1,$l);         return $ext;}?>

 

Link to comment
Share on other sites

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.