Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

HTML and PHP form - run imagemagick command on multiple images

I currently have a form on a page set up that allows you to upload a file and it will then run an imagemagick command I have created on the file and output it. I would like for this form to be able to handle multiple images (or all images in a local directory).

I currently have my form.php:

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
  <title>Image Framer </title>
</head>
<body>
  <?php
    if (isset($_SESSION['message']) && $_SESSION['message'])
    {
      printf('<p><b>%s</b></p>', $_SESSION['message']);
      unset($_SESSION['message']);
    }

    if (isset($_SESSION['final_file']) && $_SESSION['final_file'])
    {
      // printf('<b>%s</b>', $_SESSION['source_image']);
      // printf('<img src=%s />', $_SESSION['source_image']);
      // printf('<b>%s</b>', $_SESSION['final_file']);
      printf('<img src=%s />', $_SESSION['final_file']);
      echo "<br />";
      unset($_SESSION['source_image']);
      unset($_SESSION['final_file']);
    }
  ?>
  <form method="POST" action="process.php" enctype="multipart/form-data">
    <br />
    <div>
      <span>Upload an image to frame:</span>
      <input type="file" name="uploadedFile" />
    </div>

    <input type="submit" name="uploadBtn" value="Upload" />
  </form>
</body>
</html>

...my process.php:

<?php
session_start();

require_once 'process-lib.php';
$imageProcessor = new ImageProcessor();

$message = '';
if (isset($_POST['uploadBtn']) && $_POST['uploadBtn'] == 'Upload')
{
  if (isset($_FILES['uploadedFile']) && $_FILES['uploadedFile']['error'] === UPLOAD_ERR_OK)
  {
    // get details of the uploaded file
    $fileTmpPath = $_FILES['uploadedFile']['tmp_name'];
    $fileName = $_FILES['uploadedFile']['name'];
    $fileSize = $_FILES['uploadedFile']['size'];
    $fileType = $_FILES['uploadedFile']['type'];
    $fileNameCmps = explode(".", $fileName);
    $fileExtension = strtolower(end($fileNameCmps));

    // sanitize file-name
    $newFileName = md5(time() . $fileName) . '.' . $fileExtension;

    // check if file has one of the following extensions
    $allowedfileExtensions = array('jpg', 'gif', 'png', 'jpeg');

    if (in_array($fileExtension, $allowedfileExtensions))
    {
      // directory in which the uploaded file will be moved
      $uploadFileDir = './uploaded_files/';
      $dest_path = $uploadFileDir . $newFileName;

      if(move_uploaded_file($fileTmpPath, $dest_path))
      {
        $message ='File is successfully uploaded.';
        $imageProcessor->convert($dest_path);
      }
      else
      {
        $message = 'There was some error moving the file to upload directory. Please make sure the upload directory is writable by web server.';
      }
    }
    else
    {
      $message = 'Upload failed. Allowed file types: ' . implode(',', $allowedfileExtensions);
    }
  }
  else
  {
    $message = 'There is some error in the file upload. Please check the following error.<br>';
    $message .= 'Error:' . $_FILES['uploadedFile']['error'];
  }
}
$_SESSION['message'] = $message;
header("Location: form.php");

and finally my process-lib.php (containing an imageprocessor class):

<?php

class ImageProcessor {
  private $upload_dir;
  private $main_file;
  private $temp_background_file;
  private $background_file;
  private $final_file;

  public function __construct () {
    $this->upload_dir = 'uploaded_files';
    $this->main_file = $this->upload_dir . '/main.png';
    $this->temp_background_file = $this->upload_dir . '/temp_background.png';
    $this->background_file = $this->upload_dir . '/background.png';
    $this->final_file = $this->upload_dir . '/final.png';
  }

  public function convert ($source_image) {
    $_SESSION['source_image'] = $source_image;
    $_SESSION['final_file'] = $this->final_file;
    $exec = "convert " . $source_image . " -resize '500x500>' -write mpr:img0 
    -set option:distort:viewport '%[fx:max(w,h)]x%[fx:max(w,h)]' 
    -distort SRT '%[fx:(w>h)*(w/2)],%[fx:(h>w)*(h/2)] %[fx:max(w/h,h/w)] 0' 
    -fill white -colorize 70 mpr:img0 -gravity center -composite " . $this->final_file;
    $output = `$exec`;
  }

  public function logger ($message) {
    file_put_contents('uploader.log', $message);
  }
}

I'm new to PHP and most of this code was from a tutorial I found. I am struggling to figure out how to handle multiple images. Ideally it would append '_T' to the name of each input file on output. Whether the multiple images are just uploaded to the upload directory or if they are all zipped up doesnt matter.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Instead of running a command thru your script, I would suggest you to use PHP ImageMagick commands.

A typical command is

$im = new Imagick();

  // Convert image into Imagick
  $im->readimageblob($image);

  // Create thumbnail max of 200x82
  $im->thumbnailImage(200,82,true);

For sure it allows you to handle multiple files on various ImageMagick operations.

You may refer to this link for details:

https://www.php.net/manual/en/book.imagick.php

Side Notes: It is pretty straightforward to install ImageMagick module in both Linux or Windows machines.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...