title: image resizing php using GD
body: <p>I've read that with gd library i can resize an image but i can't find the correct way do it. What i'm trying to find is a function which resizes-uploads the image (to /uploads/resized) and returns the path of the resized image in order to save it in database for later use. I tried using some functions i found but i'm doing something wrong. I've got wamp64 installed in my windows 7 and in my phpinfo() it seems like gd is enabled. It doesn;t has to be gd i could use any function that resizes image. Any hint will be helpfull.</p>
  
  <pre><code>GD Support  enabled
  GD Version  bundled (2.1.0 compatible)
  FreeType Support    enabled
  FreeType Linkage    with freetype
  FreeType Version    2.5.5
  GIF Read Support    enabled
  GIF Create Support  enabled
  JPEG Support    enabled
  libJPEG Version 9 compatible
  PNG Support enabled
  libPNG Version  1.5.18
  WBMP Support    enabled
  XPM Support enabled
  libXpm Version  30411
  XBM Support enabled
  WebP Support    enabled
  </code></pre>
  
  <p>This is is the php file i use to upload my images and store information about them to a database: </p>
  
  <pre><code>&lt;?php
  // just in case, let's turn on the errors
  include_once("config.php");
  error_reporting(E_ALL);
  ini_set('display_errors', 1);
  
  if ($_SERVER['REQUEST_METHOD'] === 'POST') {
      $file = isset($_FILES['fileToUpload']['tmp_name']) ? $_FILES['fileToUpload'] : null;
      // start validation
      try {
          // check if file was NOT provided
          if (!$file ||
              !file_exists($file['tmp_name']) || 
              !is_uploaded_file($file['tmp_name']) || 
              $file['error'] !== UPLOAD_ERR_OK) {
              throw new Exception('No file was given.');
          }
          // check if file is NOT an image
          $size = getimagesize($file['tmp_name']);
          if ($size === false) {
              throw new Exception('File is not an image.');
          }
          // check if file already exists
          $target_dir = 'uploads/';
          $target_file = $target_dir . basename($file['name']);
          if (file_exists($target_file)) {
              throw new Exception('File already exists.');
          }
          // check file size is too large
          if ($file['size'] &gt; 2000000) {
              throw new Exception('File is too large.');
          }
          // check file extension is NOT accepted
          $extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
          if (!in_array($extension, ['jpg', 'png', 'gif'])) {
              throw new Exception('Only JPEG, PNG &amp; GIF files are allowed.');
          }
          // if it passes all checks, try uploading file
          //changing the files name 
          $temp = explode(".", $_FILES["fileToUpload"]["name"]); // edw
          $newfilename = round(microtime(true)) . '.' . end($temp); // edw
          $target_file_renamed = $target_dir . $newfilename;   //edw
          if (!move_uploaded_file($file['tmp_name'], $target_file_renamed)) { 
              throw new Exception('There was an error uploading your file.');
          }
          // if we reach this point, then everything worked out!
          //echo 'The file ' . basename($file['name']) . ' has been uploaded.';
  
  
          //code to resize and save image to uploads/resized and get the path
  
          //code to insert into database with pdo
          session_start();
          try{
          $con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
          $con-&gt;setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
  
          //Code to get photo_category_id
          $sth = $con-&gt;prepare('SELECT photo_category_id FROM photo_categories WHERE photo_category = :parameter');
          $sth-&gt;bindParam(':parameter', $_POST['category'], PDO::PARAM_STR);
          $sth-&gt;execute();
          $idis = $sth-&gt;fetchColumn(); //echo $idis will return the photo_category_id
  
          //Code to update the photos table
          $likes = 0;
          $sql = "INSERT INTO photos(photo_name, photo_text, photo_path, photo_lat, photo_lng, photo_likes, username, photo_category_id)
          VALUES(:name, :text, :path, :lat, :lng, :likes, :username, :category)";
          $stmt = $con-&gt;prepare( $sql );
          $stmt-&gt;bindValue( "name", basename($file['name']), PDO::PARAM_STR );
          $stmt-&gt;bindValue( "text", $_POST['description'], PDO::PARAM_STR );
          $stmt-&gt;bindValue( "path", $target_file_renamed, PDO::PARAM_STR );
          $stmt-&gt;bindValue( "lat", $_POST['lat'], PDO::PARAM_INT );
          $stmt-&gt;bindValue( "lng", $_POST['lng'], PDO::PARAM_INT );
          $stmt-&gt;bindValue( "likes", $likes, PDO::PARAM_INT );
          $stmt-&gt;bindValue( "username", $_SESSION["user"]-&gt;username, PDO::PARAM_STR );
          $stmt-&gt;bindValue( "category", $idis, PDO::PARAM_INT );
          $stmt-&gt;execute();
  
  
  
          echo 'The file has been uploaded!';
          //echo 'The file ' . basename($file['name']) . ' has been uber fully uploaded.';
          }catch (PDOException $e) {
          echo $e-&gt;getMessage();
          }
      } catch (Exception $e) {
          echo $e-&gt;getMessage(); //by doing echo here we get the message in javascript alert(data);...
      }
  }
  ?&gt;
  </code></pre>