note 36452 deleted from function.getimagesize by cmb
| From: | cmb@php.net | Date: | Sun, 18 Dec 2016 09:43:50 +0000 |
| Subject: | note 36452 deleted from function.getimagesize by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-208638@lists.php.net to get a copy of this message | ||
Note Submitter: janoma_cl
----
If you want to show thumbnails keeping the original proportions, with defined maximum width and
height, you can use this function. This is useful when showing tables of user-uploaded images, that
not necessarily are same-sized. However, for big images (like wallpapers), a better option is to
create separated thumbnails with a image-editing software.
If the image is smaller or equal than the defined maximums, then it's showed without resizing.
If not, creates a link to a pop-up that shows the full-size image.
<?php
function show_thumbnail($file)
{
$max = 200 // Max. thumbnail width and height
$size = getimagesize($file);
if ( $size[0] <= $max && $size[1] <= $max )
{
$ret = '<img src="'.$file.'" '.$size[3].'
border="0">';
}
else
{
$k = ( $size[0] >= $size[1] ) ? $size[0] / $max : $size[1] / $max;
$ret = '<a href="javascript:;"
onClick="window.open(\'image.php?img=';
$ret .= $file.'\',\'\',\'width='.$size[0];
$ret .= ',height='.$size[1].'\')">';
$ret .= '<img src="'.$file.'"
width="'.floor($size[0]/$k).'"
height="'.floor($size[1]/$k).'" border="0" alt="View full-size
image"></a>';
}
return $ret;
}
?>
Here is the code of 'image.php':
<html>
<head>
<title>Image</title>
</head>
<body leftmargin="0" topmargin="0">
<?php echo ( is_file($_GET['img']) ) ? '<a href="#"
onClick="window.close();"><img src="'.$_GET['img'].'"
border="0" alt="Close window"></a>' : 'Invalid image
filename, or no filename entered. <a href="#"
onClick="window.close();">Close window</a>.' ?>
</body>
</html>