<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of File_upload_model
 *
 * @author zmq163
 */
class File_upload_library {

    //put your code here
    public function __construct() {
        
    }
    /**
     * 
     * 
     * 
     * 
     */
    public function appLogoUploadLibrary($_app_id=NULL,$_file_name=NULL) {
        
        if (!is_dir('./miraAppLogo/' . $_app_id)) {
            mkdir('./miraAppLogo/' . $_app_id, 0777, TRUE);
        }
        $uploadedfile = $_FILES[$_file_name]['tmp_name'];
        $filename = stripslashes($_FILES[$_file_name]['name']);
        $extension = strtolower($this->getExtension($filename));
       if ($extension == "jpg" || $extension == "jpeg") {
           $src = imagecreatefromjpeg($_FILES[$_file_name]['tmp_name']);
        } else if ($extension == "png") {
            $uploadedfile = $_FILES[$_file_name]['tmp_name'];
            $src = imagecreatefrompng($uploadedfile);
        } else {
            $src = imagecreatefromgif($uploadedfile);
        }
        list($width, $height) = getimagesize($uploadedfile);
        $newwidth = 160;
        $newheight = ($height / $width) * $newwidth;
        $tmp = imagecreatetruecolor($newwidth, $newheight);
        imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        $filename = './miraAppLogo/' . $_app_id.'/'.$_FILES[$_file_name]['name'];
        if(imagejpeg($tmp, $filename, 100)){
             return array('upload_status'=>TRUE,'resource_path'=>$filename);
        }  else {
             return array('upload_status'=>FALSE);
        }
   }

    private function getExtension($str) {
        $i = strrpos($str, ".");
        if (!$i) {
            return "";
        }
        $l = strlen($str) - $i;
        $ext = substr($str, $i + 1, $l);
        return $ext;
    }

}
