<?php
/***************************************************************************
 *                                                                          *
 * Copyright (c) 2005 ISPG Technologies (I) Pvt Ltd. All rights reserved.   *
 *                                                                          *
 * This  is  commercial  software,  only  users  who have purchased a valid *
 * license  and  accept  to the terms of the  License Agreement can install *
 * and use this program.                                                    *
 *                                                                          *
 ****************************************************************************
 * PLEASE READ THE FULL TEXT  OF THE SOFTWARE  LICENSE   AGREEMENT  IN  THE *
 * "copyright.txt" FILE PROVIDED WITH THIS DISTRIBUTION PACKAGE.            *
 ****************************************************************************/
//
class AjaxcropController extends Controller {
    public function cropAction() {
        $firstVal     = explode('uploads/', $_POST['img_src']);
        $firstVal1    = explode('?', $firstVal[1]);
        $val          = explode('/', $firstVal1[0]);
        $image        = __CORE_PUBLIC_PATH . "/uploads/" . $firstVal1[0];
        $width        = $_POST['w'];
        $height       = $_POST['h'];
        $start_width  = $_POST['x1'];
        $start_height = $_POST['y1'];
        $x2           = $_POST['x2'];
        $y2           = $_POST['y2'];
        $scale        = 1;
        $p            = $_POST['p'];
        if ($p == 1) {
            if ($val[0] == 'product') {
                $thumb_image_name = __CORE_PUBLIC_PATH . "/uploads/product/thumb/" . $val[1];
            } else {
                $thumb_image_name = __CORE_PUBLIC_PATH . "/uploads/temp/thumb/" . $val[1];
            }
            if ($this->resizeThumbnailImage($thumb_image_name, $image, $width, $height, $start_width, $start_height, $scale)) {
                $rand = rand();
                if ($val[0] == 'product') {
                    copy(__CORE_PUBLIC_PATH . "/uploads/product/thumb/" . $val[1], __CORE_PUBLIC_PATH . "/uploads/temp/thumb/" . $rand . "_" . $val[1]);
                } else {
                    copy(__CORE_PUBLIC_PATH . "/uploads/temp/thumb/" . $val[1], __CORE_PUBLIC_PATH . "/uploads/temp/thumb/" . $rand . "_" . $val[1]);
                }
                //dummy thumb for escaping browser image cache
                $tempThumbCookie = $this->ObjCookie->setCookie('tempThumbCookie', $this->ObjView->_views_host . "/" . __DIR_PUBLIC . "/uploads/temp/thumb/" . $rand . "_" . $val[1]);
                echo $this->ObjView->_views_host . "/" . __DIR_PUBLIC . "/uploads/temp/thumb/" . $rand . "_" . $val[1];
            }
        }
    }
    public function resizeThumbnailImage($thumb_image_name, $image, $width, $height, $start_width, $start_height, $scale) {
        $newImageWidth  = ceil($width * $scale);
        $newImageHeight = ceil($height * $scale);
        $newImage       = imagecreatetruecolor($newImageWidth, $newImageHeight);
        //  $ext = strtolower(substr(basename($image), strrpos(basename($image), ".") + 1));
        $extension      = exif_imagetype($image);
        if ($extension == 1) {
            $ext = 'gif';
        } else if ($extension == 2) {
            $ext = 'jpeg';
        } else if ($extension == 3) {
            $ext = 'png';
        } else if ($extension == 6) {
            $ext = 'bmp';
        } else {
            $ext = strtolower(substr(basename($image), strrpos(basename($image), ".") + 1));
        }
        $source = "";
        if ($ext == "png") {
            $source = imagecreatefrompng($image);
        } elseif ($ext == "jpg" || $ext == "jpeg") {
            $source = imagecreatefromjpeg($image);
        } elseif ($ext == "gif") {
            $source = imagecreatefromgif($image);
        }
        imagecopyresampled($newImage, $source, 0, 0, $start_width, $start_height, $newImageWidth, $newImageHeight, $width, $height);
        if ($ext == "png" || $ext == "PNG") {
            imagepng($newImage, $thumb_image_name, 0);
        } elseif ($ext == "jpg" || $ext == "jpeg" || $ext == "JPG" || $ext == "JPEG") {
            imagejpeg($newImage, $thumb_image_name, 90);
        } elseif ($ext == "gif" || $ext == "GIF") {
            imagegif($newImage, $thumb_image_name);
        }
        chmod($thumb_image_name, 0777);
        return $thumb_image_name;
    }
}