Dec
21
PHP Hack - Set Image Size
December 21, 2007 |
If you don’t specify height and width attributes on the img tag on a web page, the page will jerk around as it’s being downloaded. Well, setting each image’s height and width can be tedious, there is a PHP funciton called getimagesize that can dynamically retrieve the actual width and height of an image. Let’s use an image of our Finding Nemo celebrity Dory to make an example:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>PHP Size Image</title>
</head>
<?php
function placegraphic($file) {
list($width, $height) = getimagesize($file);
echo "<img src=\"$file\" width=\"$width\" height=\"$height\" />";
}
?>
<body>
<?php placegraphic("../images/icons/nemo/dory-256x256.png"); ?>
</body>
</html>
Below is the demo page link and source code link:
http://www.lab.highub.com/php/hacks/size-image.php
Similar Posts
- None Found


































