Resizing/Cropping 要调整为布局的图片
Resizing/Cropping Image to adjust into layout
我在将缩略图调整大小/缩放到所需布局时遇到问题。我可以在此处生成缩略图,这是 crop/resize.
的代码
$filename = $key.$_FILES["files"]["name"][$key];
$filetmp = $_FILES["files"]["tmp_name"][$key];
$filetype = $_FILES["files"]["type"][$key];
$filesize = $_FILES["files"]["size"][$key];
$fileinfo = getimagesize($tmp_name);
$filewidth = $fileinfo[0];
$fileheight = $fileinfo[1];
// GETS FILE EXTENSION
$fileextension = pathinfo($filename, PATHINFO_EXTENSION);
$microtime = preg_replace('/[^A-Za-z0-9]/', "", microtime());
$filepath = "../static/products/".$microtime.".".$fileextension;
$filepath_thumb = "../static/products/thumbs/".$microtime.".".$fileextension;
$filepath2 = "/static/products/".$microtime.".".$fileextension;
move_uploaded_file($filetmp,$filepath);
if ($filetype == "image/jpeg") {
$imagecreate = "imagecreatefromjpeg";
$imageformat = "imagejpeg";
}
if ($filetype == "image/png") {
$imagecreate = "imagecreatefrompng";
$imageformat = "imagepng";
}
if ($filetype == "image/gif") {
$imagecreate = "imagecreatefromgif";
$imageformat = "imagegif";
}
$ratio = $filewidth * 1.0 / $fileheight; // width/height
if( $ratio > 1) {
$new_width = 600;
$new_height = 600/$ratio;
}
else {
$new_width = 600*$ratio;
$new_height = 600;
}
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = $imagecreate($filepath); //photo folder
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $filewidth, $fileheight);
$imageformat($image_p, $filepath_thumb);//thumb folder
问题:
我不知道图片的文件大小是多少,但肯定是 HD/DSLR 图片。现在,上面的脚本生成了一个缩略图,其中 with = 600px 并且高度 = 图像的比例(例如 600x400)
在我的布局中,我希望以某种方式调整缩略图,以免以任何方式扭曲或拉伸。容纳缩略图的容器有 200 x 200 像素 width/height.
当缩略图在浏览器中呈现时,其尺寸得到
600px × 401px(缩放为 200px × 200px) 每张图片的高度都是随机的。
HTML:
<li class="column">
<div class="post-image">
<a href="http://localhost/example/products/40/">
<img src="http://localhost/example/static/products/thumbs/0446826001431411830.JPG" alt="my photo" /></a>
</div>
<div class="product-detail">
<h4 class="post-title">
<a href="/post/40/">Post title</a>
</h4>
</div>
</li>
CSS
.post-image{ width:100%; height:200px; }
.post-image img { width:auto; height:100%; min-height:200px; }
在没有宽度 x 高度细节的情况下精确缩放到 200 x 200px 的解决方案是什么...
我认为实现您想要的最简单方法是使用背景图片。
您必须将 html 更改为类似以下内容:
.post-image {
width:200px;
height:200px;
background-repeat: no-repeat;
background-position: center center;
/* the size makes sure it gets scaled correctly to cover the area */
background-size: cover;
}
.post-image a {
display: block:
width: 100%;
height: 100%;
}
.post-image img {
display: none;
}
<div class="post-image" style="background-image: url(http://media1.s-nbcnews.com/j/newscms/2015_20/1018941/150511-ceres-bright-spots-nasa-yh-0106p_fbf0f0c348c8d1881df19c5e07c819d1.nbcnews-fp-800-520.jpg);">
<!-- ^^^^^ set the background image -->
<a href="http://localhost/example/products/40/">
<!-- You can leave the image for SEO if required or you can take it out -->
<img src="http://localhost/example/static/products/thumbs/0446826001431411830.JPG" alt="my photo" /></a>
</div>
和原图:http://www.nbcnews.com/science/space/ceres-spots-shine-new-images-dwarf-planet-n357161
请注意,这样会截掉部分图像。如果你想在 200x200 块中显示整个图像,你需要 background-size: contain;
但你会在图像周围有 space (取决于图像的方向,上/下或左/右) :
.post-image {
width:200px;
height:200px;
background-repeat: no-repeat;
background-position: center center;
/* the size makes sure it gets scaled correctly to cover the area */
background-size: contain;
/* just to illustrate */
background-color: yellow;
}
.post-image a {
display: block:
width: 100%;
height: 100%;
}
.post-image img {
display: none;
}
<div class="post-image" style="background-image: url(http://media1.s-nbcnews.com/j/newscms/2015_20/1018941/150511-ceres-bright-spots-nasa-yh-0106p_fbf0f0c348c8d1881df19c5e07c819d1.nbcnews-fp-800-520.jpg);">
<!-- ^^^^^ set the background image -->
<a href="http://localhost/example/products/40/">
<!-- You can leave the image for SEO if required or you can take it out -->
<img src="http://localhost/example/static/products/thumbs/0446826001431411830.JPG" alt="my photo" /></a>
</div>
我认为如果不遵守比例,就无法获得不失真的缩放图像。您可以裁剪图像或将其缩小,但最终会出现边带。
我会尝试确定图像是纵向还是横向,然后相应地应用 css,以便图像适合您的 200x200 帧(这是 css 带条带的示例):
.post-image-portrait img { width:auto; height:200px; }
.post-image-landscape img {width:200px; height:auto;}
(我没有测试css,你可能需要更正)。
有一个非常巧妙的解决方案!您需要为您的图像创建一个父容器,然后将带有 position: absolute
的图像放入此容器中,如下所示:
<div class="imageParent">
<img src="http://placehold.it/600x400" alt="" class="image--fitHeight" />
</div>
以及需要的css:
.imageParent {
width: 200px;
height: 200px;
position: relative;
overflow: hidden;
}
.image--fitHeight {
position: absolute;
top: -500%;
left: -500%;
right: -500%;
bottom: -500%;
margin: auto;
min-width: 100%;
height: auto;
}
它将图像拉伸到 100% 的最小宽度,并且由于容器有 overflow: hidden
,它将覆盖父容器内以 margin: auto
为中心的图像。
它不会被改变/拉伸。
如果您的图片的比例类似于 16/9(因此宽度大于高度),请使用上述方法。反之亦然使用而不是 min-width: 100%; height: auto
你只需切换这两个: width: auto; min-height: 100%
此方法与background: cover
相同,但使用的是真实图像
我在将缩略图调整大小/缩放到所需布局时遇到问题。我可以在此处生成缩略图,这是 crop/resize.
的代码$filename = $key.$_FILES["files"]["name"][$key];
$filetmp = $_FILES["files"]["tmp_name"][$key];
$filetype = $_FILES["files"]["type"][$key];
$filesize = $_FILES["files"]["size"][$key];
$fileinfo = getimagesize($tmp_name);
$filewidth = $fileinfo[0];
$fileheight = $fileinfo[1];
// GETS FILE EXTENSION
$fileextension = pathinfo($filename, PATHINFO_EXTENSION);
$microtime = preg_replace('/[^A-Za-z0-9]/', "", microtime());
$filepath = "../static/products/".$microtime.".".$fileextension;
$filepath_thumb = "../static/products/thumbs/".$microtime.".".$fileextension;
$filepath2 = "/static/products/".$microtime.".".$fileextension;
move_uploaded_file($filetmp,$filepath);
if ($filetype == "image/jpeg") {
$imagecreate = "imagecreatefromjpeg";
$imageformat = "imagejpeg";
}
if ($filetype == "image/png") {
$imagecreate = "imagecreatefrompng";
$imageformat = "imagepng";
}
if ($filetype == "image/gif") {
$imagecreate = "imagecreatefromgif";
$imageformat = "imagegif";
}
$ratio = $filewidth * 1.0 / $fileheight; // width/height
if( $ratio > 1) {
$new_width = 600;
$new_height = 600/$ratio;
}
else {
$new_width = 600*$ratio;
$new_height = 600;
}
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = $imagecreate($filepath); //photo folder
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $filewidth, $fileheight);
$imageformat($image_p, $filepath_thumb);//thumb folder
问题:
我不知道图片的文件大小是多少,但肯定是 HD/DSLR 图片。现在,上面的脚本生成了一个缩略图,其中 with = 600px 并且高度 = 图像的比例(例如 600x400)
在我的布局中,我希望以某种方式调整缩略图,以免以任何方式扭曲或拉伸。容纳缩略图的容器有 200 x 200 像素 width/height.
当缩略图在浏览器中呈现时,其尺寸得到 600px × 401px(缩放为 200px × 200px) 每张图片的高度都是随机的。
HTML:
<li class="column">
<div class="post-image">
<a href="http://localhost/example/products/40/">
<img src="http://localhost/example/static/products/thumbs/0446826001431411830.JPG" alt="my photo" /></a>
</div>
<div class="product-detail">
<h4 class="post-title">
<a href="/post/40/">Post title</a>
</h4>
</div>
</li>
CSS
.post-image{ width:100%; height:200px; }
.post-image img { width:auto; height:100%; min-height:200px; }
在没有宽度 x 高度细节的情况下精确缩放到 200 x 200px 的解决方案是什么...
我认为实现您想要的最简单方法是使用背景图片。
您必须将 html 更改为类似以下内容:
.post-image {
width:200px;
height:200px;
background-repeat: no-repeat;
background-position: center center;
/* the size makes sure it gets scaled correctly to cover the area */
background-size: cover;
}
.post-image a {
display: block:
width: 100%;
height: 100%;
}
.post-image img {
display: none;
}
<div class="post-image" style="background-image: url(http://media1.s-nbcnews.com/j/newscms/2015_20/1018941/150511-ceres-bright-spots-nasa-yh-0106p_fbf0f0c348c8d1881df19c5e07c819d1.nbcnews-fp-800-520.jpg);">
<!-- ^^^^^ set the background image -->
<a href="http://localhost/example/products/40/">
<!-- You can leave the image for SEO if required or you can take it out -->
<img src="http://localhost/example/static/products/thumbs/0446826001431411830.JPG" alt="my photo" /></a>
</div>
和原图:http://www.nbcnews.com/science/space/ceres-spots-shine-new-images-dwarf-planet-n357161
请注意,这样会截掉部分图像。如果你想在 200x200 块中显示整个图像,你需要 background-size: contain;
但你会在图像周围有 space (取决于图像的方向,上/下或左/右) :
.post-image {
width:200px;
height:200px;
background-repeat: no-repeat;
background-position: center center;
/* the size makes sure it gets scaled correctly to cover the area */
background-size: contain;
/* just to illustrate */
background-color: yellow;
}
.post-image a {
display: block:
width: 100%;
height: 100%;
}
.post-image img {
display: none;
}
<div class="post-image" style="background-image: url(http://media1.s-nbcnews.com/j/newscms/2015_20/1018941/150511-ceres-bright-spots-nasa-yh-0106p_fbf0f0c348c8d1881df19c5e07c819d1.nbcnews-fp-800-520.jpg);">
<!-- ^^^^^ set the background image -->
<a href="http://localhost/example/products/40/">
<!-- You can leave the image for SEO if required or you can take it out -->
<img src="http://localhost/example/static/products/thumbs/0446826001431411830.JPG" alt="my photo" /></a>
</div>
我认为如果不遵守比例,就无法获得不失真的缩放图像。您可以裁剪图像或将其缩小,但最终会出现边带。 我会尝试确定图像是纵向还是横向,然后相应地应用 css,以便图像适合您的 200x200 帧(这是 css 带条带的示例):
.post-image-portrait img { width:auto; height:200px; }
.post-image-landscape img {width:200px; height:auto;}
(我没有测试css,你可能需要更正)。
有一个非常巧妙的解决方案!您需要为您的图像创建一个父容器,然后将带有 position: absolute
的图像放入此容器中,如下所示:
<div class="imageParent">
<img src="http://placehold.it/600x400" alt="" class="image--fitHeight" />
</div>
以及需要的css:
.imageParent {
width: 200px;
height: 200px;
position: relative;
overflow: hidden;
}
.image--fitHeight {
position: absolute;
top: -500%;
left: -500%;
right: -500%;
bottom: -500%;
margin: auto;
min-width: 100%;
height: auto;
}
它将图像拉伸到 100% 的最小宽度,并且由于容器有 overflow: hidden
,它将覆盖父容器内以 margin: auto
为中心的图像。
它不会被改变/拉伸。
如果您的图片的比例类似于 16/9(因此宽度大于高度),请使用上述方法。反之亦然使用而不是 min-width: 100%; height: auto
你只需切换这两个: width: auto; min-height: 100%
此方法与background: cover
相同,但使用的是真实图像