Resize Video Function ‘on the fly’ with PHP

I just wrote this function to resize video elements for my WordPress Lightbox Plugin. This specific script is designed to be used with an ajax call. Like this:

Let me know if you have any problems. I just rewrote most of this on the fly, so there may be errors. …but hopefully not. :)

jQuery / Javascript

Add this to your plugin’s javascript file between the

    $('#video-code').bind('paste', function(){
        var field = $(this);
            var vall = $(field).val();
            var html = encodeURIComponent(val);
            $.post(ajaxurl, 'action=clp_resize_video&html='+html, function(response){
        }, 150);

PHP WordPress Function

This can be added to your plugin’s functions file.

add_action('wp_ajax_clp_size_video', 'clp_size_video');
function clp_size_video(){
    $new_width = 320;
    $video = stripslashes($_POST['html']);
    $w = (integer)$w[1];
    $h = (integer)$h[1];
        $new_width = $w;
    $w2 = $new_width;
    $ratio = (float)($w2/$w);
    $h2 = (integer)($h * $ratio);
    $video = str_replace("width="$w"","width="$w2"",$video);
    $video = str_replace("height="$h"","height="$h2"",$video);
    echo $video;

About Clifton Hatfield

Clifton is the kind of guy that can read code better than he can read a book and prefers it that way. He likes cold pizza, moving sidewalks, and Indiana Jones. Clifton co-founded Internet Lifestyle Network from his home office in 2012 and he believes that true leadership is to inspire.