加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP裁剪图像来修复宽度和高度,而不会损失尺寸比

发布时间:2020-12-13 17:28:09 所属栏目:PHP教程 来源:网络整理
导读:我想要创建一个100像素乘100像素的缩略图.我已经看到许多文章解释了方法,但最终如果要保持尺寸比例,最终会有width!= height. 例如,我有一个450像素×350像素的图像.我想裁剪到100像素乘100像素.如果我要保持这个比例,我最终会有100px 77px.当我们将这些图像
我想要创建一个100像素乘100像素的缩略图.我已经看到许多文章解释了方法,但最终如果要保持尺寸比例,最终会有width!= height.

例如,我有一个450像素×350像素的图像.我想裁剪到100像素乘100像素.如果我要保持这个比例,我最终会有100px 77px.当我们将这些图像列在一行和一列中时,这使得它变得丑陋.然而,没有尺寸比率的图像也将看起来很可怕.

我看过flickr的图片,看起来很棒.例如:
缩略图:http://farm1.static.flickr.com/23/32608803_29470dfeeb_s.jpg
中等大小:http://farm1.static.flickr.com/23/32608803_29470dfeeb.jpg
大尺寸:http://farm1.static.flickr.com/23/32608803_29470dfeeb_b.jpg

TKS

这是通过仅使用图像的一部分作为具有1:1宽高比(主要是图像的中心)的缩略图来完成的.如果你仔细观察,你可以在flickr缩略图中看到它.

因为你在你的问题上有“作物”,我不知道你是否还不知道,但是你想知道什么呢?

要使用裁剪,这里是一个例子:

//Your Image
$imgSrc = "image.jpg";

//getting the image dimensions
list($width,$height) = getimagesize($imgSrc);

//saving the image into memory (for manipulation with GD Library)
$myImage = imagecreatefromjpeg($imgSrc);

// calculating the part of the image to use for thumbnail
if ($width > $height) {
  $y = 0;
  $x = ($width - $height) / 2;
  $smallestSide = $height;
} else {
  $x = 0;
  $y = ($height - $width) / 2;
  $smallestSide = $width;
}

// copying the part into thumbnail
$thumbSize = 100;
$thumb = imagecreatetruecolor($thumbSize,$thumbSize);
imagecopyresampled($thumb,$myImage,$x,$y,$thumbSize,$smallestSide,$smallestSide);

//final output
header('Content-type: image/jpeg');
imagejpeg($thumb);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读