PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, October 11, 2022

[FIXED] How to resize an image like facebook cover

 October 11, 2022     facebook, gd, php     No comments   

Issue

What basically I am trying to do is to create a cover page for my personal website, just like facebook. Basically I am after the same layout of the cover as on facebook, so that user can get the same result while using the same cover on my site as well as on facebook.

The part I am stucked at is the "Drag image to position cover" thing. The Facebook uses some algorithm to convert the cover image size to something different during dragging thing. For example, if the original image dimensions are 920x720, the dimensions of same image while it is on facebook setting-cover page(drag image to position cover thing), the dimensions of the image are 851x638.

I just wanted to know what algorithm facebook uses to set the image dimensions(from 720 to 638)

NOTE: The cover has to be the pixel perfect

I know that the cover dimension of facebook is 851x315, so here is what I am doing:

    //$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;

I need to figure out how facebook calculates the new dimension of the image from previous one. Is it dependent of the actual(original) size of the image or is it dependent on some other factors?


Solution

The formulas for scaling images are quite simple.

$aspect_ratio = $original_width / $original_height;

$new_width = $new_height * $aspect_ratio;

or

$new_height = $new_width / $aspect_ratio;


Answered By - Herbert
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing