2015-11-19 75 views
0

有谁知道关联下列代码的数学公式吗?代码后面的数学公式

// aspect ratio < 
$src_pos = array(0, (($new_size[1] - $thumb_height) * ($src_size[1] /$new_size[1]))/2); 
// aspect ratio > 
$src_pos = array((($new_size[0] - $thumb_width) * ($src_size[0]/$new_size[0]))/2, 0); 

他们是从上传的图片创建拇指更宽的脚本中:

//variables 
$src_size = getimagesize($_FILES["file"]["name"]); 
$thumb_width = 250; 
$thumb_height = 200; 
$src_aspect = round(($src_size[0]/$src_size[1]), 1); 
$thumb_aspect = round(($thumb_width/$thumb_height), 1); 

if ($src_aspect < $thumb_aspect){ 
    //higher 
    $new_size = array($thumb_width,($thumb_width/$src_size[0]) * $src_size[1]); 
    $src_pos = array(0, (($new_size[1] - $thumb_height) * ($src_size[1] /$new_size[1]))/2); 
}else if($src_aspect > $thumb_aspect){ 
    //wider 
    $new_size = array(($thumb_width/$src_size[1]) * $src_size[0], $thumb_height); 
    $src_pos = array((($new_size[0] - $thumb_width) * ($src_size[0]/$new_size[0]))/2, 0); 
}else{ 
    //same shape 
    $new_size = array($thumb_width, $thumb_height); 
    $src_pos = array(0, 0); 
} 

if ($new_size[0] < 1) $new_size[0] = 1; 
if ($new_size[1] < 1) $new_size[1] = 1; 


//creation of thumb 
$thumb = imagecreatetruecolor($thumb_width, $thumb_height); 
imagecopyresampled($thumb, $src, 0, 0, $src_pos[0], $src_pos[1], $new_size[0], $new_size[1], $src_size[0], $src_size[1]); 

我STUDING这个剧本,但我无法理解的两行代码后面我在开始写的逻辑这个问题,所以我想知道他们与哪个数学公式有关。

+0

看起来像PHP所以我添加了TAG来启用语法高亮如果你有不同的语言改变你正在使用的语言的TAG – Spektre

+0

O对不起,它是PHP –

回答

1

您需要分别查看各个宽高比if部分,然后将它们混合在一起,这从起点开始有点混乱(在您的问题中添加了注释)。

我看到它像这样:

// load image 
$src_size = getimagesize($_FILES["file"]["name"]); 
// static fixed resolution for the thumb 
$thumb_width = 250; 
$thumb_height = 200; 
// aspects (if the x or y size is bigger for image and thumb 
$src_aspect = round(($src_size[0]/$src_size[1]), 1); 
$thumb_aspect = round(($thumb_width/$thumb_height), 1); 

// rescale height because the result will not exceeding thumb height in this case 
if ($src_aspect < $thumb_aspect){ 
    $new_size = array 
    (
    $thumb_width,         // thumb width stays as is 
    ($thumb_width/$src_size[0]) * $src_size[1] // thumb height is rescaled by image aspect 
    ); 
    // this just compute the distance to move the thumb after rescaling so it is still centered 
    $src_pos = array(0, (($new_size[1] - $thumb_height) * ($src_size[1] /$new_size[1]))/2); 
} 
// rescale width because the result will not exceeding thumb width in this case 
else if($src_aspect > $thumb_aspect){ 
    $new_size = array 
    (
    ($thumb_width/$src_size[1]) * $src_size[0], // thumb width is rescaled by image aspect 
    $thumb_height         // thumb height stays as is 
    ); 
    // this just compute the distance to move the thumb after rescaling so it is still centered 
    $src_pos = array((($new_size[0] - $thumb_width) * ($src_size[0]/$new_size[0]))/2, 0); 
}else{ 
    //same shape 
    $new_size = array($thumb_width, $thumb_height); 
    $src_pos = array(0, 0); 
} 

if ($new_size[0] < 1) $new_size[0] = 1; 
if ($new_size[1] < 1) $new_size[1] = 1; 


//creation of thumb 
$thumb = imagecreatetruecolor($thumb_width, $thumb_height); 
imagecopyresampled($thumb, $src, 0, 0, $src_pos[0], $src_pos[1], $new_size[0], $new_size[1], $src_size[0], $src_size[1]); 

我希望评论是够你......但可以肯定,我们看一下:

  • ($thumb_width/$src_size[0]) * $src_size[1]

我会用这个代替(所以只需要整数数学)

  • ($thumb_width * $src_size[1])/$src_size[0]

所以$thumb_width是拇指指甲目标最大分辨率和$src_size[1]/$src_size[0]是图像宽高比常数小于1.0。当您更仔细地观察整条线时,它是无偏移的简单线性插值(起始点为(0,0)),因此结果将小于缩略图分辨率(将适合于内部),并仍然与原始图像宽高比匹配。

数学公式:

  • 让有2个点(x0,y0)(x1,y1)表示的线的端点。
  • 和一些任意点(x,y)内/上线

所以,如果我们知道线上的点的一个坐标,我们可以通过利用三角形相似这样计算其他:

  • x=x0+(y-y0)*(x1-x0)/(y1-y0)
  • y=y0+(x-x0)*(y1-y0)/(x1-x0)

这就是所谓的线性内插。至于你的情况(x0,y0)=(0,0)那么等式变成了:

  • x=y*x1/y1
  • y=x*y1/x1

哪个是你的代码表示......而x,ynew_sizex1,y1src_size

现在位置偏移量

比如你计算这样的:

  • ($new_size[0] - $thumb_width) * ($src_size[0]/$new_size[0]))/2 这样的条件是:
  • ($new_size[0] - $thumb_width)之间新的和原有的缩略图分辨率空白
  • ($new_size[0] - $thumb_width)/2是你需要多少转移调整缩略图将其放在原始缩略图分辨率的中间
  • ($src_size[0]/$new_size[0])只是将此偏移量从缩略图坐标缩放转换为图像坐标系

当你把所有的位置放在图像的外面(所以缺失的数据被填充背景颜色)所以当从这个位置将图像复制到缩略图时,缩放图像的缩放图像居中(除非我缺少一些东西)

+0

感谢spektre,我正在研究你的回答。 –

+0

如果 X = Y * X1/Y1 Y = X * Y1/X1 这意味着: Y =(($ thumb_width * $ src_size [1])/ $ src_size [0]) X =( $ thumb_height * $ src_size [0])/ $ src_size [1]) 弗里斯特情况下返回 y的samee结果=($ thumb_width/$ src_size [0])* $ src_size [1]) 但第二种情况不会返回相同的结果 x =($ thumb_width/$ src_size [1])* $ src_size [0]) –

+0

在等式中: x = y * x1/y1 y = x * y1/x1 你的意思是说,x,y是thumb_size和x1,y1是src_siz e,对吗?或者x,y是new_size? –