代码背后的数学公式

math formula behind code

有谁知道与以下代码串相关的数学公式?

 // 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]);

我正在研究这个脚本,但是我无法理解我在这个问题开头写的两行代码背后的逻辑,所以我想知道它们与哪个数学公式有关。

您需要分别查看每个宽高比 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)inside/on线

因此,如果我们知道线上一点的一个坐标,我们就可以通过利用三角形相似性来计算另一个坐标:

  • 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) 为空 space 新缩略图分辨率与原始缩略图分辨率之间
  • ($new_size[0] - $thumb_width)/2 是您需要将调整后的缩略图移动多少才能将其置于原始缩略图分辨率的中间
  • ($src_size[0] / $new_size[0])只是将这个偏移量从缩略图坐标转换为图像坐标系统的比例

当你把所有东西放在一起时,你得到的位置有点在图像之外(所以丢失的数据用背景色填充)所以当从这个位置将数据从图像复制到缩略图时,你会得到重新缩放的缩略图图像居中围绕重新缩放的轴为空 space...(除非我遗漏了什么)