public Image updateImage(long imageId, byte[] bytes, String type, int height, int width, int size)
      throws PortalException, SystemException {

    Image image = imagePersistence.fetchByPrimaryKey(imageId);

    if (image == null) {
      image = imagePersistence.create(imageId);
    }

    image.setModifiedDate(new Date());
    image.setType(type);
    image.setHeight(height);
    image.setWidth(width);
    image.setSize(size);

    Hook hook = HookFactory.getInstance();

    hook.updateImage(image, type, bytes);

    imagePersistence.update(image, false);

    WebServerServletTokenUtil.resetToken(imageId);

    return image;
  }
  /**
   * Converts the soap model instance into a normal model instance.
   *
   * @param soapModel the soap model instance to convert
   * @return the normal model instance
   */
  public static Image toModel(ImageSoap soapModel) {
    Image model = new ImageImpl();

    model.setImageId(soapModel.getImageId());
    model.setModifiedDate(soapModel.getModifiedDate());
    model.setText(soapModel.getText());
    model.setType(soapModel.getType());
    model.setHeight(soapModel.getHeight());
    model.setWidth(soapModel.getWidth());
    model.setSize(soapModel.getSize());

    return model;
  }
  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();
  }
  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);
    }
  }