title: Image processing with Play Framework
body: <p>I have the following code to get a profile picture from facebook, resize it if necessary, and crop it to have a nice square picture:</p>
  
  <pre><code>            url = new URL(profilePictureUrl);
              InputStream stream = url.openStream();
  
              // Create the file from the URL
              File temp = File.createTempFile(Codec.UUID(), "temp");
              OutputStream out = null;
              try {
                  out = new FileOutputStream(temp);
              } catch (FileNotFoundException e1) {
                  Logger.error("SF-ERROR : Error creating temp file", e1);
              }
  
              int read = 0;
              byte[] bytes = new byte[1024];
  
              while ((read = stream.read(bytes)) != -1) {
                  out.write(bytes, 0, read);
              }
  
              stream.close();
              out.close();
  
              String uuid = Codec.UUID();
              File resizedFile = File.createTempFile("resized-" + uuid, ".image");
              File croppedFile = File.createTempFile("cropped-" + uuid, ".image");
  
              // Check that this is in fact, an image
              if (temp != null &amp;&amp; ImageIO.read(temp) != null) {
  
                  int height = ImageIO.read(temp).getHeight();
                  int width = ImageIO.read(temp).getWidth();
  
                  double newWidth = (height * 180) / (width);
  
                  boolean high = false;
                  boolean wide = false;
  
                  if (newWidth &gt; 220) {
                      high = true;
                  }
                  if (newWidth &lt; 140) {
                      wide = true;
                  }
  
                  if (height &gt; width) {
                      // Resize the image to a width of 180 pixels.
                      Images.resize(temp, resizedFile, 180, -1);
                  } else {
                      // Resize the image to a height of 180 pixels.
                      Images.resize(temp, resizedFile, -1, 180);
                  }
  
                  // Crop the remaining width or height to 180 pixels. This way we get a nice square picture.
                  if (high) {
                      Images.crop(resizedFile, croppedFile, 0, 20, 180, 200);
                  } else if (wide) {
                      Images.crop(resizedFile, croppedFile, 20, 0, 200, 180);
                  } else {
                      Images.crop(resizedFile, croppedFile, 0, 0, 180, 180);
                  }
  </code></pre>
  
  <p>The wide and high booleans are there so that the crop is taken roughly in the middle..</p>
  
  <p>This works, but is taking an awfull lot of time.</p>
  
  <p>Anyone can see if this can be done quicker, or if I am doing something wrong?</p>