代码背后的数学公式


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)在线内/在线

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

  • 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_size,而x1,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])只是将缩略图坐标的偏移量转换为图像坐标系

当您将所有内容放在一起时,您会得到位于图像之外的位置(因此丢失的数据填充了背景色(因此,当将数据从图像复制到此位置的缩略图时,您得到了重新缩放的缩略图图像,该图像居中,围绕重新缩放的轴有空白区域...(除非我错过了什么(