Jump to content

Complex image resizing and uploading


Richzilla

Recommended Posts

Hi All,

 

Back again. This time I'm really stuck. So, I'm currently uploading an image from a forms page along with the information to my server and also adding the filename etc to my sql database. I'm checking that the files are only jpg and gif files and that they are less than 3MB in size. I'd like to now resize this image and upload alsoto the same location as the large image but with the extension _small. e.g - largeimage_small.gif

 

So essentially I'd like to add the large image to the server and also the small resized image (max 150 pixel width). I can find a way of resizing the image by opening it as is below. So far the script for upload the image is working really well. Now I need to have the smaller image version added also.

 

Any help would be greatly apprecaited. You're all such wizards and I'm still a mear novice. Here's the code -

 

[code

<?

$event = $_POST['event'];

$date = @$_POST['date'];

$flyer = @$_POST['uploaded'];

$user="xxxx";

$pass="xxxx";

$host = "xxxx";

$dbase="xxxx";

$table = "events";

mysql_connect($host,$user,$pass);

mysql_select_db($dbase) or die("Unable to select database");

$query = "SELECT * FROM events WHERE event ='$event'";

if ($result = mysql_query($query) or die(mysql_error()) > 0) {

  if (mysql_num_rows($result)) {

    echo "Error : This event is already in the database";

  } else

{

$filename = basename($_FILES['uploaded']['name']);

if((!empty($_FILES["uploaded"])) && ($_FILES['uploaded']['error'] == 0)) {

  $tag = "events";

  $ext = substr($filename, strrpos($filename, '.') + 1);

  $goback = "<a href=\"http://www.mysite.co.uk/add_event.php\">back</a>";

  if (($ext == "jpg") || ($ext == "gif") && ($_FILES["uploaded"]["size"] < 3000000)) {

$newname = dirname(events).'/upload/'.$filename;

      if (!file_exists($newname)) {

        //Attempt to move the uploaded file to it's new place

        if ((move_uploaded_file($_FILES['uploaded']['tmp_name'],$newname))) {

          echo "You have successfully entered the following information into the database - <br> <br> Event : $event <br>Date : $date <br>Flyer : ";

  $query = "INSERT INTO events VALUES ('','$event','$date','','$tag$newname')";

$result = mysql_query($query) or die(mysql_error());

 

// How do I now open the image to resize it???

 

 

    $image = open_image('$tag$newname');

$width = imagesx($image);

$height = imagesy($image);

$new_width = 150;

$new_height = $height * ($new_width/$width);

$image_resized = imagecreatetruecolor($new_width, $new_height);

imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

 

 

//How do I now move the file to my server and also add the filename to my database??

 

 

mysql_close();

        } else {

          echo "Error: A problem occurred during file upload! Please go $goback and try again";

        }

      } else {

        echo "Error: File ".$_FILES["uploaded"]["name"]." already exists. Please rename and go $goback and try again";

      }

  } else {

    echo "Error: Only jpg and gif files under 3MB are accepted for upload. <br> Go $goback and try again";

  }

} else {

$query = "INSERT INTO events VALUES ('','$event','$date','','')";

$result = mysql_query($query) or die(mysql_error());

echo "You have successfully entered the following information into the database - <br> <br> Event : $event <br>Date : $date <br>";

mysql_close();

} }}

 

?>

code]

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.