how to play/display video in a directory with html5 - php

I do not know how to get videos in a folder to play in html or php. I know how to do a single video but that's not what I want. I want to display and play videos that are in a directory folder. Is their a certain way to do this. I tried this code but it doesn't work and I tried other codes. I know in the code below its connected to a database. I have managed to upload videos but can't display them.
none of the solutions given is working but let me ask a simpliar question can I do this to get not just one particular video but all the videos with a path to the folder with videos. I think the code above has a lot of errors and it was the only one i could find so I'm not trusting the code above.

Can you try the following?
<?php
$query = "select * from videos where `id`='" . $id . "'";
$select = mysql_query($query);
while (($row = mysql_fetch_assoc($select)) != null)
{
$video = $row['video'];
print '<video width="320" height="240" controls>' .
'<source src="uploadvideo/' . $video . '" type="video/mp4">' .
'<source src="video/' . $video . '" type="video/ogg" />' .
'Your browser does not support the video tag.' .
'</video>';
}
?>
There are a few tweaks in there, such as using backticks around the column name, inputting a message for if your browser does not support the video tag (taken from here), and handling a null result.
Also, maybe you need to stick a / in front of video/ and uploadvideo/? e.g. if your videos are located in one folder below the document root, such as wwww.mydomain.com/video/vid-name-here.mp4
Another thing, have you removed the file extensions from all the videos, or is the file extension kept in the database for both mp4 and ogg videos? Perhaps you want to attach .mp4 and .ogg accordingly in the source links to be like so:
'<source src="uploadvideo/' . $video . '.mp4" type="video/mp4">' .
'<source src="video/' . $video . '.ogg" type="video/ogg" />' .

<?php
$query = "select * from `videos` where `id`='" . $id . "'";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0){
while ($row = mysql_fetch_array($result))
{
$video = $row['video'];
echo '<video width="320" height="240" controls>' .
'<source src="uploadvideo/' . $video . '" type="video/mp4">' .
'<source src="video/' . $video . '" type="video/ogg" />' .
'Your browser does not support the HTML5 tags.' .
'</video>';
}
}
?>

Related

How to generate thumbnail from PDF upload in laravel

I am trying to generate a thumbnail of the PDF I upload in laravel the thumbnail should be the first page of the PDF. Right now I am manually uploading an image to make the thumbnail like this:
if (request()->has('pdf')) {
$pdfuploaded = request()->file('pdf');
$pdfname = $request->book_name . time() . '.' . $pdfuploaded->getClientOriginalExtension();
$pdfpath = public_path('/uploads/pdf');
$pdfuploaded->move($pdfpath, $pdfname);
$book->book_file = '/uploads/pdf/' . $pdfname;
$pdf = $book->book_file;
}
if (request()->has('cover')) {
$coveruploaded = request()->file('cover');
$covername = $request->book_name . time() . '.' . $coveruploaded->getClientOriginalExtension();
$coverpath = public_path('/uploads/cover');
$coveruploaded->move($coverpath, $covername);
$book->card_image = '/uploads/cover/' . $covername;
}
This can be tedious while entering many data I want to generate thumbnail automatically. I searched many answers but I am not able to find laravel specific. I tried to use ImageMagic and Ghost script but I couldn't find a solution and proper role to implement.
Sorry, can't comment yet!
You can use spatie/pdf-to-image to parse the first page as image when file is uploaded and store it in your storage and save the link in your database.
First you need to have php-imagick and ghostscript installed and configured. For issues with ghostscript installation you can refer this. Then add the package composer require spatie/pdf-to-image.
As per your code sample:
if (request()->has('pdf')) {
$pdfuploaded = request()->file('pdf');
$pdfname = $request->book_name . time() . '.' . $pdfuploaded->getClientOriginalExtension();
$pdfpath = public_path('/uploads/pdf');
$pdfuploaded->move($pdfpath, $pdfname);
$book->book_file = '/uploads/pdf/' . $pdfname;
$pdf = $book->book_file;
$pdfO = new Spatie\PdfToImage\Pdf($pdfpath . '/' . $pdfname);
$thumbnailPath = public_path('/uploads/thumbnails');
$thumbnail = $pdfO->setPage(1)
->setOutputFormat('png')
->saveImage($thumbnailPath . '/' . 'YourFileName.png');
// This is where you save the cover path to your database.
}

php getimagesize check if image exist

I am using following
if ($dataprev['imagefile']!='' && getimagesize('http://localhost/branches/standard/uploads/' . $dataprev['imagefile']) !== false) {
$html .= ' <img src="http://localhost/branches/standard/uploads/' . $dataprev['imagefile'] . '" height="100" border="0"/>';
}
To check if image exist. For some reason it doesnt work well. The app tries to load some unexisting images. What is the problem and how to do it better?

How to Fix "not allowed to load local resource" [Wordpress]

I am using the img tag but the code is in a php block.
<img class='client_image' src='" . get_stylesheet_directory() . "/assets/images/" . $print->client_images . "' alt='clientimage" . $clientindex . "' width='200' height ='200' />";
I keep getting not allowed to load local resource and I do not know why. I am using a premade wordpress theme. All I get is the broken image icon. The path is correct as well as the name of the file.
get_stylesheet_directory() will returns the path on your server, you need get_stylesheet_directory_uri() instead:
<img class='client_image' src='" . get_stylesheet_directory_uri() .
"/assets/images/" . $print->client_images . "' alt='clientimage" .
$clientindex . "' width='200' height ='200' />";

Cropping, Re-sizing and Saving images from one folder to another folder in wordpress

I'm adding an upgrade for the WordPress plugin which I have developed ages ago. This plugin is just a product catalog, so just show products and their images. Products can have more than one images.
I was re-sizing images in the older version of the plugin by CSS, assigning them width and height. Which was working but images look stretched but users were happy. Now I have added a new feature in the plugin which is to crop and re-size from the uploaded image and save it with a different name, like a thumbnail.jpg.
The new feature is working remarkably for the new users who upload images, but the thing is about old users who upgraded to newer version.
The issue is old users already has products and images. When I try to get all the products and images via foreach loop, it works perfectly on 200 - 250 images but breaks on more than 250+ images - No Error :(
Many of my old users has 600+ images so I want a way to crop and re-size the existing images and saves them with a new name and save the file names in the DB.
I'm using wordpress's default wp_get_image_editor(); function.
Here's my query to get old products which has images:
$wpc_product_images_sql = "Select wpc_posts.*, wpc_meta.* From " . $wpdb->posts . " As wpc_posts Inner Join " . $wpdb->postmeta . " As wpc_meta On wpc_posts.ID = wpc_meta.post_id Where wpc_meta.meta_key = 'product_images' Order By wpc_posts.post_title;
And here's my foreach loops (I'm using two loops. First one is getting the products which has images and the second loop is for get images from each post, as I'm mentioned earlier in my question that products can have more than one images. So have to use two loops)
foreach ($wpc_images_qry as $wpc_prod_images) {
echo '<div class="wpc_image_body">'
. '<h3>' . $wpc_prod_images->post_title . '</h3>'
. '<div class="wpc_images">';
$wpc_post_id = $wpc_prod_images->ID;
$wpc_product_images = get_post_meta($wpc_post_id, 'product_images', true);
$big_img_name = array();
foreach ($wpc_product_images as $wpc_prod_img) {
/// For Big
$big_resize_img = wp_get_image_editor($wpc_prod_img['product_img']);
if (!is_wp_error($big_resize_img)) {
$product_big_img = $wpc_prod_img['product_img'];
$product_img_explode = explode('/', $product_big_img);
$product_img_name = end($product_img_explode);
$product_img_name_explode = explode('.', $product_img_name);
$product_img_name = $product_img_name_explode[0];
$product_img_ext = $product_img_name_explode[1];
$big_crop = array('center', 'center');
$big_resize_img->resize($wpc_image_width, $wpc_image_height, $big_crop);
$big_filename = $big_resize_img->generate_filename('big-' . $wpc_image_width . 'x' . $wpc_image_height, $upload_dir['path'], NULL);
$big_resize_img->save($big_filename);
$big_img_name[]['wpc_big_img'] = $upload_dir['url'] . '/' . $product_img_name . '-big-' . $wpc_image_width . 'x' . $wpc_image_height . '.' . $product_img_ext;
if (file_exists($upload_dir['path'] . '/' . $product_img_name . '-big-' . $wpc_image_width . 'x' . $wpc_image_height . '.' . $product_img_ext)) {
echo $upload_dir['path'] . '/' . $product_img_name . '-big-' . $wpc_image_width . 'x' . $wpc_image_height . '.' . $product_img_ext . ' - <strong style="color: #7ad03a;">OK</strong><br>';
} else {
echo $upload_dir['path'] . '/' . $product_img_name . '-big-' . $wpc_image_width . 'x' . $wpc_image_height . '.' . $product_img_ext . ' <strong style="color: red">:(</strong><br>';
}
}
}
update_post_meta($wpc_post_id, 'wpc_big_images', $big_img_name);
echo '</div>'
. '</div>';
}
You might try adding the new image size with
add_image_size()
and then run
https://wordpress.org/plugins/regenerate-thumbnails/
which surely has a solution to the timeout problem.
There is a way to display a message for your plugin users upon update, ask them to install and run the plugin if they have old images.
Hope that works for you.
If you want resize image mean follow these one, it resize your image whenever upload image in media,
add_filter('wp_handle_upload_prefilter','aj_handle_upload');
function aj_handle_upload($file)
{
add_image_size( 'mobile', 360, 0, array( 'center', 'center' ));//mobile is userdefined name.
add_image_size( 'desktop', 700, 0, array( 'center', 'center' ));//desktop & tablet is user defined name.
return $file;
}

List files on directory and build the html

I'm making a page with some flash games, but, since in the past will be many games it will be very bored to add all those games one by one on the site. I know that using PHP we can get the list of the files in the directory and I know that, using this method, I can display all the games on the directory to the visitors page but, I was thinking if you can help me to add an image to each game that corespond with the name of the game. Ex:
In "games" folder we will have games: play1.swf, play2.swf, play3.swf
and, in "images" directory we will have images: play1.jpg, play2.jpg, play3.jpg.
To display the list of the games, can we make a combination of jpg files with swf files? Play1.jpg will be the image of play1.swf game and, when a visitor clicks on the image, will be redirect to a page named ex: playgames.php?play1
assuming this directory structure:
/foo/images
game1.jpg
game2.jpg
/foo/games
game1.swf
game2.swf
Something like this:
<?php
$dir = "/foo";
$files = new DirectoryIterator($dir . DIRECTORY_SEPARATOR . 'games');
$games = array();
foreach($files as $file){
if (!$file->isDot()) {
$potential_image_location = $file->getPath() . '/../images/' . $file->getBasename('.swf') . '.jpg';
if (file_exists($potential_image_location)) {
$games[$file->getBaseName()] = array(
'game_path' => $file->getPathname(),
'image_path' => realpath($potential_image_location),
);
}
}
}
if (!count($games)) {
die("Sorry, couldn't find any game / image combos");
}
?>
<table>
<?php
foreach ($games as $game_name => $info) {
echo '<tr><td><img src="' . htmlentities($info['image_path']) . '" alt="' . htmlentities($game_name) . '"></td>' . PHP_EOL;
echo '<td>' . $info['game_path'] . '</td></tr>';
}
?>
</table>
its all about using opendir in a clever way, heres the docs on it http://php.net/manual/en/function.opendir.php

Categories