Php Function to resize video from YouTube and Vimeo

When you make a web application that let the user insert video embeds coming from YouTube or from Vimeo, or…

October 6, 2010

When you make a web application that let the user insert video embeds coming from YouTube or from Vimeo, or what ever else, there is the problem of the embed size. In fact the object tag (iframe, embed, object) can damage your template and that’s not nice.

You can try to handle the input and the best thing (and also the simplest) you can do is to use regular expression to extract the aspect ratio of the video, remove unwanted tags (such as p tag after the embed that tell the origin of the video) and change the dimensions as you need for your site, preserving template.

I’ve made this function that receive the input $video string that contains the dirty embed code as it is from youtube. The second parameter $new_width (if specified) permits to resize the embed code proportionally to the width you need for your template.
This function removes also unwanted tags added after the important tag (such as happens from Vimeo embeds that have a p tag after the iframe).

function resizeEmbed($video,$new_width='') {
	$video = real_strip_tags($video,array('iframe','embed','param','object'),true);
	preg_match("/width=\"([^\"]*)\"/i",$video,$w); $w = (integer)$w[1];
	preg_match("/height=\"([^\"]*)\"/i",$video,$h); $h = (integer)$h[1];
        if (!$new_width) $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);
	return array("embed"=>$video,"w"=>$w2,"h"=>$h2,"w0"=>$w,"h0"=>$h);

As you can see this function uses also this “real strip tags” function that I’ve found on the site:

function real_strip_tags($i_html, $i_allowedtags = array(), $i_trimtext = FALSE) {
	if (!is_array($i_allowedtags)) $i_allowedtags = !empty($i_allowedtags) ? array($i_allowedtags) : array();
	$tags = implode('|', $i_allowedtags);
	if (empty($tags)) $tags = '[a-z]+';
	preg_match_all('@</?\s*(' . $tags . ')(\s+[a-z_]+=(\'[^\']+\'|"[^"]+"))*\s*/?>@i', $i_html, $matches);
	$full_tags = $matches[0];
	$tag_names = $matches[1];
	foreach ($full_tags as $i => $full_tag) {
		if (!in_array($tag_names[$i], $i_allowedtags)) if ($i_trimtext) unset($full_tags[$i]); else $i_html = str_replace($full_tag, '', $i_html);
	return $i_trimtext ? implode('', $full_tags) : $i_html;


I'm a software engineer, an everyday web developer and a maker. I usually build sites with PHP, within or without WordPress. I build Internet of Things with Arduino and ESP8266. I'm the founder of and and I'm actually the Chief Technical Officer of Better Days web agency.

Comments on “Php Function to resize video from YouTube and Vimeo”

One thought

  1. Luke says:

    Great post. For a more low-level explanation about how to actually get the code to embed videos, I wrote up this post recently:

Comments are closed


Highlight text for search results in PHP

Useful code to highlight text occurences in search results or in a text. How to highlight text in a string…

September 2, 2016

Customize your site icon for new WordPress 4.4 embeds

With the new WordPress 4.4 embed feature┬áthe posts of your blog becomes embeddable in other WordPress sites, like you do…

December 11, 2015

How to add rel=”nofollow” to links with preg_replace()

Adding rel="nofollow" to external link is a good SEO practice.

September 22, 2015

Block junk emails, spammers and temporary emails

If you need an Email Validator Function, consider this version that includes also the check against common temporary mail services…

December 3, 2013

How many times a web link has been shared on Twitter

Twitter share button and Facebook share button are the most used buttons to share links on Internet. You can read…

October 19, 2012

get MySpace events with a PHP function

Here is a function to read the concerts for a myspace band page. This code retrieves the “shows page” for…

February 21, 2011