Help Uploading Image, Resize it and Save in Image Folder

images php coding issues or problems here.

Moderators: macek, egami, gesf

New php-forum User
New php-forum User
Posts: 1
Joined: Mon Apr 01, 2013 12:37 pm

Help Uploading Image, Resize it and Save in Image Folder

Postby jillianmariegraphics » Mon Apr 01, 2013 12:42 pm

Hey Guys,

Need some help here.

I am building an image gallery for a client's website and wanted a way for the client to upload their own images from the back end and shows up on the website page for the pictures. I am using color box as the lightbox feature for the photos and on the webpage thumbnails are displayed in rows.

In the backend, I have the upload page and here is my form:

Code: Select all


<form name="form" action="../uploader.php" method="POST"
    <label for="file">Filename:</label>
    <input type="file" name="uploaded_file" id="uploaded_file">
    <input type="hidden" name="MAX_FILE_SIZE" value="10485760" /><br />
    <input type="hidden" name="image_type" value="" /><br />
<input type="submit" name="submit" value="Submit">
<input type="hidden" name="MM_insert" value="form" />

From the admin upload page, the client can upload their image and it will save it into the "image/uploads/" folder. Here is the uploader.php script:

Code: Select all



//Сheck that we have a file
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
  //Check if the file is JPEG image and it's size is less than 1.4MB
  $filename = basename($_FILES['uploaded_file']['name']);
  $ext = substr($filename, strrpos($filename, '.') + 1);
  if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") &&
    ($_FILES["uploaded_file"]["size"] < 10485760))
    //Determine the path to which we want to save this file
      $newname = dirname(__FILE__).'/images/uploads/'.$filename;
      //Check if the file with the same name is already exists on the server
      if (!file_exists($newname)) {
        //Attempt to move the uploaded file to it's new place
        if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
           echo "Upload Complete! ";
        } else {
           echo "Error: A problem occurred during file upload!";
      } else {
         echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";
  } else {
     echo "Error: File too big to upload";
} else {
 echo "Error: No file uploaded";


Now the website page will display all the images in the "images/uploads" folder and will update itself when new photos are uploaded.

Here is the gallery code from the website:

Code: Select all

<div class="gallery">

/* function: returns files from dir */
function get_files($images_dir,$exts = array('jpg')) {
  $files = array();
  if($handle = opendir($images_dir)) {
    while(false !== ($file = readdir($handle))) {
      $extension = strtolower(get_file_extension($file));
      if($extension && in_array($extension,$exts)) {
        $files[] = $file;
  return $files;

/* function: returns a file's extension */
function get_file_extension($file_name) {
  return substr(strrchr($file_name,'.'),1);

/** settings **/
$images_dir = 'images/uploads/';
$images_per_row = 5;

/** generate photo gallery **/
$image_files = get_files($images_dir);
if(count($image_files)) {
  $index = 0;
  foreach($image_files as $index=>$file) {
    echo '<a href="',$images_dir.$file,'" class="photo-link group1" rel="uploads"><img src="timthumb.php?src=',$images_dir.$file,'&h=150&w=150" /></a>';
    if($index % $images_per_row == 0) { echo '<div class="clear"></div>'; }
  echo '<div class="clear"></div>';
else {
  echo '<p>There are no images in this gallery.</p>';



With the help from timthumb.php and timthumb-config.php I am able to produce the thumbnails for the website. Once clicked, brings up the colorbox feature for the image slideshow.

Everything works, except for any images over 1500px do not display their thumbnails so I need to resize the images to be smaller than that - and porportional.
How can I upload a bigger image and resize it to save itself into the "images/uploads/" folder to be under 1500px?

Please look at my codes and let me know where and what code I need to produce the resized image.

Thanks! :)

php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Help Uploading Image, Resize it and Save in Image Folder

Postby johnj » Thu Apr 04, 2013 1:26 am

Please read about built-in php function for resizing a image ... ampled.php

Return to “PHP coding => Images”

Who is online

Users browsing this forum: No registered users and 1 guest