Пример #1
0
  protected void storeThumbnailImages(FileVersion fileVersion, File file) throws Exception {

    ImageBag imageBag = ImageToolUtil.read(file);

    RenderedImage renderedImage = imageBag.getRenderedImage();

    storeThumbnailImages(fileVersion, renderedImage);
  }
  protected void serveTempImageFile(MimeResponse mimeResponse, InputStream tempImageStream)
      throws Exception {

    ImageBag imageBag = ImageToolUtil.read(tempImageStream);

    byte[] bytes = ImageToolUtil.getBytes(imageBag.getRenderedImage(), imageBag.getType());

    String contentType = MimeTypesUtil.getExtensionContentType(imageBag.getType());

    mimeResponse.setContentType(contentType);

    PortletResponseUtil.write(mimeResponse, bytes);
  }
Пример #3
0
  protected Image getImage(InputStream is, byte[] bytes) throws PortalException, SystemException {

    try {
      if (is != null) {
        bytes = FileUtil.getBytes(is);
      }

      ImageBag imageBag = ImageProcessorUtil.read(bytes);

      RenderedImage renderedImage = imageBag.getRenderedImage();
      String type = imageBag.getType();

      if (renderedImage == null) {
        throw new ImageTypeException();
      }

      int height = renderedImage.getHeight();
      int width = renderedImage.getWidth();
      int size = bytes.length;

      Image image = new ImageImpl();

      image.setTextObj(bytes);
      image.setType(type);
      image.setHeight(height);
      image.setWidth(width);
      image.setSize(size);

      return image;
    } catch (IOException ioe) {
      throw new SystemException(ioe);
    } finally {
      if (is != null) {
        try {
          is.close();
        } catch (IOException ioe) {
          if (_log.isWarnEnabled()) {
            _log.warn(ioe);
          }
        }
      }
    }
  }
  protected byte[] getImageBytes(HttpServletRequest request, Image image) {
    try {
      if (!PropsValues.IMAGE_AUTO_SCALE) {
        return image.getTextObj();
      }

      ImageBag imageBag = null;

      if (image.getImageId() == 0) {
        imageBag = ImageToolUtil.read(image.getTextObj());

        RenderedImage renderedImage = imageBag.getRenderedImage();

        image.setHeight(renderedImage.getHeight());
        image.setWidth(renderedImage.getWidth());
      }

      int height = ParamUtil.getInteger(request, "height", image.getHeight());
      int width = ParamUtil.getInteger(request, "width", image.getWidth());

      if ((height >= image.getHeight()) && (width >= image.getWidth())) {
        return image.getTextObj();
      }

      if (image.getImageId() != 0) {
        imageBag = ImageToolUtil.read(image.getTextObj());
      }

      RenderedImage renderedImage = ImageToolUtil.scale(imageBag.getRenderedImage(), height, width);

      return ImageToolUtil.getBytes(renderedImage, imageBag.getType());
    } catch (Exception e) {
      if (_log.isWarnEnabled()) {
        _log.warn("Error scaling image " + image.getImageId(), e);
      }
    }

    return image.getTextObj();
  }
Пример #5
0
  @Override
  protected void storeThumbnailImages(FileVersion fileVersion, File file) throws Exception {

    if (!hasThumbnail(fileVersion, THUMBNAIL_INDEX_DEFAULT)) {
      addFileToStore(
          fileVersion.getCompanyId(),
          THUMBNAIL_PATH,
          getThumbnailFilePath(fileVersion, THUMBNAIL_INDEX_DEFAULT),
          file);
    }

    if (isThumbnailEnabled(THUMBNAIL_INDEX_CUSTOM_1)
        || isThumbnailEnabled(THUMBNAIL_INDEX_CUSTOM_2)) {

      ImageBag imageBag = ImageToolUtil.read(file);

      RenderedImage renderedImage = imageBag.getRenderedImage();

      storeThumbnailImage(fileVersion, renderedImage, THUMBNAIL_INDEX_CUSTOM_1);
      storeThumbnailImage(fileVersion, renderedImage, THUMBNAIL_INDEX_CUSTOM_2);
    }
  }
  protected Image getImage(InputStream is, byte[] bytes, boolean cleanUpStream)
      throws PortalException, SystemException {

    try {
      if (is != null) {
        bytes = FileUtil.getBytes(is, -1, cleanUpStream);
      }

      if (bytes == null) {
        return null;
      }

      ImageBag imageBag = ImageToolUtil.read(bytes);

      RenderedImage renderedImage = imageBag.getRenderedImage();
      String type = imageBag.getType();

      if (renderedImage == null) {
        throw new ImageTypeException();
      }

      int height = renderedImage.getHeight();
      int width = renderedImage.getWidth();
      int size = bytes.length;

      Image image = new ImageImpl();

      image.setTextObj(bytes);
      image.setType(type);
      image.setHeight(height);
      image.setWidth(width);
      image.setSize(size);

      return image;
    } catch (IOException ioe) {
      throw new SystemException(ioe);
    }
  }
  public Properties generate(
      ServletContext servletContext,
      List<URL> imageURLs,
      String spriteRootDirName,
      String spriteFileName,
      String spritePropertiesFileName,
      String rootPath,
      int maxHeight,
      int maxWidth,
      int maxSize)
      throws IOException {

    if (imageURLs.size() < 1) {
      return null;
    }

    Collections.sort(imageURLs, new PropertyComparator("path"));

    File spriteRootDir = null;

    if (Validator.isNull(spriteRootDirName)) {
      File tempDir =
          (File) servletContext.getAttribute(JavaConstants.JAVAX_SERVLET_CONTEXT_TEMPDIR);

      spriteRootDir = new File(tempDir, SpriteProcessor.PATH);
    } else {
      spriteRootDir = new File(spriteRootDirName);
    }

    spriteRootDir.mkdirs();

    File spritePropertiesFile = new File(spriteRootDir, spritePropertiesFileName);

    File spritePropertiesParentFile = spritePropertiesFile.getParentFile();

    spritePropertiesParentFile.mkdirs();

    boolean build = false;

    long lastModified = 0;

    if (spritePropertiesFile.exists()) {
      lastModified = spritePropertiesFile.lastModified();

      URLConnection urlConnection = null;

      for (URL imageURL : imageURLs) {
        urlConnection = imageURL.openConnection();

        if ((urlConnection != null) && (urlConnection.getLastModified() > lastModified)) {

          build = true;

          break;
        }
      }
    } else {
      build = true;
    }

    if (!build) {
      String spritePropertiesString = FileUtil.read(spritePropertiesFile);

      if (Validator.isNull(spritePropertiesString)) {
        return null;
      } else {
        return PropertiesUtil.load(spritePropertiesString);
      }
    }

    List<RenderedImage> renderedImages = new ArrayList<RenderedImage>();

    Properties spriteProperties = new SortedProperties();

    float x = 0;
    float y = 0;

    URLConnection urlConnection = null;

    for (URL imageURL : imageURLs) {
      urlConnection = imageURL.openConnection();

      if ((urlConnection != null) && (urlConnection.getContentLength() > maxSize)) {

        continue;
      }

      try {
        ImageBag imageBag = ImageToolUtil.read(urlConnection.getInputStream());

        RenderedImage renderedImage = imageBag.getRenderedImage();

        int height = renderedImage.getHeight();
        int width = renderedImage.getWidth();

        if ((height <= maxHeight) && (width <= maxWidth)) {
          renderedImage = convert(renderedImage);

          renderedImage = TranslateDescriptor.create(renderedImage, x, y, null, null);

          renderedImages.add(renderedImage);

          String key = imageURL.getPath();

          int pos = key.indexOf(rootPath);

          if (pos == 0) {
            key = key.substring(rootPath.length());
          }

          String contextPath = ContextPathUtil.getContextPath(servletContext);

          key = contextPath.concat(key);

          String value = (int) y + "," + height + "," + width;

          spriteProperties.setProperty(key, value);

          y += renderedImage.getHeight();
        }
      } catch (Exception e) {
        if (_log.isWarnEnabled()) {
          _log.warn("Unable to process " + imageURL);
        }

        if (_log.isDebugEnabled()) {
          _log.debug(e, e);
        }
      }
    }

    if (renderedImages.size() <= 1) {
      renderedImages.clear();
      spriteProperties.clear();
    } else {

      // PNG

      RenderedImage renderedImage =
          MosaicDescriptor.create(
              renderedImages.toArray(new RenderedImage[renderedImages.size()]),
              MosaicDescriptor.MOSAIC_TYPE_OVERLAY,
              null,
              null,
              null,
              null,
              null);

      File spriteFile = new File(spriteRootDir, spriteFileName);

      spriteFile.mkdirs();

      ImageIO.write(renderedImage, "png", spriteFile);

      if (lastModified > 0) {
        spriteFile.setLastModified(lastModified);
      }

      ImageWorker imageWorker = new ImageWorker(renderedImage);

      imageWorker.forceIndexColorModelForGIF(true);

      // GIF

      renderedImage = imageWorker.getPlanarImage();

      spriteFile = new File(spriteRootDir, StringUtil.replace(spriteFileName, ".png", ".gif"));

      FileOutputStream fos = new FileOutputStream(spriteFile);

      try {
        ImageToolUtil.encodeGIF(renderedImage, fos);
      } finally {
        fos.close();
      }

      if (lastModified > 0) {
        spriteFile.setLastModified(lastModified);
      }
    }

    FileUtil.write(spritePropertiesFile, PropertiesUtil.toString(spriteProperties));

    if (lastModified > 0) {
      spritePropertiesFile.setLastModified(lastModified);
    }

    return spriteProperties;
  }
  protected FileEntry saveTempImageFileEntry(ActionRequest actionRequest) throws Exception {

    FileEntry tempFileEntry = null;

    InputStream tempImageStream = null;

    try {
      tempFileEntry = getTempImageFileEntry(actionRequest);

      tempImageStream = tempFileEntry.getContentStream();

      ImageBag imageBag = ImageToolUtil.read(tempImageStream);

      RenderedImage renderedImage = imageBag.getRenderedImage();

      String cropRegionJSON = ParamUtil.getString(actionRequest, "cropRegion");

      if (Validator.isNotNull(cropRegionJSON)) {
        JSONObject jsonObject = JSONFactoryUtil.createJSONObject(cropRegionJSON);

        int height = jsonObject.getInt("height");
        int width = jsonObject.getInt("width");
        int x = jsonObject.getInt("x");
        int y = jsonObject.getInt("y");

        if ((x == 0)
            && (y == 0)
            && (renderedImage.getHeight() == height)
            && (renderedImage.getWidth() == width)) {

          return tempFileEntry;
        }

        if ((height + y) > renderedImage.getHeight()) {
          height = renderedImage.getHeight() - y;
        }

        if ((width + x) > renderedImage.getWidth()) {
          width = renderedImage.getWidth() - x;
        }

        renderedImage = ImageToolUtil.crop(renderedImage, height, width, x, y);
      }

      byte[] bytes = ImageToolUtil.getBytes(renderedImage, imageBag.getType());

      ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

      File file = FileUtil.createTempFile(bytes);

      try {
        TempFileEntryUtil.deleteTempFileEntry(
            themeDisplay.getScopeGroupId(),
            themeDisplay.getUserId(),
            getTempImageFolderName(),
            getTempImageFileName(actionRequest));
      } catch (Exception e) {
      }

      return TempFileEntryUtil.addTempFileEntry(
          themeDisplay.getScopeGroupId(),
          themeDisplay.getUserId(),
          getTempImageFolderName(),
          getTempImageFileName(actionRequest),
          file,
          tempFileEntry.getMimeType());
    } catch (NoSuchFileEntryException nsfee) {
      throw new UploadException(nsfee);
    } catch (NoSuchRepositoryException nsre) {
      throw new UploadException(nsre);
    } finally {
      StreamUtil.cleanUp(tempImageStream);
    }
  }