Copter Labs Copter Labs

Smart Design.

For Smart People.

Hold on... This isn't EnnuiDesign.com — What Gives?

It's been a long time coming, but Jason Lengstorf, formerly of Ennui Design, has expanded his team to include Drew Douglass, Rob MacKay, Henry Moran, and Tom Sturge.

It didn't feel right to keep the same name, so we decided to continue on as Copter Labs. You can expect the same great content under this new name!

Dynamically Change the Width and Height of Flash Objects

A Quick Note from the Guest Author

Hello everyone, I'm Brenley Dueck or better known as Brenelz. I currently run my own business called Brenelz Web Solutions which focuses primary on web design in Winnipeg. The web technologies I most specialize in are CSS, jQuery, AJAX, PHP, and the MySQL database. Please make sure to visit my web design blog and follow me on twitter.

Defining the Problem

In a recent project I was working on we had to allow the client to embed videos within the site. This had to be done using an easy-to-use customized CMS. The thought is that they can take the embed code right off the YouTube site and save it to the DB for use throughout the site.

YouTube's default dimensions for a video are 480 x 295, but what if you need a different size of video? Obviously we don't want to have the client fiddling around with the embed code changing the width and height attributes; so this is where I turned to using regular expressions in PHP. Take a look!

Let's pretend the following source is what we would like to modify and it is stored in a $youtube variable:

    $youtube = <<<EOF
<object width="480" height="295">
	<param name="movie" value="http://www.youtube.com/v/Xq-HRHMEmfg&hl=en&fs=1"></param>
	<param name="allowFullScreen" value="true"></param>
	<param name="allowscriptaccess" value="always"></param>
	<embed src="http://www.youtube.com/v/Xq-HRHMEmfg&hl=en&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="295"></embed>
</object>
EOF;

Fixing the Problem

How can we change the width and height attributes throughout this code? There are probably numerous ways, but none quite as easy as using regular expressions. If you are unfamiliar with the basics of regular expressions I suggest checking out Regular-Expressions.info.

A PHP function that makes use of regular expressions is "preg_replace". It allows you to use regular expressions to replace whatever matches. More details on the function can be found at PHP.NET

Creating The Function

We will be creating a function that allows you to resize any mark-up and not just YouTube videos. It will allow you to modify an elements width and height attributes as well as changes any inline styling. Below is what we get:

function resizeMarkup($markup, $dimensions)
{
    $w = $dimensions['width'];
    $h = $dimensions['height'];
    
    $patterns = array();
    $replacements = array();
    if( !empty($w) )
    {
        $patterns[] = '/width="([0-9]+)"/';
        $patterns[] = '/width:([0-9]+)/';
        
        $replacements[] = 'width="'.$w.'"';
        $replacements[] = 'width:'.$w;
    }
    
    if( !empty($h) )
    {
        $patterns[] = '/height="([0-9]+)"/';
        $patterns[] = '/height:([0-9]+)/';
        
        $replacements[] = 'height="'.$h.'"';
        $replacements[] = 'height:'.$h;
    }
    
    return preg_replace($patterns, $replacements, $markup); 
}

The most difficult part of the above to understand is probably the ([0-9]+) part. Let me break it down:

  1. () - wrap the value that we want to replace
  2. [0-9] - any number between and including 0 and 9
  3. + - one or more occurrences

Using the function:

We can then dynamically change the size based on the query string (?width=228&height=178)

$width = intval($_GET['width']);
$height = intval($_GET['height']);

$youtube = resizeMarkup($youtube, array(
		'width'=>$width,
		'height'=>$height
));
echo $youtube;

Below is the result:

Another little feature I should quickly point out is that you don't have to pass both height and width. For example if you want a div to dynamically change its width but stay a fixed height (YouTube caption), you can do the following:

$width = intval($_GET['width']);

$caption = <<<EOF
<div style="background:#F8EBD7;border:1px solid #444;text-align:center;width:500px;height:50px;">
	commandN Episode #170
</div>
EOF;

$caption = resizeMarkup($caption, array(
		'width'=>$width
));
echo $caption;

In this case, the div will remain 50px high but its width will be changed by the query string (?width=228)

commandN Episode #170

 

Conclusion

We now have a re-usable function that allows us to use many different sizes of the same YouTube clip without making the client tinker with any code!

A Note from Jason

Do you have an idea for an article? We're accepting guest submissions, so give me your best shot!

Date. 03/17/2009

Comments. 12

Category. PHP

Jason Lengstorf

Jason Lengstorf

Jason Lengstorf a turbogeek hailing from Portland, Oregon. He designs and develops websites using PHP, MySQL, JavaScript (jQuery), CSS, and HTML. He's written two books (PHP for Absolute Beginners [2009 Apress] and Pro PHP and jQuery [2010 Apress]), and he's written articles on development and design for Nettuts, CSS Tricks, and Smashing Magazine, among others.

Was This Post Helpful? Pass It On!

Share the Love

If this post taught you something, reminded you of something you had forgotten, or just made you feel good, there's really no better way to say "thank you" than passing it along to your friends.

Don't forget to like us on Facebook, join our newsletter, and/or subscribe to our RSS feed to make sure you hear about new posts first!

Join Our Gaggle of Geeks
* indicates required

Comments.

  1. Gravatar

    Great post. This not only is a useful function but it is also a great example of what you can do with regular expression magic.

  2. Gravatar

    Thanks Shane. Glad you liked the post! Regular Expressions have unlimited power. I don't even tap into a lot of the power it has.

  3. Gravatar

    This was really usefull! Thanks for sharing this with others!

  4. Gravatar

    Or you can simply add something like this in your CSS:

    div.comments_div object {

    max-width: 250px !important;

    max-height: 190px !important;

    }

    div.comments_div embed {

    max-width: 250px !important;

    max-height: 190px !important;

    }

    This is what I do in my commenting application, to allow users to embed ANY object, but limit their dimensions. ;)

  5. Gravatar

    Great post. After a long search, i found your post, very helpful. thanks

  6. Gravatar

    Its really great post, Thank you very much for this great article and code

    Thanks

  7. Gravatar

    Its really great post, Thank you very much for this great article and code

    Thanks

  8. Gravatar

    great content that i search for a long time.

    troubleshooting

  9. Gravatar

    great content that i search for a long time.

    troubleshooting

  10. Gravatar

    Thanks really easy to add to any framework, y added tube helper

    to my project in cakephp ;-)

  11. Gravatar

    Great post......Saved lots of my time..... keep posting :-)

  12. Gravatar

    Thanks, Really good this post

Join In.

Have something to say? By all means, speak up!

But first, a few rules:

  • Don’t be a jerk.
  • Use your real name, not your business name - this is a discussion, not a billboard.
  • Only <strong>, <em>, and <code> are allowed tags.
  • Wrap code samples in <code> tags.

Happy commenting!

Add a Comment