<?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']);
			error_log(date('YmdHis') .' - '.$pathToImage .' - '. $extension . "\n", 3, '/srv/www/sbg/log/thumbcreator.log');
            $imagefunc = 'imagejpeg';

            if (in_array($extension, array('jpg', 'jpeg', 'png', 'gif'))) {

                switch ($extension) {
                    case 'png':
                        $img = imagecreatefrompng("{$pathToImage}");
                        $imagefunc = 'imagepng';
                        break;
                    case 'gif':
                        $img = imagecreatefromgif("{$pathToImage}");
                        $imagefunc = 'imagegif';
                        break;
                    case 'jpg':
                    case 'jpeg':
                    default:
                        $img = imagecreatefromjpeg("{$pathToImage}");
                        $imagefunc = 'imagejpeg';
                        break;
                }

                list($width, $height) = getimagesize($pathToImage);

                $max_width = 64;
                $max_height = 50;
                $ratio = $max_width/$max_height;
                $cropped_width = $height/$ratio;
                $cropped_height = $width/$ratio;

                if ($width < $height) {
                	$new_height = $max_height;
                	$new_width = $width * ($max_height/$height);
                }
                else if ($width > $height) {
                	$new_width = $max_width;
                	$new_height = $height * ($max_width/$width);
                }

                $image_p = imagecreatetruecolor($new_width, $new_height);

                imagecopyresampled($image_p, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

                $thumnail = $info['dirname'].DIRECTORY_SEPARATOR.$info['filename'].'_thumb.'.$extension;

                if ($imagefunc == 'imagegif' || $imagefunc == 'imagepng' ) {
                	call_user_func($imagefunc, $image_p, $thumnail);
                }
                else {
                	call_user_func($imagefunc, $image_p, $thumnail, 30);
                }

                return str_replace($_SERVER['DOCUMENT_ROOT'], "", $thumnail);
            } else {
                return "";
            }
        } else {
            return "";
        }

        return sfView::NONE;
    }
}
