Jump to content

Image upload, Now I want to resize?


StefanRSA

Recommended Posts

Hi, I have written a script to upload images to a server...

I am not to sure what I should do next to re-size the uploaded image....

 

Please help me with my code?

<?php
set_time_limit(60);
$err = false;

if (isset($_FILES['image'])) {
$ftmp = $_FILES['image']['tmp_name'];
$oname = $_FILES['image']['name'];
$div_id = $_POST['div_id'];

$type = @explode('/', $_FILES['image']['type']);
$type = isset($type[1]) ? $type[1] : '';

$type = ($type != 'pjpeg') ? $type : 'jpeg';

$img_types = array('jpg', 'jpeg', 'gif', 'png');



if (in_array($type, $img_types)) {
	$file_temp_name = substr(md5(time() . $div_id), 0, 14) . 'n' . '.' . $type;
///////////////////////

///////////////////////		
	$fname = "photos/temp/" . $file_temp_name;
	$afname = "photos/temp/" . $file_temp_name;

	if (move_uploaded_file($ftmp, $fname)){
?>
<html>
<head>
	<script language="javascript">
		window.parent.setUploadedImage('<?=$afname?>', '<?=$file_temp_name?>', '<?=$div_id?>');
	</script>
</head>
</html>
<?php
		exit();
	}	
}
else {
	$err = true;
}
}
?>
<html>
<head>
    	<style type="text/css">
		body {
			margin: 0px;
			padding: 0px;
			background-color: #FFF;
			color:#000;
		}
	</style>
</head>
<body>
	<?php
		if ($err) {
	?>
		<script language="javascript">
			window.parent.uploadError('<?=$div_id?>', '<?=$oname?>');
		</script>
	<?php
		}
	?>
        <div>
	<form name="iform" action="" method="post" enctype="multipart/form-data">
	<?
		$hostname = 'localhost';
$username = 'root';
$password = '';
$database = 'photo';
		$link = mysql_connect($hostname, $username, $password);
  mysql_select_db($database, $link);
	$result = mysql_query("SELECT * FROM ad_image");
    $num_rows = mysql_num_rows($result);
    if ($num_rows <=3){
    ?>
		<input id="file" type="file" name="image" onChange="window.parent.upload(this);" /><br>
            <span style="font-size:11px; color:#666666;">only gif, png, jpg files.</span>
		<input type="hidden" value="" name="div_id" />
		<?
      }
echo $num_rows;
      ?>
	</form>
        </div>
</body>
</html>

Link to comment
Share on other sites

There are literally a million image resize tutorials/scripts out there. Do you seriously think you're the first person to ever ask about resizing images? We aren't here to sift through your code, write your code for you, or re-invent the wheel.  Google for image resize scripts/tutorials.  Pick any one of the millions out there.  If you have a specific problem, feel free to post it here.

Link to comment
Share on other sites

I think you guys are really rude... I am trying my best to do this all myself and was under the impression that if I need help, I will get it here... I did not ask for anybody to re-write my code or do it for me... I am just not to sure where in my script do I have to implement the re-size section...

 

Will try another forum.....

Link to comment
Share on other sites

I'm amazed you think I'm rude since I answered your orginal post as best I could then gave you a reason for why I put my answer and told you you need to be more specific. That's not rude, it's just what needs to be done

 

Anyway, regarding your question, you need it just after here

      if (move_uploaded_file($ftmp, $fname)){

Link to comment
Share on other sites

Thanks Jay...

 

I am sorry for being so upfront... I am so frustrated with the code as I just don't get it to do what I want it to do. I am new to php...

 

Anyway... I was actually thinking it should be before the line you pointed out! I will try again.

May I print my code on here again if I don't get it right?

 

Link to comment
Share on other sites

Thanks Jay...

 

I am sorry for being so upfront... I am so frustrated with the code as I just don't get it to do what I want it to do. I am new to php...

 

Anyway... I was actually thinking it should be before the line you pointed out! I will try again.

May I print my code on here again if I don't get it right?

 

ok. this script could and can be consolidated but i wrote it this way because it is easier to edit individual parts for the desired application and transplanting script is less time consuming then writing one for each site. this particular application is used for an image gallery with thumbnail views. after you have taken the time to pick apart and research the parts you do not understand you will by then understand how to integrate this into your script.

if you try to cut and paste this it will not work. you need to edit the required conditions, file path, desired sizes, and header pref. but if you can handles that then this script should help you. it requires the gd lib on your server and will resize without squishing or squashing the image(keeps proportions). the or statement for jpg and JPG is because digital cameras use the JPG instead of jpg and its just easier to add this then to require clients or users to rename the file ext, that is even if they knew how to.

if (isset($_SESSION['user'])) 
{   
if (isset($_POST['submitimage']))
{ 
if (isset ($_FILES['new_image']))
{
if (!empty($_FILES['new_image']['name']))
{ 
$imagename = $_FILES['new_image']['name'];
$charref = substr($imagename, 0, strrpos($imagename, '.')); 
if(ctype_alnum($charref)) 
{
if (($pos = strrpos($imagename, ".")) === FALSE) { echo  "<p class=\"p7\">Fail</p><br />"; exit; }
else {  $extension = substr($imagename, $pos + 1); }
if ( $extension == "jpg" || $extension == "JPG" || $extension == "gif" || $extension == "png"   ) 
{ 
$source = $_FILES['new_image']['tmp_name'];
$target = "images/gallery/$imagename";
move_uploaded_file($source, $target);
$file = "images/gallery/$imagename"; 
$save = "images/gallery/$imagename";
list($width, $height) = getimagesize($file) ; 
if ($width > "300" )
{ 
$modwidth = 300; 
}
else
{
$modwidth = $width ;
} 
$diff = $width / $modwidth;
$modheight = $height / $diff; 
$tn = imagecreatetruecolor($modwidth, $modheight) ; 
if( $extension == "jpg"  || $extension == "JPG" ) { $image = imagecreatefromjpeg ($file); }
if( $extension == "gif" ) { $image = imagecreatefromgif ($file);  }
if( $extension == "png" ) { $image = imagecreatefrompng ($file);  } 
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
if( $extension == "jpg" || $extension == "JPG" ) { imagejpeg($tn, $save, 100) ;  }
if( $extension == "gif" ) { imagegif($tn, $save, 100) ;  }
if( $extension == "png" ) { imagepng($tn, $save, 9) ;  }
$save = "images/gallery/thumbs/$imagename";
list($width, $height) = getimagesize($file) ; 
if ($width > "100" )
{ 
$modwidth = 100; 
}
else
{
$modwidth = $width ;
}  
$diff = $width / $modwidth;
$modheight = $height / $diff; 
$tn = imagecreatetruecolor($modwidth, $modheight) ; 
if( $extension == "jpg" || $extension == "JPG" ) { $image = imagecreatefromjpeg ($file); }
if( $extension == "gif" ) { $image = imagecreatefromgif ($file);  }
if( $extension == "png" ) { $image = imagecreatefrompng ($file);  } 
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
if( $extension == "jpg"  || $extension == "JPG" ) { imagejpeg($tn, $save, 100) ;  }
if( $extension == "gif" ) { imagegif($tn, $save, 100) ;  }
if( $extension == "png" ) { imagepng($tn, $save, 9) ;  }
header("Location: http://#"); exit;
} 
else 
{
echo "Invalid or empty File <br>Valid file types .jpg .gif .png<br />"; exit; 
} 
}
else 
{ 
echo "Image name may have alphanumeric characters only and no spaces $charref"; exit;
}
}
else
{ 
echo "You did not select a file to upload<br />"; exit;
}
}
}
}

Link to comment
Share on other sites

Thanks Everybody!

 

I am almost done with my script and think its a good idea to paste the whole script as I feel this is a general problem with image upload without refresh on a form for newbies like me.

For the php guru's... Thanks for the help. Comment would really be appreciated on my complete script, that I will paste a bit later today.

 

Thanks!

Stefan

Link to comment
Share on other sites

thank you both. i will put this to use right now lol.

 

statement for jpg and JPG is because digital cameras use the JPG instead of jpg and its just easier to add this then to require clients or users to rename the file ext,

 

Coughs strtolower

 

OMG WUT SORCERY IS THIS???!?!?!?ONE11!!?!?!

Link to comment
Share on other sites

I am done with the script and it works 100%. This code is built out of lost of examples found on google.com by a newbie(Me...  ::) ! The only problem I do have, at this stage is that when an image is marked for deletion, I need to click on the submit button do delete the image. Can anybody that have time to work through my code, tell me what I should do, to delete the image in the background without the need to hit submit?

 

Any comments will also be appreciated to streamline my code and even to point out errors...

The full script is zipped and attached.

 

[attachment deleted by admin]

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.