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

时间:2013-07-29 02:06:21

标签: php facebook gd

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

我被困的部分是“拖动图像到位置封面”的事情。 Facebook使用一些算法在拖动事物期间将封面图像大小转换为不同的东西。例如,如果原始图像尺寸为920x720,同样图像的尺寸在Facebook设置封面页面上(将图像拖动到位置封面的东西),图像的尺寸为851x638。

我只是想知道facebook使用什么算法来设置图像尺寸(从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;

我需要弄清楚facebook如何计算前一个图像的新维度。它是否取决于图像的实际(原始)尺寸,还是取决于其他一些因素?

1 个答案:

答案 0 :(得分:0)

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

$aspect_ratio = $original_width / $original_height;

$new_width = $new_height * $aspect_ratio;

$new_height = $new_width / $aspect_ratio;