Mic*_*iel 2 css php resize image stretch
如何避免照片拉伸?PHP从文件夹中随机选择2张照片并使用它们显示echo.但是现在,所有照片都是纵向的.
<?php if(!empty($images)) {
$rand_key = array_rand($images, 1);
$src = $images[$rand_key];
echo "<img class=\"flickrphoto\" src='".$src."' align='absmiddle'>";
unset($images[$rand_key]);
$rand_key = array_rand($images, 1);
$src = $images[$rand_key];
echo "<img class=\"flickrphoto\" src='".$src."' align='absmiddle'>";
} else {
echo 'Error';
} ?>
Run Code Online (Sandbox Code Playgroud)
而CSS:
.flickrphoto {
max-width: 100px;
max-height: 100px;
overflow: hidden;
}
Run Code Online (Sandbox Code Playgroud)
**编辑**
当前代码:
// protrait calculations;
$size = getimagesize($images_folder_path);
if($size[0] < $size[1]) {
$orientation = 'portrait';
} else {
$orientation = 'landscape';
}
Run Code Online (Sandbox Code Playgroud)
我只是检查方向是否是protrait并添加一个css类;
<?php if(!empty($images)) {
$rand_key = array_rand($images, 1);
$src = $images[$rand_key];
// protrait calculations;
$fullpath = // statement to fetch the path on the server
$size = getimagesize($fullpath);
if($size[0] < $size[1]) {
$orientation = 'portrait';
} else {
$orientation = 'landscape';
}
echo "<img class=\"flickrphoto ". $orientation ."\" src='".$src."' align='absmiddle'>";
unset($images[$rand_key]);
$rand_key = array_rand($images, 1);
$src = $images[$rand_key];
echo "<img class=\"flickrphoto\" src='".$src."' align='absmiddle'>";
} else {
echo 'Error';
} ?>
Run Code Online (Sandbox Code Playgroud)
在你的CSS这样的事情:
img.flickrphoto {
max-width: 100px;
max-height: 100px;
overflow: hidden;
}
img.flickrphoto.portrait {
max-width: 50px;
}
Run Code Online (Sandbox Code Playgroud)