Oct 06 2010

Php Function to resize video from YouTube and Vimeo

Category: PhpGiulio Pons @ 2:31 pm

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 php.net 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;
}
Share

Tags: , , , ,


Jan 20 2010

New version of Mini Bots PHP Class (v.1.4)

Category: Php,Spiders & web botsGiulio Pons @ 11:53 pm

I’ve added three more bots to the Mini Bots Php Class, now the version number is 1.4 and it has these new features.

Follow the link, watch demos and download the class from the Mini Bots Php Class page:

  1. addeded twitterInfo method to retrieve the numbers of following, followers, and lists.
  2. addeded url2pdf method to convert and save locally a page to a pdf (thanks to pdfmyurl.com service).
  3. addeded webpage2txt method to extract all the text from a url, stripping html but also scripts and styles.

What else do you need? Suggest some usefull small bots. Comment to suggest.

Share

Tags: , , , , , ,


Jan 16 2010

PHP Web page to text function

Category: Php,Spiders & web botsGiulio Pons @ 2:55 pm

I’ve found this nice small bot on the www.php.net site, thanks to the author of the script on the preg_replace page.
This bot returns the text content of a url and it could be used to take text from a site and find relevant words to search.

It’s nice because it uses CURL and let us see some nice stuff that CURL does:

  1. hide himself presenting with a specific user agent, to seem a browser and not a spider
  2. follows redirect (this means that you can call this function with a tiny url to retrieve the text in the real page!)
  3. use very powerful regular expression to remove html tags, but also javascript and styles (they remain if you simply do a strip_tags)

This script will be included in the next version of the Mini Bots PHP Class.

function webpage2txt($url) {
	$user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";

	$ch = curl_init();    // initialize curl handle
	curl_setopt($ch, CURLOPT_URL, $url); // set url to post to
	curl_setopt($ch, CURLOPT_FAILONERROR, 1);              // Fail on errors
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);    // allow redirects
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
	curl_setopt($ch, CURLOPT_PORT, 80);            //Set the port number
	curl_setopt($ch, CURLOPT_TIMEOUT, 15); // times out after 15s

	curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);

	$document = curl_exec($ch);

	$search = array('@<script[^>]*?>.*?</script>@si',  // Strip out javascript
		'@<style[^>]*?>.*?</style>@siU',    // Strip style tags properly
		'@<[\/\!]*?[^<>]*?>@si',            // Strip out HTML tags
		'@<![\s\S]*?–[ \t\n\r]*>@',         // Strip multi-line comments including CDATA
		'/\s{2,}/',
	);

	$text = preg_replace($search, "\n", html_entity_decode($document));

	$pat[0] = "/^\s+/";
	$pat[2] = "/\s+\$/";
	$rep[0] = "";
	$rep[2] = " ";

	$text = preg_replace($pat, $rep, trim($text));

	return $text;
}

echo webpage2txt("http://www.rockit.it");
Share

Tags: , , , ,


Next Page »