コード例 #1
0
 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);
   }
 }
コード例 #2
0
  protected static void createCropThumbnailFileOnServer(
      String pathOriginalFile,
      String pathCropdir,
      String pathCropFile,
      ThumbnailDetail thumbnail,
      int thumbnailWidth,
      int thumbnailHeight) {
    try {
      // Creates folder if not exists
      File dir = new File(pathCropdir);
      if (!dir.exists()) {
        FileFolderManager.createFolder(pathCropdir);
      }
      // create empty file
      File cropFile = new File(pathCropFile);
      if (!cropFile.exists()) {
        cropFile.createNewFile();
      }

      File originalFile = new File(pathOriginalFile);
      BufferedImage bufferOriginal = ImageIO.read(originalFile);
      // crop image
      BufferedImage cropPicture =
          bufferOriginal.getSubimage(
              thumbnail.getXStart(),
              thumbnail.getYStart(),
              thumbnail.getXLength(),
              thumbnail.getYLength());
      BufferedImage cropPictureFinal =
          new BufferedImage(thumbnailWidth, thumbnailHeight, BufferedImage.TYPE_INT_RGB);
      // Redimensionnement de l'image
      Graphics2D g2 = cropPictureFinal.createGraphics();
      g2.setRenderingHint(
          RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
      g2.drawImage(cropPicture, 0, 0, thumbnailWidth, thumbnailHeight, null);
      g2.dispose();

      // save crop image
      String extension = FilenameUtils.getExtension(originalFile.getName());
      ImageIO.write(cropPictureFinal, extension, cropFile);
    } catch (Exception e) {
      SilverTrace.warn(
          "thumbnail",
          "ThumbnailController.createThumbnailFileOnServer()",
          "thumbnail_MSG_CREATE_CROP_FILE_KO",
          "originalFileName="
              + thumbnail.getOriginalFileName()
              + " cropFileName = "
              + thumbnail.getCropFileName(),
          e);
    }
  }
コード例 #3
0
  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);
    }
  }