private static void createCropFile(
      int thumbnailWidth, int thumbnailHeight, ThumbnailDetail thumbDetailComplete)
      throws IOException, ThumbnailException {

    String pathOriginalFile =
        getImageDirectory(thumbDetailComplete.getInstanceId())
            + thumbDetailComplete.getOriginalFileName();

    if (thumbnailWidth == -1 && thumbnailHeight != -1) {
      // crop with fix height
      String[] result =
          ImageUtil.getWidthAndHeightByHeight(new File(pathOriginalFile), thumbnailHeight);
      thumbnailWidth = Integer.valueOf(result[0]);
      thumbnailHeight = Integer.valueOf(result[1]);
    } else if (thumbnailHeight == -1 && thumbnailWidth != -1) {
      // crop with fix width
      String[] result =
          ImageUtil.getWidthAndHeightByWidth(new File(pathOriginalFile), thumbnailWidth);
      thumbnailWidth = Integer.valueOf(result[0]);
      thumbnailHeight = Integer.valueOf(result[1]);
    } else if (thumbnailHeight == -1) {
      // crop full file
      String[] result = ImageUtil.getWidthAndHeight(new File(pathOriginalFile));
      thumbnailWidth = Integer.valueOf(result[0]);
      thumbnailHeight = Integer.valueOf(result[1]);
    }

    String extension = FilenameUtils.getExtension(thumbDetailComplete.getOriginalFileName());
    // add 2 to be sure cropfilename is different from original filename
    String cropFileName = String.valueOf(new Date().getTime() + 2) + '.' + extension;
    thumbDetailComplete.setCropFileName(cropFileName);
    // crop sur l image entiere
    cropFromPath(pathOriginalFile, thumbDetailComplete, thumbnailHeight, thumbnailWidth);
  }
 public static ThumbnailDetail cropThumbnail(
     ThumbnailDetail thumbnail, int thumbnailWidth, int thumbnailHeight) {
   try {
     ThumbnailDetail thumbDetailComplete = getThumbnailService().getCompleteThumbnail(thumbnail);
     if (thumbDetailComplete.getCropFileName() != null) {
       // on garde toujours le meme nom de fichier par contre on le supprime
       // puis le recreer avec les nouvelles coordonnees
       deleteThumbnailFileOnServer(
           thumbnail.getInstanceId(), thumbDetailComplete.getCropFileName());
     } else {
       // case creation
       String extension = FilenameUtils.getExtension(thumbDetailComplete.getOriginalFileName());
       String cropFileName = String.valueOf(new Date().getTime()) + '.' + extension;
       thumbDetailComplete.setCropFileName(cropFileName);
     }
     String pathCropdir = getImageDirectory(thumbnail.getInstanceId());
     String pathOriginalFile = pathCropdir + thumbDetailComplete.getOriginalFileName();
     String pathCropFile = pathCropdir + thumbDetailComplete.getCropFileName();
     createCropThumbnailFileOnServer(
         pathOriginalFile, pathCropdir, pathCropFile, thumbnail, thumbnailWidth, thumbnailHeight);
     thumbDetailComplete.setXStart(thumbnail.getXStart());
     thumbDetailComplete.setXLength(thumbnail.getXLength());
     thumbDetailComplete.setYStart(thumbnail.getYStart());
     thumbDetailComplete.setYLength(thumbnail.getYLength());
     getThumbnailService().updateThumbnail(thumbDetailComplete);
     return thumbDetailComplete;
   } catch (Exception e) {
     throw new ThumbnailRuntimeException(
         "ThumbnailController.cropThumbnail()",
         SilverpeasRuntimeException.ERROR,
         "thumbnail_MSG_GET_IMAGE_KO",
         e);
   }
 }
  /**
   * To update thumbnails files informations.
   *
   * @param thumbDetail :ThumbnailDetail.
   * @author Sebastien ROCHET
   */
  public static void updateThumbnail(ThumbnailDetail thumbDetail) {

    try {
      ThumbnailDetail completeThumbnail = getThumbnailService().getCompleteThumbnail(thumbDetail);
      // first, delete former thumbnail
      if (completeThumbnail != null) {
        if (completeThumbnail.getCropFileName() != null) {
          deleteThumbnailFileOnServer(
              completeThumbnail.getInstanceId(), completeThumbnail.getCropFileName());
        }
        getThumbnailService().deleteThumbnail(thumbDetail);
      }
      thumbDetail.setCropFileName(null);
      thumbDetail.setXLength(-1);
      thumbDetail.setXStart(-1);
      thumbDetail.setYLength(-1);
      thumbDetail.setYStart(-1);
      getThumbnailService().createThumbnail(thumbDetail);
    } catch (Exception e) {
      throw new ThumbnailRuntimeException(
          "ThumbnailController.updateThumbnail()",
          SilverpeasRuntimeException.ERROR,
          "thumbnail_MSG_UPDATE_THUMBNAIL_KO",
          e);
    }
  }
  public static void copyThumbnail(ForeignPK fromPK, ForeignPK toPK) {
    ThumbnailDetail vignette =
        ThumbnailController.getCompleteThumbnail(
            new ThumbnailDetail(
                fromPK.getInstanceId(),
                Integer.parseInt(fromPK.getId()),
                ThumbnailDetail.THUMBNAIL_OBJECTTYPE_PUBLICATION_VIGNETTE));
    try {
      if (vignette != null) {
        ThumbnailDetail thumbDetail =
            new ThumbnailDetail(
                toPK.getInstanceId(),
                Integer.valueOf(toPK.getId()),
                ThumbnailDetail.THUMBNAIL_OBJECTTYPE_PUBLICATION_VIGNETTE);
        if (vignette.getOriginalFileName().startsWith("/")) {
          thumbDetail.setOriginalFileName(vignette.getOriginalFileName());
          thumbDetail.setMimeType(vignette.getMimeType());
        } else {
          String from = getImageDirectory(fromPK.getInstanceId()) + vignette.getOriginalFileName();

          String type = FilenameUtils.getExtension(vignette.getOriginalFileName());
          String newOriginalImage = String.valueOf(System.currentTimeMillis()) + "." + type;

          String to = getImageDirectory(toPK.getInstanceId()) + newOriginalImage;
          FileRepositoryManager.copyFile(from, to);
          thumbDetail.setOriginalFileName(newOriginalImage);

          // then copy thumbnail image if exists
          if (vignette.getCropFileName() != null) {
            from = getImageDirectory(fromPK.getInstanceId()) + vignette.getCropFileName();
            type = FilenameUtils.getExtension(vignette.getCropFileName());
            String newThumbnailImage = String.valueOf(System.currentTimeMillis()) + "." + type;
            to = getImageDirectory(toPK.getInstanceId()) + newThumbnailImage;
            FileRepositoryManager.copyFile(from, to);
            thumbDetail.setCropFileName(newThumbnailImage);
          }
          thumbDetail.setMimeType(vignette.getMimeType());
          thumbDetail.setXLength(vignette.getXLength());
          thumbDetail.setYLength(vignette.getYLength());
          thumbDetail.setXStart(vignette.getXStart());
          thumbDetail.setYStart(vignette.getYStart());
        }
        getThumbnailService().createThumbnail(thumbDetail);
      }
    } catch (Exception e) {
      throw new ThumbnailRuntimeException(
          "ThumbnailController.copyThumbnail()",
          SilverpeasRuntimeException.ERROR,
          "thumbnail_CANT_COPY_THUMBNAIL",
          e);
    }
  }