我在w3schools上找到了一个媒体库脚本,但我无法将自己的系统视频上传到脚本并在localhost中播放,我尝试了很多但无法做到这一点请帮我演示:https://w3schools.in/demo/mediagallery/
我只想使用脚本在图库中播放我的系统上的视频,请建议我
uploadmedia.php:
<?php
include("config.php");
/*
|--------------------------------------------------------------------------
| Upload Media
|--------------------------------------------------------------------------
*/
if (isset($_FILES['upload_media'])) {
/* Upload Settings Starts */
ini_set("memory_limit", "99M");
ini_set('post_max_size', '20M');
ini_set('max_execution_time', 600);
define('IMAGE_LARGE_DIR', './images/large/');
define('IMAGE_LARGE_WIDTH', 800);
define('IMAGE_LARGE_HEIGHT', 800);
define('IMAGE_SMALL_DIR', './images/small/');
define('IMAGE_SMALL_SIZE_WIDTH', 200);
define('IMAGE_SMALL_SIZE_HEIGHT', 200);
/* Upload Settings Ends */
/* Validations Starts */
$output['status'] = FALSE;
$allowedImageType = array("image/gif", "image/jpeg", "image/pjpeg", "image/png", "image/x-png", "video/mp4");
if ($_FILES['upload_media']["error"] > 0) {
$output['error'] = "Error in File";
} elseif (!in_array($_FILES['upload_media']["type"], $allowedImageType)) {
$output['error'] = "You can only upload JPG, PNG and GIF file";
} elseif (round($_FILES['upload_media']["size"] / 1024) > 4000096) {
$output['error'] = "You can upload file size up to 4 MB";
} else {
/* Validations Ends */
/* create directory with 777 permission if not exist Starts */
$obj->createDir(IMAGE_LARGE_DIR);
$obj->createDir(IMAGE_SMALL_DIR);
/* create directory with 777 permission if not exist Ends */
/* Upload Settings Starts */
$file = pathinfo($_FILES['upload_media']['name']);
$fileType = $file["extension"];
$desiredExt = 'jpg/mp4';
$fileNameNew = rand(333, 999) . time() . ".$desiredExt";
$path[1] = IMAGE_LARGE_DIR . $fileNameNew;
$path[2] = IMAGE_SMALL_DIR . $fileNameNew;
/* Upload Settings Ends */
/* Upload Starts */
if ($obj->createThumb($_FILES['upload_media']['tmp_name'], $path[1], $fileType, IMAGE_LARGE_WIDTH, IMAGE_LARGE_HEIGHT)) {
if ($obj->createThumb($path[1], $path[2], "$desiredExt", IMAGE_SMALL_SIZE_WIDTH, IMAGE_SMALL_SIZE_HEIGHT, IMAGE_SMALL_SIZE_WIDTH)) {
$output['status'] = TRUE;
$output['image_large'] = $path[1];
$output['image_small'] = $path[2];
$output['image_name_original'] = $file['filename'];
$output['media_GUID'] = rand(1111, 9999) . time();
/* Insert into DB */
$obj->addEdit('media', array('media_GUID' => $output['media_GUID'], 'media_name' => $fileNameNew, 'media_name_original' => $file['filename'], 'media_type' => $_FILES['upload_media']["type"], 'created_date' => date('Y-m-d')));
}
}
/* Upload Ends */
}
/* Return JSON Output */
header('Content-type: application/json');
exit(json_encode($output));
}
/*
|--------------------------------------------------------------------------
| Upload YouTube URL
|--------------------------------------------------------------------------
*/
if (isset($_POST['youtube_id']) && $_POST['youtube_id'] != '') {
/* Insert into DB */
$obj->addEdit('media', array('media_GUID' => rand(1111, 9999) . time(), 'media_name' => $_POST['youtube_id'], 'media_name_original' => $_POST['youtube_id'], 'media_type' => 'YouTube', 'created_date' => date('Y-m-d')));
$output['status'] = TRUE;
$output['youtube_url'] = "http://www.youtube.com/embed/" . $_POST['youtube_id'];
$output['image_small'] = "http://img.youtube.com/vi/" . $_POST['youtube_id'] . "/mqdefault.jpg";
$output['media_name_original'] = '';
$output['media_GUID'] = rand(1111, 9999) . time();
/* Return JSON Output */
header('Content-type: application/json');
exit(json_encode($output));
}
/*
|--------------------------------------------------------------------------
| Rename Media
|--------------------------------------------------------------------------
*/
if (isset($_POST['media_name_original']) && $_POST['media_name_original'] != '') {
/* Update into DB */
$obj->addEdit('media', array('media_name_original' => $_POST['media_name_original']), array('media_GUID' => $_POST['media_GUID']));
}
?>
upload.php的
<?php include("config.php"); ?>
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PHP Media` Gallery Demo</title>
<!-- CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel='stylesheet' href='css/app.css' type='text/css' media='all' />
<link rel='stylesheet' href='css/photobox.css' type='text/css'>
<!-- Font -->
<link href='https://fonts.googleapis.com/css?family=Roboto:300,400,500,700,900' rel='stylesheet' type='text/css'>
<!-- JavaScripts -->
<script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script type='text/javascript' src='js/jquery.photobox.js'></script>
<script src="js/jquery.form.js"></script>
<script src="js/app.js"></script>
</head>
<body>
<div class="container">
<h1>MEDIA GALLERY DEMO, V1.0</h1>
<div class="row">
<hr>
</div>
<!-- Photo Gallery Starts -->
<div class="row gallery">
<?php
$result = $obj->getGalley();
if (!empty($result)) {
foreach ($result AS $row) {
/* IF is Video */
if ($row['media_type'] == 'YouTube') {
$rel = 'video';
$href = "http://www.youtube.com/embed/" . $row['media_name'];
$src_bg = 'http://img.youtube.com/vi/' . $row['media_name'] . '/mqdefault.jpg';
$src = 'http://img.youtube.com/vi/' . $row['media_name'] . '/mqdefault.jpg';
} else {
/* IF is Image */
$rel = '';
$href = "images/large/" . $row['media_name'];
$src_bg = "images/small/" . $row['media_name'];
$src = "images/small/" . $row['media_name'];
}
?>
<div class="col-xs-6 col-md-3 col-lg-2 col-sm-4 gallery-block" data-media_guid="<?php echo $row['media_GUID']; ?>">
<div class="gallery-block-inner">
<a class="photobox_a <?php if ($rel != '') { ?> media-video<?php } ?>" href="<?php echo $href; ?>" rel="<?php echo $rel; ?>" <?php if ($rel != '') { ?> style="background-image:url('images/placeholder.png'), url('<?php echo $src_bg; ?>');"<?php } ?>>
<img src="<?php echo $src; ?>" class="img-responsive">
</a>
</div>
<span class="media-name"><?php echo $row['media_name_original']; ?></span>
</div>
<?php
}
} else {
?>
<p id="gallery-empty">It's Empty</p>
<?php
}
?>
<!-- ProgressBar Starts -->
<div class="col-lg-2 col-md-3 col-xs-6 gallery-block" id="gallery-block-loader">
<div class="gallery-block-inner">
<div id="progressbar">
<div class="bar percent"></div>
</div>
</div>
</div>
<!-- ProgressBar Ends -->
</div>
<!-- Photo Gallery Ends -->
<!-- Upload Media Starts -->
<div class="row">
<hr>
<h3>ADD MORE MEDIA TO COLLECTION</h3>
<div id="upload_button_group">
<a href="javascript:void(0)" class="button" id="upload_button">From My Computer</a>
<a href="javascript:void(0)" class="button button-blue" id="upload_button_URL">From YouTube URL</a>
</div>
<form id="upload_form" name="upload_form" method="post" action="media_upload.php" enctype="multipart/form-data">
<input type="file" id="upload_media" name="upload_media" accept="image/*" style="display:none">
</form>
<form id="upload_form_url" name="upload_form_url" method="post" action="media_upload.php" style="display:none">
<input class="text-field" name="youtube_url" id="youtube_url" type="text" placeholder="Please enter YouTube URL">
<a href="javascript:void(0)" class="button" id="upload_button_URL_save">Save</a>
<a href="javascript:void(0)" class="button button-blue" id="upload_button_URL_cancel">Cancel</a>
</form>
</div>
<!-- Upload Media Ends -->
<br>
</div>
</body>
</html>
的config.php
<?php
class imageGallery {
private $sql, $query;
function __construct() {
/* DB Connection */
$this->sql = new mysqli('localhost', 'root', '', 'mediagallery');
}
/*
|--------------------------------------------------------------------------
| Get Gallery
|--------------------------------------------------------------------------
*/
function getGalley() {
if ($query = $this->sql->query("SELECT * FROM `media` ORDER BY media_id ASC ")) {
$rows = array();
while ($row = $query->fetch_assoc()) {
$rows[] = $row;
}
return $rows;
} else {
return "Error";
}
}
/*
|--------------------------------------------------------------------------
| Insery/Update
|--------------------------------------------------------------------------
*/
function addEdit($table_name, $data_array, $where = array()) {
if ($table_name && is_array($data_array)) {
if (!empty($where)) {
/* Update */
foreach ($data_array AS $key => $val) {
$value[] = $key . '=' . "'" . $this->sql->real_escape_string($val) . "'";
}
foreach ($where AS $key => $val) {
$whr[] = $key . '=' . "'" . $this->sql->real_escape_string($val) . "'";
}
$value = implode($value, ',');
$whr = implode($whr, ',');
$this->sql->query("UPDATE `$table_name` SET $value WHERE $whr");
} else {
/* Insert */
foreach ($data_array AS $key => $val) {
$field[] = $key;
$value[] = "'" . $this->sql->real_escape_string($val) . "'";
}
$field = implode($field, ',');
$value = implode($value, ',');
$this->sql->query("INSERT INTO `$table_name` ($field) VALUES($value) ");
}
}
}
/*
|--------------------------------------------------------------------------
| Create directory if not exists
|--------------------------------------------------------------------------
*/
function createDir($path) {
if (!file_exists($path)) {
$old_mask = umask(0);
mkdir($path, 0777, TRUE);
umask($old_mask);
}
}
/*
|--------------------------------------------------------------------------
| Thumbnail Creation
|--------------------------------------------------------------------------
*/
function createThumb($path1, $path2, $file_type, $new_w, $new_h, $squareSize = '') {
/* Read the source image Starts */
$source_image = FALSE;
if (preg_match("/jpg|JPG|jpeg|JPEG/", $file_type)) {
$source_image = imagecreatefromjpeg($path1);
} elseif (preg_match("/png|PNG/", $file_type)) {
if (!$source_image = @imagecreatefrompng($path1)) {
$source_image = imagecreatefromjpeg($path1);
}
} elseif (preg_match("/gif|GIF/", $file_type)) {
$source_image = imagecreatefromgif($path1);
}
if ($source_image == FALSE) {
$source_image = imagecreatefromjpeg($path1);
}
/* Read the source image Ends */
/* Manage Image Orientation Starts */
$exif = @exif_read_data($path1);
if (!empty($exif['Orientation'])) {
switch ($exif['Orientation']) {
case 8:
$source_image = imagerotate($source_image, 90, 0);
break;
case 3:
$source_image = imagerotate($source_image, 180, 0);
break;
case 6:
$source_image = imagerotate($source_image, -90, 0);
break;
}
}
/* Manage Image Orientation Ends */
$orig_w = imageSX($source_image);
$orig_h = imageSY($source_image);
if ($orig_w < $new_w && $orig_h < $new_h) {
$desired_width = $orig_w;
$desired_height = $orig_h;
} else {
$scale = min($new_w / $orig_w, $new_h / $orig_h);
$desired_width = ceil($scale * $orig_w);
$desired_height = ceil($scale * $orig_h);
}
if ($squareSize != '') {
$desired_width = $squareSize;
$desired_height = $squareSize;
}
/* create a new, "virtual" image */
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
/* For PNG set white background */
$kek = imagecolorallocate($virtual_image, 255, 255, 255);
imagefill($virtual_image, 0, 0, $kek);
if ($squareSize == '') {
/* copy source image at a resized size */
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $orig_w, $orig_h);
} else {
$wm = $orig_w / $desired_width;
$hm = $orig_h / $desired_height;
$h_height = $desired_height / 2;
$w_height = $desired_width / 2;
if ($orig_w > $orig_h) {
$adjusted_width = $orig_w / $hm;
$half_width = $adjusted_width / 2;
$int_width = $half_width - $w_height;
imagecopyresampled($virtual_image, $source_image, -$int_width, 0, 0, 0, $adjusted_width, $squareSize, $orig_w, $orig_h);
} elseif (($orig_w <= $orig_h)) {
$adjusted_height = $orig_h / $wm;
$half_height = $adjusted_height / 2;
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $adjusted_height, $orig_w, $orig_h);
} else {
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $orig_w, $orig_h);
}
}
if (@imagejpeg($virtual_image, $path2, 90)) {
imagedestroy($virtual_image);
imagedestroy($source_image);
return TRUE;
} else {
return FALSE;
}
}
}
/* Create Object */
$obj = new imageGallery();
?>