<?php

/**
 * Utility functions for violet application
 *
 */
class violetUtil {

  public static function getSpacePath($absolute = true) {
    if ($absolute) {
      return sfConfig::get('sf_upload_dir').'/'.sfConfig::get('app_space_dir').'/';
    } else {
      return sfConfig::get('sf_upload_dir_name').'/'.sfConfig::get('app_space_dir').'/';
    }
  }

  public static function getTempPath($subDir = '', $create = false) {
    $tempPath = sfConfig::get('sf_upload_dir') .'/'. sfConfig::get('app_temp_dir') .'/'. $subDir;
    if ($create && !file_exists($tempPath)) mkdir($tempPath, 0777, true);
    return $tempPath;
  }

  public static function getContentType($fileType) {
    switch (strtolower($fileType)) {
      case 'xml':
      case 'xvl': return 'text/xml';

      case 'jpe':
      case 'jpg':
      case 'jpeg': return 'image/jpeg';
      case 'gif': return 'image/gif';
      case 'png': return 'image/png';

      case 'flv': return 'video/x-flv';
      case 'mp4': return 'video/mp4';
      case 'mp3': return 'audio/mpeg';
      case 'swf': return 'application/x-shockwave-flash';
    }
  }
   public function createThumb($pathToImage="") {

        if (is_file($pathToImage)) {
      
            $info = pathinfo($pathToImage);
            $extension = strtolower($info['extension']);
            
            if (in_array($extension, array('jpg', 'jpeg', 'png', 'gif'))) {

                switch ($extension) {
                    case 'jpg':
                        $img = imagecreatefromjpeg("{$pathToImage}");
                        break;
                    case 'jpeg':
                        $img = imagecreatefromjpeg("{$pathToImage}");
                        break;
                    case 'png':
                        $img = imagecreatefrompng("{$pathToImage}");
                        break;
                    case 'gif':
                        $img = imagecreatefromgif("{$pathToImage}");
                        break;
                    default:
                        $img = imagecreatefromjpeg("{$pathToImage}");
                }
                list($width, $height) = getimagesize($pathToImage);
                $new_width = 64;
                $new_height = 50;
                $image_p = imagecreatetruecolor($new_width, $new_height);
          
                imagecopyresampled($image_p, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                $thumnail = $info['dirname']."/".$info['filename']."_thumb.jpg";
                imagejpeg($image_p,$thumnail, 30);
                return str_replace($_SERVER['DOCUMENT_ROOT'], "", $thumnail);
            } else {
                return "";
            }
        } else {
            return "";
        }
       
        return sfView::NONE;
    }
}
