Jump to content

multiply file upload with thumbnails


rcouser

Recommended Posts

I have been pulling my hair out all day trying to get this working. Could some kindly please help.

 

<form name="update" id="update" method="post" enctype="multipart/form-data" action="">
<p><label for="image">Image 1 (W:532px - H:399px)</label>
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>" />
<input type="file" name="image[]" class="sampleimage" /></p>
<p><label for="image">Image 2 (W:532px - H:399px)</label>
<input type="file" name="image[]" class="sampleimage" /></p>
<p><label for="image">Image 3 (W:532px - H:399px)</label>
<input type="file" name="image[]" class="sampleimage" /></p>
<p><label for="image">Image 4 (W:532px - H:399px)</label>
<input type="file" name="image[]" class="sampleimage" /></p>
<p><label for="image">Image 5 (W:532px - H:399px)</label>
<input type="file" name="image[]" class="sampleimage" /></p>
<p><label for="webstatus">Show On Web</label></p>
<p><input type="submit" name="save" id="save" value="Upload" /></p>
</form>

 

<?php
// define a constant for the maximum upload size to 1MB
  define ('MAX_FILE_SIZE', 1048576);
if (array_key_exists('save', $_POST)) {
  // define constant for upload folder
  define('UPLOAD_DIR', 'C:/upload_test/');
  // convert the maximum size to KB
  $max = number_format(MAX_FILE_SIZE/1024, 1).'KB';
  // create an array of permitted MIME types
  $permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png');
  
  foreach ($_FILES['image']['name'] as $number => $file) {
    // replace any spaces in the filename with underscores
$file = str_replace(' ', '_', $file);
    // begin by assuming the file is unacceptable
    $sizeOK = false;
    $typeOK = false;
  
    // check that file is within the permitted size
    if ($_FILES['image']['size'][$number] > 0 || $_FILES['image']['size'][$number] <= MAX_FILE_SIZE) {
    $sizeOK = true;
}

    // check that file is of an permitted MIME type
    foreach ($permitted as $type) {
      if ($type == $_FILES['image']['type'][$number]) {
        $typeOK = true;
    break;
    }
  }
  
    if ($sizeOK && $typeOK) {
      switch($_FILES['image']['error'][$number]) {
    case 0:
	include('system/core/create_thumb.php');
    break;
    case 3:
	  $missing[] = "Error uploading $file. Please try again.";
	default:
          $missing[] = "System error uploading $file. Contact webmaster.";
    }
      }
    elseif ($_FILES['image']['error'][$number] == 4) {
  $missing[] = 'No file selected';
  }
else {
      $missing[] = "$file cannot be uploaded. Maximum size: 1024KB or 1MB, Acceptable file types: gif, jpg, png.";
  }
}
  }
?>

 

 

create_thumb.php

<?php
  include('system/core/getNextFilename5.php');
  
  // define constants
  define('THUMBS_DIR', 'C:/upload_test/thumbs/');
  define('MAX_WIDTH', 82);
  define('MAX_HEIGHT', 82);
	  
	  
  // process the uploaded image
  if (is_uploaded_file($_FILES['image']['tmp_name'][$number])) {
    $original = $_FILES['image']['tmp_name'][$number];
    // begin by getting the details of the original
    list($width, $height, $type) = getimagesize($original);
// calculate the scaling ratio
    if ($width <= MAX_WIDTH && $height <= MAX_HEIGHT) {
      $ratio = 1;
      }
    elseif ($width > $height) {
      $ratio = MAX_WIDTH/$width;
      }
    else {
      $ratio = MAX_HEIGHT/$height;
      }
// strip the extension off the image filename
$imagetypes = array('/\.gif$/', '/\.jpg$/', '/\.jpeg$/', '/\.png$/');
    $name = preg_replace($imagetypes, '', basename($_FILES['image']['name'][$number]));

// converting file type to a string assigned to $t
switch($type) {
  case 1:
    $t = 'gif';
	break;
  case 2:
    $t = 'jpg';
	break;
  case 3:
    $t = 'png';
  }
  
// get new name for upload file
$newName = getNextFilename5(UPLOAD_DIR, $name, $t);

    // move the temporary file to the upload folder
$moved = move_uploaded_file($original, UPLOAD_DIR.$newName);
if ($moved) {
  $message = "$newName successfully uploaded; ";
  $original = UPLOAD_DIR.$newName;
  }
else {
  $missing = 'Problem uploading '.$_FILES['image']['name'][$number].'; ';
  }

// create an image resource for the original
switch($type) {
      case 1:
        $source = @ imagecreatefromgif($original);
    if (!$source) {
      $missing = 'Cannot process GIF files. Please use JPEG or PNG.';
      }
    break;
      case 2:
        $source = imagecreatefromjpeg($original);
    break;
      case 3:
        $source = imagecreatefrompng($original);
    break;
      default:
        $source = NULL;
    $missing = 'Cannot identify file type.';
      }
// make sure the image resource is OK
if (!$source) {
  $missing = 'Problem copying original';
  }
else {
  // calculate the dimensions of the thumbnail
      $thumb_width = round($width * $ratio);
      $thumb_height = round($height * $ratio);
  // create an image resource for the thumbnail
      $thumb = imagecreatetruecolor($thumb_width, $thumb_height);
  // create the resized copy
  imagecopyresampled($thumb, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);
  // save the resized copy
  switch($type) {
        case 1:
	  $name = basename($newName, '.gif');
	  if (function_exists('imagegif')) {
        $success = imagegif($thumb, THUMBS_DIR.$name.'.gif');
        $thumb_name = $name.'.gif';
	    }
      else {
        $success = imagejpeg($thumb, THUMBS_DIR.$name.'.jpg', 50);
	    $thumb_name = $name.'.jpg';
	    }
      break;
    case 2:
	  $name = basename($newName, '.jpg');
	  $success = imagejpeg($thumb, THUMBS_DIR.$name.'.jpg', 100);
      $thumb_name = $name.'.jpg';
      break;
    case 3:
	  $name = basename($newName, '.png');
	  $success = imagepng($thumb, THUMBS_DIR.$name.'.png');
      $thumb_name = $name.'.png';
    }
	if ($success) {
	  $message .= "$thumb_name created";
	  }
	else {
	  $missing .= 'Problem creating thumbnail';
	  }
  // remove the image resources from memory
  imagedestroy($source);
      imagedestroy($thumb);
  }
}
?>

 

getNextFilename5.php

<?php
function getNextFilename5($dir, $prefix, $type) {
  // run some security checks on the arguments supplied
  if (!is_dir($dir)) return false;
  if (!preg_match('/^[-._a-z0-9]+$/i', $prefix)) return false;
  $permittedTypes = array('txt', 'doc', 'pdf', 'jpg', 'jpeg', 'gif', 'png');
  if (!in_array(strtolower($type), $permittedTypes)) return false;
  
  // if the checks are OK, get an array of the directory contents
  $existing = scandir($dir);
  // create a search pattern for filenames that match the prefix and type
  $pattern = '/^'.$prefix.'(\d+)\.'.$type.'$/i';
  $nums = array();
  // loop through the directory
  // get the numbers from all files that match the pattern 
  foreach ($existing as $file) {
    if (preg_match($pattern, $file, $m)) {
  $nums[] = intval($m[1]);
  }
}
  // find the highest number and increase it by 1
  // if no file yet created, assign it number 1
  $next = $nums ? max($nums)+1 : 1;
  // calculate how many zeros to prefix the number with
  if ($next < 10) {
    $zeros = '00';
}
  elseif ($next < 100) {
    $zeros = '0';
}
  else {
    $zeros = '' ;
}
  // return the next filename in the series
  return "{$prefix}{$zeros}{$next}.{$type}";
  }
?>

 

Any help would be much appreciated

Thank

Link to comment
https://forums.phpfreaks.com/topic/125631-multiply-file-upload-with-thumbnails/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.