Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6617015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:39:12+00:00 2026-05-25T20:39:12+00:00

I’ve got a script that uploads files to the server as well as adds

  • 0

I’ve got a script that uploads files to the server as well as adds the filename to a database, but what I’d like to do it restrict the maximum dimensions of the image before uploading. So if I upload an image that is 1000 x 500 it will be restricted but still keep it’s dimensions and will be changed to 200 x 100, but an image that is 300 x 300 will be restricted to 200 x 200

    <?php 

     //This is the directory where images will be saved 
     $target = "uploads/"; 
     $target = $target . basename( $_FILES['photo']['name']); 

     //This gets all the other information from the form 
     $name=$_POST['name']; 
     $pic=($_FILES['photo']['name']); 

     // Connects to your Database 
     mysql_connect("hostname", "username", "password") or die(mysql_error()) ; 
     mysql_select_db("database") or die(mysql_error()) ; 

     //Writes the information to the database 
     mysql_query("INSERT INTO `table` (name, photo) VALUES ('$name','$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"; 
     } 
     else { 

     //Gives and error if its not 
     echo "Sorry, there was a problem uploading your file."; 
     } 
     ?> 

Thanks for your help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:39:13+00:00Added an answer on May 25, 2026 at 8:39 pm

    To my knowledge, you can’t resize the image before uploading it. (I could be wrong!) However, when you upload the image it goes into a temporary file. You can resize the temporary image and copy the resized image to its final destination.

    Since (it seems) you want to keep the width constant, you don’t really need to do a lot of ratio tests.

    Update:

    You should be able to simply use this in place of your original code. Most of it is unchanged.

    <?php
    
    // resizes an image to fit a given width in pixels.
    // works with BMP, PNG, JPEG, and GIF
    // $file is overwritten
    function fit_image_file_to_width($file, $w, $mime = 'image/jpeg') {
        list($width, $height) = getimagesize($file);
        $newwidth = $w;
        $newheight = $w * $height / $width;
        
        switch ($mime) {
            case 'image/jpeg':
                $src = imagecreatefromjpeg($file);
                break;
            case 'image/png';
                $src = imagecreatefrompng($file);
                break;
            case 'image/bmp';
                $src = imagecreatefromwbmp($file);
                break;
            case 'image/gif';
                $src = imagecreatefromgif($file);
                break;
        }
        
        $dst = imagecreatetruecolor($newwidth, $newheight);
        imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        
        switch ($mime) {
            case 'image/jpeg':
                imagejpeg($dst, $file);
                break;
            case 'image/png';
                imagealphablending($dst, false);
                imagesavealpha($dst, true);
                imagepng($dst, $file);
                break;
            case 'image/bmp';
                imagewbmp($dst, $file);
                break;
            case 'image/gif';
                imagegif($dst, $file);
                break;
        }
        
        imagedestroy($dst);
    }
    
    // init file vars
    $pic  = $_FILES['photo']['name'];
    $target = 'uploads/' . basename( $_FILES['photo']['name']);
    $temp_name = $_FILES['photo']['tmp_name'];
    $type = $_FILES["photo"]["type"];
    
    // Connects to your Database 
    mysql_connect("hostname", "username", "password") or die(mysql_error()) ; 
    mysql_select_db("database") or die(mysql_error()) ; 
    
    // get form data
    $name = mysql_real_escape_string(isset($_POST['name']) ? $_POST['name'] : 'No name');
    
    //Writes the information to the database 
    mysql_query("INSERT INTO `table` (name, photo) VALUES ('$name','$pic')") ; 
    
    // resize the image in the tmp directorys
    fit_image_file_to_width($temp_name, 200, $type);
    
    //Writes the photo to the server
    if(move_uploaded_file($temp_name, $target)) {
    
        //Tells you if its all ok 
        echo "The file ". basename( $_FILES['photo']['name'] ). " has been uploaded"; 
    
    } else {
    
        //Gives and error if its not 
        echo "Sorry, there was a problem uploading your file."; 
    
    }
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I would like to count the length of a string with PHP. The string
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.