<?php
class docImage extends docBase {

  public function getPreviewFileName($absolute = true) {
    $dw = $this->doc->getDocWidth();
    $dh = $this->doc->getDocHeight();
    if ($dw <= 300 && $dh <= 300) return parent::getPreviewFileName($absolute);
    return $this->getPath($absolute, $this->thumbdir).'thumbnails2/'.$this->doc->getDocFilename().'.jpg';
  }

  public function getPreview($width = 300, $height = 300) {
    return image_tag($this->getPreviewUrl(), $this->getInnerSize($width, $height));
  }

  public function onPrivateUpload($tempFile) {
    $err = new Exception('Cấu trúc ảnh đưa lên không hợp lệ, hãy kiểm tra xem ảnh này có xem được trên máy bạn hay không');

    $size = sfConversion::getImageSize($tempFile);
    $this->doc->setDocWidth($size[0]);
    $this->doc->setDocHeight($size[1]);
    $this->doc->setDocThumbnail(1);

    if (!sfConversion::SizeImage(150, 100, $tempFile, $this->getThumbFileName())) throw $err;
    else myUtility::track_upload($this->getThumbFileName(), 'thumbnails');

    if ($size[0] > 300 || $size[1] > 300) {
      if (!sfConversion::SizeImage(300, 300, $tempFile, $this->getPreviewFileName())) throw $err;
      else myUtility::track_upload($this->getPreviewFileName(), 'thumbnails');
    }
  }
}
?>
