尝试使用PHP GD以固定的宽度/高度生成按比例裁剪的缩略图 [英] Trying to generate proportionally cropped thumbnails at a fixed width/height with PHP GD

查看:154
本文介绍了尝试使用PHP GD以固定的宽度/高度生成按比例裁剪的缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用GD在PHP中创建一个缩略图生成器,它将拍摄图像并将其缩小到固定的宽度/高度。从原始图像(基于我的固定宽度/高度)拍摄的正方形将来自图像的中心,以提供按比例正确的缩略图。

I'm trying to create a Thumbnail Generator in PHP with GD that will take an image and reduce it to a fixed width/height. The square it takes from the original image (based on my fixed width/height) will come from the center of the image to give a proportionally correct thumbnail.

我会尝试用一些不错的ASCII来证明这句话令人困惑:}

I'll try to demonstrate that confusing sentence with some nice ASCII :}

LANDSCAPE EXAMPLE:

XXXXXXXXXXXXXXXX
XXXXOOOOOOOOXXXX
XXXXOOOOOOOOXXXX
XXXXOOOOOOOOXXXX
XXXXOOOOOOOOXXXX
XXXXXXXXXXXXXXXX

    XXXXXXXX    
    XXXXXXXX    
    XXXXXXXX    
    XXXXXXXX    


PORTRAIT EXAMPLE:

    XXXXXXXX
    XXXXXXXX
    OOOOOOOO
    OOOOOOOO
    OOOOOOOO
    OOOOOOOO
    XXXXXXXX
    XXXXXXXX

    XXXXXXXX    
    XXXXXXXX    
    XXXXXXXX    
    XXXXXXXX

如您所见,它从图像中心拉出一个正方形以用作缩略图。理论上,获取图像的高度/宽度然后根据我固定的宽度/高度计算偏移量以获得缩略图似乎很简单。但我似乎无法想到一种编码方式:/

As you can see, it pulls out a square from the center of the image to use as a thumbnail. It seems simple, in theory, to get the height/width of the image and then calculate the offset based on my fixed width/height to get the thumbnail. But I can't seem to think of a way to code it :/

另外,在拉出中心广场之前,我该如何调整图像大小?那么缩略图包含原始图像而不是一些放大图形的详细图像?

Also, how would I go about resizing the image before pulling out the center square? So the thumbnail contains a detailed image of the original rather than some zoomed in graphic?

推荐答案

这就是你要找的东西吗? :

Is this what you're looking for:

使用ASP / PHP裁剪图像

该脚本可用于创建纵向方形缩略图/风景图片。该解决方案需要两个步骤:

The script can be used to create square thumbnails out of portrait/landscape images. The solution requires two steps:

1:按比例调整图像大小以使其中一个尺寸与所需尺寸匹配,而其他尺寸相等或更大。

1: resize the image proportionally so that one of the dimension matches the desired dimension while other is equal or greater.


  • 示例1:要将1024x768px图像(ar = 1.33)裁剪为200x200px,您必须按比例将图像调整为266x200px(ar = 1.33) )

  • 示例2:要将600x900px图像(ar = 0.66)裁剪为200x200px,您必须按比例将图像大小调整为200x300px(ar = 0.66)

2:从图像中间裁剪;数学很简单。

2: crop from the middle of the image; the math is simple.


  • 示例1:从266x200px图像中提取200x200px部分,从33,0
  • $ b中裁剪$ b
  • 示例2:从200x300px图像中提取200x200px部分,裁剪为0,50

  • Example 1: To extract 200x200px portion from a 266x200px image, crop from 33,0
  • Example 2: To extract 200x200px portion from a 200x300px image, crop from 0,50

这篇关于尝试使用PHP GD以固定的宽度/高度生成按比例裁剪的缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆