将每种尺寸的图像调整为固定宽度和高度

发布于 2025-01-07 04:24:35 字数 329 浏览 2 评论 0原文

我尝试了很多方法来做到这一点,但仍然遇到很多麻烦。

是否可以将所有图像调整为固定的宽度和高度?

我希望将每个上传的 width>=200pxheight>=260px 图像的大小调整为 width=200pxheight=260px,但我想保持一点比例,如果图像大于200x260px,则按比例调整其大小,然后捕获图像中心200x260px

我只需要知道从哪里开始以及做什么,但如果你有一个例子,我想看看。谢谢。

I tried a lot of methods to do that, but I still have a lot of troubles with that.

Is it possible to resize all images to fixed width and height?

I want every uploaded image with width>=200px and height>=260px to be resized to width=200px and height=260px, but I want to keep a bit of proportionality and if image is bigger than 200x260px to resize it proportionally and then to capture center of image 200x260px.

I just need idea to know where to start and what to do, but if you have an example I would like to see it. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青巷忧颜 2025-01-14 04:24:35

如果您想修剪图像,可以按以下方式进行:-

//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, 0, 0, $x, $y, $thumbSize, $thumbSize,        $smallestSide, $smallestSide);

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

If you want to trim the image you can do in the following way:-

//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, 0, 0, $x, $y, $thumbSize, $thumbSize,        $smallestSide, $smallestSide);

//final output
header('Content-type: image/jpeg');
imagejpeg($thumb);
亚希 2025-01-14 04:24:35

要开始编写函数,我们必须这样声明它......然后我们必须添加我们的属性。我们想要限制我们的图像,所以我们必须让函数知道我们想要限制它的尺寸,并且我们必须知道原始图像的大小是多少(我们将在一秒钟内讨论这部分) )。

<?php
function imageResize($width, $height, $target) {
//takes the larger size of the width and height and applies the
formula accordingly...this is so this script will work
dynamically with any size image
if ($width > $height) {
$percentage = ($target / $width);
} else {
$percentage = ($target / $height);
}
//gets the new value and applies the percentage, then rounds the value
$width = round($width * $percentage);
$height = round($height * $percentage);
//returns the new sizes in html image tag format...this is so you
can plug this function inside an image tag and just get the
return "width=\"$width\" height=\"$height\"";
}
?>

在测试新功能之前,我们需要获取要显示的图像的宽度和高度。 PHP 中有一个神奇的命令,叫做 getimagesize()。如果使用得当,该命令将返回图像的宽度、高度、类型,甚至 HTML 图像标签格式的宽度和高度(宽度=“x”高度=“y”)。

$mysock = getimagesize("images/sock001.jpg");

现在,$mysock 是一个数组,它保存有关我们要显示的特定图像的重要信息。在索引 0 中,我们有宽度 ($mysock[0]),在索引 1 中,我们有高度 ($mysock[1])。为了完成我们想要完成的事情,这确实是我们所需要的。想看看这个函数……好吧,函数?开始了!

假设您想要显示漂亮袜子的列表,但您希望页面上有空间将它们整齐地排成一行,并且为此它们的高度或宽度不能大于 150 像素。

<?php
//get the image size of the picture and load it into an array
$mysock = getimagesize("images/sock001.jpg");
?>
<!-using a standard html image tag, where you would have the
width and height, insert your new imageResize() function with
the correct attributes -->

<img src="images/sock001.jpg" <?php imageResize($mysock[0],
$mysock[1], 150); ?>>

就是这样!现在,无论原始文件大小如何,宽度或高度(或您指定的任何内容)都将限制为不超过 150 像素。

To begin writing the function, we have to declare it as such… Then we have to throw in our attributes. We want to restrict our image, so we have to let the function know the dimensions to which we want to restrict it, and we have to know what the original image size is to begin with (we’ll get to that part in a second).

<?php
function imageResize($width, $height, $target) {
//takes the larger size of the width and height and applies the
formula accordingly...this is so this script will work
dynamically with any size image
if ($width > $height) {
$percentage = ($target / $width);
} else {
$percentage = ($target / $height);
}
//gets the new value and applies the percentage, then rounds the value
$width = round($width * $percentage);
$height = round($height * $percentage);
//returns the new sizes in html image tag format...this is so you
can plug this function inside an image tag and just get the
return "width=\"$width\" height=\"$height\"";
}
?>

Before we take our new function on a test drive, we need to get the width and height of the image that we want to display. There is a magical command in PHP called getimagesize(). This command, used properly, will return the image width, height, type, and even the width and height in HTML image tag format (width=”x” height=”y”).

$mysock = getimagesize("images/sock001.jpg");

Now, $mysock is an array that holds vital information about the particular image we want to display. In index 0, we have the width ($mysock[0]), and in index 1, we have the height ($mysock[1]). That’s really all we need, in order to get what we want done. Want to see the function… well, function? Here we go!

Let’s say you want to display a list of your beautiful socks, but you want room on the page to show them neatly in a row, and to do that they cannot be larger than 150 pixels tall or wide.

<?php
//get the image size of the picture and load it into an array
$mysock = getimagesize("images/sock001.jpg");
?>
<!-using a standard html image tag, where you would have the
width and height, insert your new imageResize() function with
the correct attributes -->

<img src="images/sock001.jpg" <?php imageResize($mysock[0],
$mysock[1], 150); ?>>

That’s it! Now, no matter what the original file size, it will be restricted to no more than 150 pixels in width or height (or whatever you specify).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文