I have observed the content links with images in facebook wrong size. Facebook has given some specifications for images with content links. To get best look and feel, we need to optimize our images. Whatever images you are sharing, convert that image size into optimized image size.

Facebook Optimized Image Size 

1200  X  300
  600  X  315
  600  X  600
If you create an Image of size 730 X 500 ,then what you have to do optimize the image for facebook. You might need photoshop or any other image manipulation tool for adjusting size.  Here I am giving you the quick way of doing this by java program.

Java Library :

For quality image scaling, I have used java-image-scaling library.

Java Code

Replace destinationDir with your destination folder path to save images. Give your image path to sourceImage
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import com.mortennobel.imagescaling.ResampleOp;

public class FacebookLinkSharedImages {
  static String destinationDir = "C:\\fbIcons";
  static String sourceImage = "C:\\facebook1.png";
  static ArrayList<Dimension> dimensions = new ArrayList<Dimension>();
  
  public static void main(String[] args) throws IOException {
      dimensions.add(new Dimension(1200,630));
      dimensions.add(new Dimension(600,315));
      dimensions.add(new Dimension(600,600));
      readFile(destinationDir);
  }
  
    public static void readFile(String path) throws IOException {
        File dir = new File(destinationDir);
        if(!dir.exists()) {
            dir.mkdirs();
        }
        BufferedImage bufferedImg = ImageIO.read(new File(sourceImage));
        for(Dimension dim : dimensions) {
            String destFilePath = destinationDir+"\\icon"+(int)dim.getWidth()+"X"+(int)dim.getHeight()+".png";
            File destFile = new File(destFilePath);
            if(!destFile.exists()) {
                  destFile.createNewFile();
            }
            System.out.println("File "+ destFile.getAbsolutePath());
            ImageIO.write(createScreenImage(bufferedImg,(int)dim.getWidth(),(int)dim.getHeight()), "png", destFile);    
        }
    } 
    
      private static BufferedImage createScreenImage(BufferedImage appIconImg, int width, int height) throws IOException {
          int type = appIconImg.getType() == 0? BufferedImage.TYPE_INT_ARGB : appIconImg.getType();
          BufferedImage newPGScreen = new BufferedImage(width, height, type);
          Graphics2D g = newPGScreen.createGraphics();
          g.setColor(new Color(255,255,255));
          g.fillRect(0, 0, width, height);
          double tw = (double)width, th = (double)height;
          int iw = appIconImg.getWidth();
          int ih = appIconImg.getHeight();
          double iwf = (double)(tw / iw);
          double ihf = (double)(th / ih);
          double imf = iwf < ihf ? iwf:ihf;
          int tfw = (int)(imf * iw);
          int tfh = (int)(imf * ih);
          ResampleOp  resampleOp = new ResampleOp(tfw, tfh);
          BufferedImage resizedIcon = resampleOp.filter(appIconImg, null);
          g.drawImage(resizedIcon, (int)((tw - tfw)/2), (int)((th - tfh)/2) , null);
          g.dispose();
          return newPGScreen;
      }
  
}

0 comments:

Blogroll

Follow this blog by Email

Popular Posts