Image resizing using PHP for uploaded images

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
slim123
Forum Newbie
Posts: 2
Joined: Mon Aug 11, 2008 9:07 am

Image resizing using PHP for uploaded images

Post by slim123 »

Hello All, New User Here.

I got a problem and cant seem to come up with any solutions, appologies if this is in the wrong place.

The code im using below uploads an image to my server and changes the name of that upload to something random, so whoever uploads an image makes it highly unlikly they will overwrite something already in the folder.

However, i cant seem to find any solution to resizing that image to about 600 x 450 pixels, i dont need any thumbnails or any other things, just resize the image to 600 x 450 pixel, this saving space on my server.

The user inputs the details in an html form.

Code: Select all

 
[color=#0000FF]<form enctype="multipart/form-data" action="http://www.mywebspace.co.uk/add2.php" method="POST"> 
Name: 
  <input type="text" name="name"><br> 
E-mail: <input type="text" name = "email"><br> 
Phone: <input type="text" name = "phone"><br> 
Photo: <input type="file" name="photo"><br> 
<input type="submit" value="Add"> 
</form>[/color]
 
Then the PHP code named add2.php is as follows

Code: Select all

 
<?php 
 
$ran = rand () ;
$ran2 = $ran.".";
//This is the directory where images will be saved 
$target = "photopics/";
$target = $target . $ran2 . basename( $_FILES['photo']['name']);
 
//This gets all the other information from the form 
$name=$_POST['name']; 
$email=$_POST['email']; 
$phone=$_POST['phone']; 
$pic= $ran2 . ($_FILES['photo']['name']); 
 
// Connects to your Database 
mysql_connect("serverip", "username", "password") or die(mysql_error()) ; 
mysql_select_db("databasename") or die(mysql_error()) ; 
 
//Writes the information to the database 
mysql_query("INSERT INTO `employees` VALUES ('$name', '$email', '$phone', '$pic')") ; 
 
 
//Writes the photo to the server 
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
{ 
 
//Tells you if its all ok 
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
} 
else { 
 
//Gives and error if its not 
echo "Sorry, there was a problem uploading your file."; 
} 
?>
 
That code works fine in uploading the image and writing the info to the database.

but can someone please help me on resizing that image, would like to keep the above code just dont know where to add the extra code.

Many thanks

Simon
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Image resizing using PHP for uploaded images

Post by onion2k »

Resizing an image to make something 600*450 is no different to making a thumbnail, it's just that the image is bigger. Any thumbnail code will work fine.
slim123
Forum Newbie
Posts: 2
Joined: Mon Aug 11, 2008 9:07 am

Re: Image resizing using PHP for uploaded images

Post by slim123 »

Thanks for quick reply, but ive only just started using PHP and dont really understand what to do just yet. Would you have any code which would resize the uploaded image? and could you please show me where to put this is my code?

Many thanks

Simon
Post Reply