如何调整像facebook封面一样的图片大小


How to resize an image like facebook cover

基本上我想做的是为我的个人网站创建一个封面,就像facebook一样。基本上,我追求与在脸书上相同的封面布局,这样用户可以在我的网站和脸书上使用相同的封面时获得相同的结果。

我被卡住的部分是"拖动图像以定位封面"的东西。Facebook在拖动过程中使用一些算法将封面图像大小转换为不同的大小。例如,如果原始图像的尺寸为920x720,则当它在facebook设置封面页面时(拖动图像以定位封面内容),图像的尺寸是851x638。

我只是想知道脸书使用什么算法来设置图像尺寸(从720到638)

注意:封面必须是像素完美

我知道facebook的封面尺寸是851x315,所以我正在做的是:

    //$x =  X origin cordinate variable obtained by dragging image 
    //$y =  Y origin cordinate variable obtained by dragging image 
    list($k, $l) = getimagesize($src); // $src == image source 
    //$w = Needs to be calculated
    //$h = Needs to be calculated 
    $img_r = imagecreatefromjpeg($src);
    $dst_r = ImageCreateTrueColor( 854,316 );
    imagecopyresampled($dst_r,$img_r,0,0,$x,$y,$w,$h,$k,$l);
    imagejpeg($dst_r,$src,$jpeg_quality);
    $img_name = writeToImage($src, $des); //writeToImage() is just a demo function created by me to do some other things with them which do not affect this part of code
    echo $img_name;

我需要弄清楚脸书是如何计算出前一张图片的新维度的。它是取决于图像的实际(原始)大小,还是取决于其他一些因素?

缩放图像的公式非常简单。

$aspect_ratio = $original_width / $original_height;
$new_width = $new_height * $aspect_ratio;

$new_height = $new_width / $aspect_ratio;