如何应用图像和多图像上传的基于文本的水印

时间:2017-06-09 15:49:27

标签: javascript php watermark

我想在每张图片上添加基于图像和文字的标记。

我正在使用此JavaScript上传多张图片。

<script>
      $(document).ready(function() {
        $(".add").click(function() {
          $('<div><input class="files" name="user_files[]" type="file" ><span class="rem" ><a href="javascript:void(0);" >Remove</span></div><br />').appendTo(".contents");

        });
        $('.contents').on('click', '.rem', function() {
          $(this).parent("div").remove();
        });

      });
    </script>

用于上传多张图片

<div class="form-group">
                  <div class="col-lg-10">
                  <input class="files" name="user_files[]" type="file" >

                  <br />
                  <span><a href="javascript:void(0);" class="add" >Add More</a></span>

                  <div class="contents"></div>
              <div class="height10"></div><br />
              <div><input type="submit" name="sub2" value="Upload Images" /> </div>

这是用于上传和播放的php。插入数据库

            <!-- Image code starts here -->

                <?php

                $strmodelid=$_POST['txtmodelid'];
                $strphotocat=$_POST['rbtnalbum'];


error_reporting(E_ALL & ~E_NOTICE);
@ini_set('post_max_size', '64M');
@ini_set('upload_max_filesize', '64M');

/* * *********************************************** */
// database constants
define('DB_DRIVER', 'mysql');
define('DB_SERVER', 'localhost');
define('DB_SERVER_USERNAME', 'admin');
define('DB_SERVER_PASSWORD', 'admin');
define('DB_DATABASE', 'databasename');

$dboptions = array(
    PDO::ATTR_PERSISTENT => FALSE,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);

try {
  $DB = new PDO(DB_DRIVER . ':host=' . DB_SERVER . ';dbname=' . DB_DATABASE, DB_SERVER_USERNAME, DB_SERVER_PASSWORD, $dboptions);
} catch (Exception $ex) {
  echo $ex->getMessage();
  die;
}
//include"config/conn.php";
if (isset($_POST["sub1"]) || isset($_POST["sub2"])) {
  // include resized library
  require_once('imagecode/php-image-magician/php_image_magician.php');
  $msg = "";
  $valid_image_check = array("image/gif", "image/jpeg", "image/jpg", "image/png", "image/bmp");
  if (count($_FILES["user_files"]) > 0) {
    $folderName = "modelimgs/";

    $sql = "INSERT INTO photogallery (catid,modelid,imgurl) VALUES ('$strphotocat','$strmodelid',:img)";
    $stmt = $DB->prepare($sql);

    for ($i = 0; $i < count($_FILES["user_files"]["name"]); $i++) {

      if ($_FILES["user_files"]["name"][$i] <> "") {

        $image_mime = strtolower(image_type_to_mime_type(exif_imagetype($_FILES["user_files"]["tmp_name"][$i])));
        // if valid image type then upload
        if (in_array($image_mime, $valid_image_check)) {

          $ext = explode("/", strtolower($image_mime));
          $ext = strtolower(end($ext));
          $filename = rand(10000, 990000) . '_' . time() . '.' . $ext;
          $filepath = $folderName . $filename;
            $filetitle = $_POST[txtname];
          if (!move_uploaded_file($_FILES["user_files"]["tmp_name"][$i], $filepath)) {
            $emsg .= "Failed to upload <strong>" . $_FILES["user_files"]["name"][$i] . "</strong>. <br>";
            $counter++;
          } else {
            $smsg .= "<strong>" . $_FILES["user_files"]["name"][$i] . "</strong> uploaded successfully. <br>";

            $magicianObj = new imageLib($filepath);
            $magicianObj->resizeImage(295, 295);
            $magicianObj->saveImage($folderName . 'thumb/' . $filename, 100);

            /*             * ****** insert into database starts ******** */
            try {
              $stmt->bindValue(":img", $filename);
              $stmt->execute();
              $result = $stmt->rowCount();
              if ($result > 0) {
                // file uplaoded successfully.
              } else {
                // failed to insert into database.
              }
            } catch (Exception $ex) {
              $emsg .= "<strong>" . $ex->getMessage() . "</strong>. <br>";
            }
            /*             * ****** insert into database ends ******** */
          }
        } else {
          $emsg .= "<strong>" . $_FILES["user_files"]["name"][$i] . "</strong> not a valid image. <br>";
        }
      }
    }


   // $msg .= (strlen($smsg) > 0) ? successMessage($smsg) : "";
    //$msg .= (strlen($emsg) > 0) ? errorMessage($emsg) : "";
    header("location:photo_add.php");
  } else {
    $msg = errorMessage("You must upload atleast one file");
  }
}
?>
            <!-- image code ends here -->

1 个答案:

答案 0 :(得分:0)

你会做这样的事情:

  // *** Open JPG image
  $magicianObj = new imageLib('racecar.jpg');

  // *** Add watermark to bottom right, 50px from the edges
  $magicianObj -> addWatermark('monkey.png', 'br', 50);

  // *** Save watermarked image as a PNG
  $magicianObj -> saveImage('racecar_small.png');

请参阅:this page