コード例 #1
0
    public function uploadimageAction()
    {
        $this->view->messages = $this->_flashMessenger->getMessages();
        $this->_helper->layout->disableLayout();
        $id = $this->_request->getParam('id', 0);
        $BackURL = $this->view->url(array('module' => 'publisher', 'controller' => 'book', 'action' => 'view', 'id' => $id), '', true);
        $formDataSave = $this->getRequest()->getPost();
        //Some Settings
        $ThumbSquareSize = 100;
        //Thumbnail will be 100x100
        $BigImageMaxSize = 600;
        //Image Maximum height or width
        $ThumbPrefix = "thumb_";
        //Normal thumb Prefix
        $ThumbPrefix1 = "thumb1_";
        //Normal thumb Prefix
        $ThumbPrefix2 = "thumb2_";
        //Normal thumb Prefix
        $DestinationDirectory = USER_UPLOAD_DIR;
        //Upload Directory ends with / (slash)
        $Quality = 90;
        if (!isset($_FILES['ImageFile']) || !is_uploaded_file($_FILES['ImageFile']['tmp_name'])) {
            //echo "<font color='red'>Please select image</font>";
            /* echo "<script>alert('Please select image');</script>";*/
            $this->_flashMessenger->addMessage('<div class="div-error">Please select image!</div>');
            echo $BackURL;
            exit;
        }
        // Random number for both file, will be added after image name
        $RandomNumber = rand(0, 9999999999.0);
        $ImageName = str_replace(' ', '-', strtolower($_FILES['ImageFile']['name']));
        $ImageSize = $_FILES['ImageFile']['size'];
        $TempSrc = $_FILES['ImageFile']['tmp_name'];
        $ImageType = $_FILES['ImageFile']['type'];
        //Let's use $ImageType variable to check wheather uploaded file is supported.
        //We use PHP SWITCH statement to check valid image format, PHP SWITCH is similar to IF/ELSE statements
        //suitable if we want to compare the a variable with many different values
        switch (strtolower($ImageType)) {
            case 'image/png':
                $CreatedImage = imagecreatefrompng($_FILES['ImageFile']['tmp_name']);
                break;
            case 'image/gif':
                $CreatedImage = imagecreatefromgif($_FILES['ImageFile']['tmp_name']);
                break;
            case 'image/jpeg':
            case 'image/pjpeg':
                $CreatedImage = imagecreatefromjpeg($_FILES['ImageFile']['tmp_name']);
                break;
            default:
                //echo "<font color='red'>Unsupported File!</font>";
                /*	 echo "<script>alert('Unsupported File!');</script>";*/
                $this->_flashMessenger->addMessage('<div class="div-error">Invalid File Type!</div>');
                echo $BackURL;
                exit;
        }
        //PHP getimagesize() function returns height-width from image file stored in PHP tmp folder.
        //Let's get first two values from image, width and height. list assign values to $CurWidth,$CurHeight
        list($CurWidth, $CurHeight) = getimagesize($TempSrc);
        //if($CurWidth<315 || $CurWidth>325 || $CurHeight<440 || $CurHeight>450)
        if ($CurWidth < 300 || $CurHeight < 300) {
            $this->_flashMessenger->addMessage('<div class="div-error">Invalid Image Dimension!<br /> Acceptable Width: Minimum 300<br />
			Acceptable Height: Minimum 300<br />
			</div>');
            echo $BackURL;
            exit;
        }
        //Get file extension from Image name, this will be re-added after random name
        $ImageExt = substr($ImageName, strrpos($ImageName, '.'));
        $ImageExt = str_replace('.', '', $ImageExt);
        //remove extension from filename
        $ImageName = preg_replace("/\\.[^.\\s]{3,4}\$/", "", $ImageName);
        //Construct a new image name (with random number added) for our new image.
        $NewImageName = $ImageName . '-' . $RandomNumber . '.' . $ImageExt;
        //set the Destination Image
        $thumb_DestRandImageName = $DestinationDirectory . $ThumbPrefix . $NewImageName;
        //Thumb name
        $thumb1_DestRandImageName = $DestinationDirectory . $ThumbPrefix1 . $NewImageName;
        //Thumb name
        $DestRandImageName = $DestinationDirectory . $NewImageName;
        //Name for Big Image
        //Resize image to our Specified Size by calling resizeImage function.
        if ($this->modelBooks->resizeImage($CurWidth, $CurHeight, $BigImageMaxSize, $DestRandImageName, $CreatedImage, $Quality, $ImageType, 300, 300)) {
            if (!$this->modelBooks->cropImage($CurWidth, $CurHeight, $ThumbSquareSize, $thumb_DestRandImageName, $CreatedImage, $Quality, $ImageType, 166, 208)) {
                $this->_flashMessenger->addMessage('<div class="div-error">Error Creating thumbnail for sliders!</div>');
                echo $BackURL;
                exit;
            }
            if (!$this->modelBooks->cropImage($CurWidth, $CurHeight, $ThumbSquareSize, $thumb1_DestRandImageName, $CreatedImage, $Quality, $ImageType, 140, 175)) {
                $this->_flashMessenger->addMessage('<div class="div-error">Error Creating thumbnail for catalogue and search pages!</div>');
                echo $BackURL;
                exit;
            }
            /*
            At this point we have succesfully resized and created thumbnail image
            We can render image to user's browser or store information in the database
            For demo, we are going to output results on browser.
            */
            //Get New Image Size
            list($ResizedWidth, $ResizedHeight) = getimagesize($DestRandImageName);
            /*echo $ThumbPrefix.$NewImageName;
            		echo "<br>";
            		echo $NewImageName;*/
            $imageData = array('product_id' => $formDataSave['productid'], 'image_name' => $NewImageName, 'image_name_thumb' => $ThumbPrefix . $NewImageName, 'filesize' => $ImageSize, 'status' => '1');
            $modelImage = new Publisher_Model_DbTable_BookImages();
            $imageId = $modelImage->insert($imageData);
            if ($imageId > 0) {
                //echo "<font color='green'>Image Uploaded Successfully</font>";
                //echo "<BR><BR>";
                //echo "<img src='".$this->view->serverUrl().$this->view->baseUrl()."/".USER_UPLOAD_DIR.$ThumbPrefix.$NewImageName."' height='50' width='50'>";
                $this->_flashMessenger->addMessage('<div class="div-success">Image Uploaded Successfully!</div>');
                echo $BackURL;
                exit;
            }
            /*
            // Insert info into database table!
            mysql_query("INSERT INTO myImageTable (ImageName, ThumbName, ImgPath)
            VALUES ($DestRandImageName, $thumb_DestRandImageName, 'uploads/')");
            */
        } else {
            //echo "<font color='red'>Resize Error!</font>";
            /* echo "<script>alert('Resize Error!');</script>";*/
            $this->_flashMessenger->addMessage('<div class="div-error">Resize Error!</div>');
            echo $BackURL;
            exit;
        }
    }
コード例 #2
0
ファイル: IssuesController.php プロジェクト: vmangla/evendor
    public function uploadimageAction()
    {
        $UploadConstantList = $this->modelSiteSetting->getList("site_constant='IMAGE_UPLOAD_SIZE'");
        $this->view->messages = $this->_flashMessenger->getMessages();
        $this->_helper->layout->disableLayout();
        $id = $this->_request->getParam('id', 0);
        $parentid = $this->_request->getParam('parentid', 0);
        $BackURL = $this->view->url(array('module' => 'publisher', 'controller' => 'issues', 'action' => 'view', 'id' => $id, 'parentid' => $parentid), '', true);
        $formDataSave = $this->getRequest()->getPost();
        $ThumbSquareSize = 100;
        //Thumbnail will be 200x200
        $BigImageMaxSize = 600;
        //Image Maximum height or width
        $ThumbPrefix = "thumb_";
        //Normal thumb Prefix
        $ThumbPrefix1 = "thumb1_";
        //Normal thumb Prefix
        $DestinationDirectory = USER_UPLOAD_DIR;
        //Upload Directory ends with / (slash)
        $Quality = 90;
        if (!isset($_FILES['ImageFile']) || !is_uploaded_file($_FILES['ImageFile']['tmp_name'])) {
            $this->_flashMessenger->addMessage('<div class="div-error">Please select image!</div>');
            echo $BackURL;
            exit;
        }
        $RandomNumber = rand(0, 9999999999.0);
        $ImageName = str_replace(' ', '-', strtolower($_FILES['ImageFile']['name']));
        $ImageSize = $_FILES['ImageFile']['size'];
        $TempSrc = $_FILES['ImageFile']['tmp_name'];
        $ImageType = $_FILES['ImageFile']['type'];
        $size = $ImageSize / (1024 * 1024);
        if (isset($size) && $size >= $UploadConstantList['0']['constant_value']) {
            $this->_flashMessenger->addMessage('<div class="div-error">Image size should not exceed more than ' . $UploadConstantList['0']['constant_value'] . ' MB!</div>');
            echo $BackURL;
            exit;
        }
        switch (strtolower($ImageType)) {
            case 'image/png':
                $CreatedImage = imagecreatefrompng($_FILES['ImageFile']['tmp_name']);
                break;
            case 'image/gif':
                $CreatedImage = imagecreatefromgif($_FILES['ImageFile']['tmp_name']);
                break;
            case 'image/jpeg':
            case 'image/pjpeg':
                $CreatedImage = imagecreatefromjpeg($_FILES['ImageFile']['tmp_name']);
                break;
            default:
                $this->_flashMessenger->addMessage('<div class="div-error">Invalid File Type!</div>');
                echo $BackURL;
                exit;
        }
        //PHP getimagesize() function returns height-width from image file stored in PHP tmp folder.
        //Let's get first two values from image, width and height. list assign values to $CurWidth,$CurHeight
        list($CurWidth, $CurHeight) = getimagesize($TempSrc);
        //if($CurWidth<315 || $CurWidth>325 || $CurHeight<440 || $CurHeight>450)
        if ($CurWidth < 312 || $CurHeight < 439) {
            $this->_flashMessenger->addMessage('<div class="div-error">Invalid Image Dimension!<br /> Acceptable Width: Minimum 312<br />
			Acceptable Height: Minimum 439<br />
			</div>');
            echo $BackURL;
            exit;
        }
        //Get file extension from Image name, this will be re-added after random name
        $ImageExt = substr($ImageName, strrpos($ImageName, '.'));
        $ImageExt = str_replace('.', '', $ImageExt);
        //remove extension from filename
        $ImageName = preg_replace("/\\.[^.\\s]{3,4}\$/", "", $ImageName);
        //Construct a new image name (with random number added) for our new image.
        $NewImageName = $ImageName . '-' . $RandomNumber . '.' . $ImageExt;
        //set the Destination Image
        $thumb_DestRandImageName = $DestinationDirectory . $ThumbPrefix . $NewImageName;
        //Thumb name
        $thumb1_DestRandImageName = $DestinationDirectory . $ThumbPrefix1 . $NewImageName;
        //Thumb name
        $DestRandImageName = $DestinationDirectory . $NewImageName;
        //Name for Big Image
        //Resize image to our Specified Size by calling resizeImage function.
        if ($this->modelBooks->resizeImage($CurWidth, $CurHeight, $BigImageMaxSize, $DestRandImageName, $CreatedImage, $Quality, $ImageType, 312, 439)) {
            if (!$this->modelBooks->cropImage($CurWidth, $CurHeight, $ThumbSquareSize, $thumb_DestRandImageName, $CreatedImage, $Quality, $ImageType, 166, 208)) {
                $this->_flashMessenger->addMessage('<div class="div-error">Error Creating thumbnail for sliders!</div>');
                echo $BackURL;
                exit;
            }
            if (!$this->modelBooks->cropImage($CurWidth, $CurHeight, $ThumbSquareSize, $thumb1_DestRandImageName, $CreatedImage, $Quality, $ImageType, 140, 175)) {
                $this->_flashMessenger->addMessage('<div class="div-error">Error Creating thumbnail for catalogue and search pages!</div>');
                echo $BackURL;
                exit;
            }
            /*
            At this point we have succesfully resized and created thumbnail image
            We can render image to user's browser or store information in the database
            For demo, we are going to output results on browser.
            */
            //Get New Image Size
            list($ResizedWidth, $ResizedHeight) = getimagesize($DestRandImageName);
            /*echo $ThumbPrefix.$NewImageName;
            		echo "<br>";
            		echo $NewImageName;*/
            $imageData = array('product_id' => $formDataSave['productid'], 'image_name' => $NewImageName, 'image_name_thumb' => $ThumbPrefix . $NewImageName, 'filesize' => $ImageSize, 'status' => '1');
            $modelImage = new Publisher_Model_DbTable_BookImages();
            $imageId = $modelImage->insert($imageData);
            if ($imageId > 0) {
                $this->_flashMessenger->addMessage('<div class="div-success">Image Uploaded Successfully</div>');
                echo $BackURL;
                exit;
            }
            /*
            // Insert info into database table!
            mysql_query("INSERT INTO myImageTable (ImageName, ThumbName, ImgPath)
            VALUES ($DestRandImageName, $thumb_DestRandImageName, 'uploads/')");
            */
        } else {
            $this->_flashMessenger->addMessage('<div class="div-error">Resize Error!</div>');
            echo $BackURL;
            exit;
        }
    }