调整大小/裁剪/将图片填充到固定大小(Resize/crop/pad a picture to a fixed size)

编程入门 行业动态 更新时间:2024-10-24 16:31:02
调整大小/裁剪/将图片填充到固定大小(Resize/crop/pad a picture to a fixed size)

我需要将图片大小调整为固定大小。 但它必须保持宽度和高度之间的因素。

假设我想将238 (w) X 182 (h)的图片大小调整为238 (w) X 182 (h)

我现在所做的是:

Original width / target width = 1.333333 Original Height / target Height = 1.213333

现在我采取最小的因素。

现在我总是有正确的宽度238 / 1.333333 = 210 。 但身高仍然是160 。

如何在不破坏图片的情况下将高度降低到160 ?

我需要裁剪吗? 如果是这样如何?

I need to resize a picture to a fixed size. But it has to keep the factors between the width and height.

Say I want to resize a picture from 238 (w) X 182 (h) to 210 / 150

What I do now is:

Original width / target width = 1.333333 Original Height / target Height = 1.213333

Now I take the smallest factor.

Now I always have the right width since 238 / 1.333333 = 210. But the height is still 160.

How do I get the height down to 160 without ruining the pic?

Do I need to crop? If so how?

最满意答案

这个解决方案基本上和Can BerkGüder的相同,但是花了一些时间写作和评论之后,我觉得就像发布一样。

此功能会创建一个缩略图,其大小与您提供的尺寸完全相同。 调整图像大小以适合缩略图的大小。 如果它不适合两个方向,则它以thumnail为中心。 广泛的评论解释了这些事情。

function thumbnail_box($img, $box_w, $box_h) { //create the image, of the required size $new = imagecreatetruecolor($box_w, $box_h); if($new === false) { //creation failed -- probably not enough memory return null; } //Fill the image with a light grey color //(this will be visible in the padding around the image, //if the aspect ratios of the image and the thumbnail do not match) //Replace this with any color you want, or comment it out for black. //I used grey for testing =) $fill = imagecolorallocate($new, 200, 200, 205); imagefill($new, 0, 0, $fill); //compute resize ratio $hratio = $box_h / imagesy($img); $wratio = $box_w / imagesx($img); $ratio = min($hratio, $wratio); //if the source is smaller than the thumbnail size, //don't resize -- add a margin instead //(that is, dont magnify images) if($ratio > 1.0) $ratio = 1.0; //compute sizes $sy = floor(imagesy($img) * $ratio); $sx = floor(imagesx($img) * $ratio); //compute margins //Using these margins centers the image in the thumbnail. //If you always want the image to the top left, //set both of these to 0 $m_y = floor(($box_h - $sy) / 2); $m_x = floor(($box_w - $sx) / 2); //Copy the image data, and resample // //If you want a fast and ugly thumbnail, //replace imagecopyresampled with imagecopyresized if(!imagecopyresampled($new, $img, $m_x, $m_y, //dest x, y (margins) 0, 0, //src x, y (0,0 means top left) $sx, $sy,//dest w, h (resample to this size (computed above) imagesx($img), imagesy($img)) //src w, h (the full size of the original) ) { //copy failed imagedestroy($new); return null; } //copy successful return $new; }

用法示例:

$i = imagecreatefromjpeg("img.jpg"); $thumb = thumbnail_box($i, 210, 150); imagedestroy($i); if(is_null($thumb)) { /* image creation or copying failed */ header('HTTP/1.1 500 Internal Server Error'); exit(); } header('Content-Type: image/jpeg'); imagejpeg($thumb);

This solution is basically the same as Can Berk Güder's, but after having spent some time writing and commenting, I felt like posting.

This function creates a thumbnail that is exactly as big as the size you give it. The image is resized to best fit the size of the thumbnail. If it does not fit exactly in both directions, it's centered in the thumnail. Extensive comments explain the goings-on.

function thumbnail_box($img, $box_w, $box_h) { //create the image, of the required size $new = imagecreatetruecolor($box_w, $box_h); if($new === false) { //creation failed -- probably not enough memory return null; } //Fill the image with a light grey color //(this will be visible in the padding around the image, //if the aspect ratios of the image and the thumbnail do not match) //Replace this with any color you want, or comment it out for black. //I used grey for testing =) $fill = imagecolorallocate($new, 200, 200, 205); imagefill($new, 0, 0, $fill); //compute resize ratio $hratio = $box_h / imagesy($img); $wratio = $box_w / imagesx($img); $ratio = min($hratio, $wratio); //if the source is smaller than the thumbnail size, //don't resize -- add a margin instead //(that is, dont magnify images) if($ratio > 1.0) $ratio = 1.0; //compute sizes $sy = floor(imagesy($img) * $ratio); $sx = floor(imagesx($img) * $ratio); //compute margins //Using these margins centers the image in the thumbnail. //If you always want the image to the top left, //set both of these to 0 $m_y = floor(($box_h - $sy) / 2); $m_x = floor(($box_w - $sx) / 2); //Copy the image data, and resample // //If you want a fast and ugly thumbnail, //replace imagecopyresampled with imagecopyresized if(!imagecopyresampled($new, $img, $m_x, $m_y, //dest x, y (margins) 0, 0, //src x, y (0,0 means top left) $sx, $sy,//dest w, h (resample to this size (computed above) imagesx($img), imagesy($img)) //src w, h (the full size of the original) ) { //copy failed imagedestroy($new); return null; } //copy successful return $new; }

Example usage:

$i = imagecreatefromjpeg("img.jpg"); $thumb = thumbnail_box($i, 210, 150); imagedestroy($i); if(is_null($thumb)) { /* image creation or copying failed */ header('HTTP/1.1 500 Internal Server Error'); exit(); } header('Content-Type: image/jpeg'); imagejpeg($thumb);

更多推荐

本文发布于:2023-07-17 13:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1144995.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:大小   图片   Resize   crop   size

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!