如何使用 PHP 调整高分辨率图片的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4754594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do You Resize a High Resolution Picture with PHP
提问by wavyGravy
I'm processing images when the user uploads a pic but my problem is high rez pics. How do you detect when the user uploads one? I'm using the GD library for PHP. Setting the quality to 90 with the imagejpeg() method doesn't do anything to it but scales the dimension and then sets the ppi to 72. I want to be able to preserve the dimension and drop the ppi to 72 at the same time. Any advice would be greatly appreciated.
当用户上传图片时,我正在处理图片,但我的问题是高分辨率图片。您如何检测用户何时上传?我正在为 PHP 使用 GD 库。使用 imagejpeg() 方法将质量设置为 90 对它没有任何作用,但会缩放尺寸,然后将 ppi 设置为 72。我希望能够保留尺寸并同时将 ppi 降低到 72。任何建议将不胜感激。
回答by coreyward
There is no such thing as PPI as far as you need to be concerned. Also, DPI doesn't exist in digital. There are only pixels, and the dimensions (which are just a measurement of pixels in either direction).
就您而言,没有 PPI 之类的东西。此外,数字中不存在 DPI。只有像素和尺寸(这只是在任一方向上测量像素)。
What you are really trying to do is downsample an image, reducing the overall dimensions of an image. You will probably want to do it proportionally, which is to say you want to keep the same ratio of width to height so that the image doesn't appear stretched after resampling it. Also worth noting, resizing and resampling differ in that resizing doesn't pay much attention to the content, resulting in very visually-poor resized images. You probably want resampling.
您真正想做的是对图像进行下采样,减少图像的整体尺寸。您可能希望按比例进行操作,也就是说您希望保持相同的宽高比,以便图像在重新采样后不会出现拉伸。同样值得注意的是,调整大小和重新采样的不同之处在于,调整大小不会太注意内容,导致调整后的图像视觉效果非常差。您可能想要重新采样。
The PHP documentation has some great examples on handling uploaded filesas well as how to resize imagesusing the GDImage library. I suggest you check them out. Other relevant methods you might want to know about:
PHP 文档有一些很好的例子来处理上传的文件以及如何使用 GDImage 库调整图像大小。我建议你检查一下。您可能想了解的其他相关方法:
- getimagesize()will let you check the dimensions of an image without opening it (useful to check if it needs to be resized without loading it up).
- imagesx()and imagesy()grab dimensions of a GD Image resource.
- getimagesize()可以让您在不打开图像的情况下检查图像的尺寸(用于检查是否需要在不加载的情况下调整其大小)。
- imagesx()和imagesy()抓取 GD Image 资源的尺寸。
回答by TMS
PPI, DPI...
PPI、DPI...
First, let us clarify the terminology. Both PPI(pixels per inch) and DPI(dot per inch) are ratios that specify conversion of pixels to physical units (inch in this case). In the digital world - computers, images, ... DPI is mistakenly used where PPI should be used instead. DPI in fact only exist in a printing world. But for us now, DPI = PPI. >> More info
首先,让我们澄清一下术语。既PPI(每英寸象素)和DPI(点每英寸)是(在此情况下英寸)指定像素的物理单位的转换比。在数字世界中 - 计算机、图像、... DPI 被错误地用于应该使用 PPI 的地方。DPI 实际上只存在于印刷界。但现在对我们来说,DPI = PPI。>> 更多信息
The resolution information (sometimes refered to as DPI, but it is PPI) is stored in the headers (image meta information). JPG and PNG do support this; but GIF doesn't support DPI- it is not present in the GIF header.
分辨率信息(有时称为 DPI,但它是 PPI)存储在标题(图像元信息)中。JPG 和 PNG 确实支持这个;但GIF 不支持 DPI- 它不存在于 GIF 标头中。
Change PPI in PHP
在 PHP 中更改 PPI
So, you are probably asking how to set PPI (DPI) in the image header? In PHP, you can do it using Imagick library (of course not for GIFs):
那么,您可能会问如何在图像标题中设置 PPI (DPI)?在 PHP 中,您可以使用 Imagick 库(当然不适用于 GIF):
$image = new Imagick('img.jpg'); // default 72 dpi image
$image->setImageResolution(500, 500);
$image->writeImage("img-500dpi.jpg"); // this image will have 500 dpi
If you download these images and try to print them e.g. in IrfanView, it will actually use the dpi information to convert pixels to target physical unit. Both images will be printed in different size.
如果您下载这些图像并尝试在 IrfanView 中打印它们,它实际上会使用 dpi 信息将像素转换为目标物理单位。两个图像将以不同的尺寸打印。
Image PPI won't work on web
图像 PPI 在网络上不起作用
Bad news is that this won't change anythingwhen displaying the image on a web page. The anchor unitfor the display is always a pixel in CSS. So img.jpg
and the img-500dpi.jpg
will have exactly the same size in the browser. Even if you open the images directly. Even if you use physical unit like cm
:
坏消息是, 在网页上显示图像时,这不会改变任何内容。显示的锚单元在 CSS 中始终是一个像素。所以img.jpg
和img-500dpi.jpg
将在浏览器中具有完全相同的大小。即使您直接打开图像。即使您使用物理单位,如cm
:
img { width: 8cm; height: 6cm; }
both images will be displayed in the same size; physical units are converted to pixels regardless of the image resolution using fixed ratio. And surprisingly even if you try to printthe raw images (not the page) in Firefox, they both will be printed at the same size. Cannot comment on other browsers. Strange enough - this works as expected in Irfanview.
两个图像将以相同的尺寸显示;无论图像分辨率如何,物理单位都使用固定比率转换为像素。令人惊讶的是,即使您尝试在 Firefox 中打印原始图像(而不是页面),它们也会以相同的尺寸打印。无法对其他浏览器发表评论。很奇怪 - 这在 Irfanview 中按预期工作。
PPI and retina images
PPI 和视网膜图像
Now with Iphones etc. retina (i.e. high resolution) images are becoming trendy. Here, a lot is spoken about DPI (PPI in fact), but this is all related only to the target device and the CSS media queries. There is nothing that would actually recognize the PPI of your image; you have to specify the actual dimensions of the high-res retina image in pixels. This is an example for 66x66 icon (from here):
现在有了 Iphone 等,视网膜(即高分辨率)图像变得流行起来。这里讲了很多 DPI(实际上是 PPI),但这都只与目标设备和CSS 媒体查询有关。没有什么可以真正识别图像的 PPI;您必须以像素为单位指定高分辨率视网膜图像的实际尺寸。这是 66x66 图标的示例(来自此处):
@media (-webkit-min-device-pixel-ratio: 2),
(min-resolution: 192dpi) {
.box {
background:url('images/icon66x66.png') no-repeat top left;
background-size: 33px 33px;
}
}
So you can actually "downscale" the high-res images by setting lower dimensions for them in the CSS. But I would only recommend to do this for retina images and within correct media query.
因此,您实际上可以通过在 CSS 中为它们设置较低的尺寸来“缩小”高分辨率图像。但我只建议对视网膜图像和正确的媒体查询执行此操作。
So, you have to resample in PHP
所以,你必须在 PHP 中重新采样
So in fact, if you want to make uploaded image appear smaller "on the Web", the best way is to resample it. Resampling means not to change the header only, but to change the actual pixel matrix of the image. Here is the code I use in PHP, which uses the GD library:
所以实际上,如果你想让上传的图像在“网络上”看起来更小,最好的方法是重新采样。重采样意味着不仅仅改变头部,而是改变图像的实际像素矩阵。这是我在 PHP 中使用的代码,它使用了 GD 库:
$t = imagecreatefromjpeg($old_img_path);
$s = imagecreatetruecolor($new_width, $new_height);
$x = imagesx($t);
$y = imagesy($t);
imagecopyresampled($s, $t, 0, 0, 0, 0, $new_width, $new_height, $x, $y);
imagejpeg($s, $new_img_path);
chmod($new_img_path, 0644);
Be aware that this requires quite a lot of memory, which might be limited by your hosting provider.
请注意,这需要相当多的内存,这可能会受到您的托管服务提供商的限制。
回答by niggles
If you're dealing with hi-res images a lot I'd go down the ImageMagick path as it is fastert + gives a lot more information about the images + lets you write images back at 300 DPI (or other resolutions) if you want.
如果您经常处理高分辨率图像,我会沿着 ImageMagick 路径走下去,因为它更快 + 提供更多有关图像的信息 + 如果需要,您可以以 300 DPI(或其他分辨率)写回图像.
Here's what I do with Jcrop and ImageMagick (but at 72 DPI)
这是我用 Jcrop 和 ImageMagick 做的事情(但在 72 DPI)
$picture = new Imagick($src);
$picture->cropImage( $_POST['w'], $_POST['h'], $_POST['x'], $_POST['y']);
$picture->resizeImage(690, 500, Imagick::FILTER_LANCZOS,1, true);
$picture->writeImage($cropped);
$picture->clear();
$picture->destroy();
回答by sh4
Using phpthumb (http://phpthumb.gxdlabs.com) is a good, elegant and easy solution.
使用 phpthumb ( http://phpthumb.gxdlabs.com) 是一个很好、优雅和简单的解决方案。
I have a working code example using it uploaded to GitHub. It can use both GD & Imagemagick, it's up to you.
我有一个使用它上传到 GitHub 的工作代码示例。它可以同时使用 GD 和 Imagemagick,这取决于您。
回答by user3865787
bool Imagick::setResolution ( float $x_resolution , float $y_resolution )
bool Imagick::setResolution ( float $x_resolution , float $y_resolution )
回答by blueberryfields
If you're reducing ppi, the dimensions will change to reflect the reduction - fewer pixels per inch mean there will be less inches to your picture.
如果您要减少 ppi,尺寸将发生变化以反映减少量 - 每英寸像素越少意味着图片的英寸数越少。