Example #1
0
function ResizeAndCrop($img_src, $params)
{
    $ext = end(explode(".", $img_src));
    $function = returnCorrectFunction($ext);
    $image = $function($img_src);
    $width = imagesx($image);
    $height = imagesy($image);
    // Resample first
    $image_p = imagecreatetruecolor($params[0], $params[1]);
    setTransparency($image, $image_p, $ext);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $params[0], $params[1], $width, $height);
    imagedestroy($image);
    $pWidth = imagesx($image_p);
    $pHeight = imagesy($image_p);
    $viewport = imagecreatetruecolor($params[4], $params[5]);
    setTransparency($image_p, $viewport, $ext);
    imagecopy($viewport, $image_p, 0, 0, $params[2], $params[3], $params[4], $params[5]);
    imagedestroy($image_p);
    return parseImage($ext, $viewport, $file = null);
}
    $src_x = abs($_POST["imageX"]);
}
if ($_POST["imageY"] > 0) {
    $dst_y = abs($_POST["imageY"]);
} else {
    $src_y = abs($_POST["imageY"]);
}
$viewport = imagecreatetruecolor($_POST["viewPortW"], $_POST["viewPortH"]);
setTransparency($image_p, $viewport, $ext);
imagecopy($viewport, $image_p, $dst_x, $dst_y, $src_x, $src_y, $pWidth, $pHeight);
imagedestroy($image_p);
$selector = imagecreatetruecolor($_POST["selectorW"], $_POST["selectorH"]);
setTransparency($viewport, $selector, $ext);
imagecopy($selector, $viewport, 0, 0, $selectorX, $selectorY, $_POST["viewPortW"], $_POST["viewPortH"]);
$file = "uploads/o_" . $ext . time() . "." . $ext;
parseImage($ext, $selector, $file);
$fileName = $file;
// Create image for 1920x1440
$this->convert_images($fileName, 1920, 1440);
// Create image for 1680x1050
$this->convert_images($fileName, 1680, 1050);
// Create image for 1440x900
$this->convert_images($fileName, 1440, 900);
// Create image for 1366x768
$this->convert_images($fileName, 1366, 768);
// Create image for 1280x1024
$this->convert_images($fileName, 1280, 1024);
// Create image for 1024x768
$this->convert_images($fileName, 1024, 768);
// Create image for 640x480
$this->convert_images($fileName, 640, 480);
ini_set('max_execution_time', 360);
//300 seconds = 5 minutes
ini_set('max_input_time', 360);
//300 seconds = 5 minutes
// define('ENVIRONMENT', 'production');
define('ENVIRONMENT', 'testing');
header('Content-Type: application/json; charset=utf-8');
//header('Content-Type: text/html; charset=utf-8');
if (!isset($_POST['uniqueId'])) {
    $returnObject = array('status' => 'error', 'message' => 'Произошла ошибка, пожалуйста, повторите попытку позже');
    echo json_encode($returnObject);
    exit;
}
$folderPath = 'images/';
if ($_GET['action'] == 'image') {
    $parsedImage = parseImage($_POST['imageData'], $_POST['uniqueId'] . '-' . $_POST['imageName']);
    file_put_contents($folderPath . $parsedImage['name'], base64_decode($parsedImage['image']));
    $returnObject = array('status' => 'success', 'message' => 'Изображение успешно загружено');
    echo json_encode($returnObject);
    exit;
}
if (ENVIRONMENT == 'testing') {
    if ($_GET['action'] == 'data') {
        $uniqueId = $_POST['uniqueId'];
        $imagesIds = $_POST['imagesIds'];
        $imagesIdsExtensions = $_POST['imagesIdsExtensions'];
        $files = array();
        foreach ($imagesIds as $imageId) {
            $imageExtension = $imagesIdsExtensions[$imageId];
            $imageName = $imageId . '.' . $imageExtension;
            $files[] = array('name' => $imageName, 'image' => base64_encode(file_get_contents($folderPath . $_POST['uniqueId'] . '-' . $imageName)));
Example #4
0
function resize_and_crop()
{
    error_log(print_r($_POST, true));
    $uid = MooGetGPC('uid', 'integer');
    list($width, $height) = getimagesize($_POST["imageSource"]);
    $pWidth = $_POST["imageW"];
    $pHeight = $_POST["imageH"];
    $ext = end(explode(".", $_POST["imageSource"]));
    $function = returnCorrectFunction($ext);
    $image = $function($_POST["imageSource"]);
    $width = imagesx($image);
    $height = imagesy($image);
    // Resample
    $image_p = imagecreatetruecolor($pWidth, $pHeight);
    setTransparency($image, $image_p, $ext);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $pWidth, $pHeight, $width, $height);
    imagedestroy($image);
    $widthR = imagesx($image_p);
    $hegihtR = imagesy($image_p);
    if ($_POST["imageRotate"]) {
        $angle = 360 - $_POST["imageRotate"];
        $image_p = imagerotate($image_p, $angle, 0);
        $pWidth = imagesx($image_p);
        $pHeight = imagesy($image_p);
    }
    if ($pWidth > $_POST["viewPortW"]) {
        $src_x = abs(abs($_POST["imageX"]) - abs(($_POST["imageW"] - $pWidth) / 2));
        $dst_x = 0;
    } else {
        $src_x = 0;
        $dst_x = $_POST["imageX"] + ($_POST["imageW"] - $pWidth) / 2;
    }
    if ($pHeight > $_POST["viewPortH"]) {
        $src_y = abs($_POST["imageY"] - abs(($_POST["imageH"] - $pHeight) / 2));
        $dst_y = 0;
    } else {
        $src_y = 0;
        $dst_y = $_POST["imageY"] + ($_POST["imageH"] - $pHeight) / 2;
    }
    $viewport = imagecreatetruecolor($_POST["viewPortW"], $_POST["viewPortH"]);
    setTransparency($image_p, $viewport, $ext);
    imagecopy($viewport, $image_p, $dst_x, $dst_y, $src_x, $src_y, $pWidth, $pHeight);
    imagedestroy($image_p);
    $selector = imagecreatetruecolor($_POST["selectorW"], $_POST["selectorH"]);
    setTransparency($viewport, $selector, $ext);
    imagecopy($selector, $viewport, 0, 0, $_POST["selectorX"], $_POST["selectorY"], $_POST["viewPortW"], $_POST["viewPortH"]);
    $file = "tmp/test" . time() . "." . $ext;
    parseImage($ext, $selector, $file);
    imagedestroy($viewport);
    //Return value
    echo $file;
    //写水印......fanglin
    $thumb_image_name = $file;
    $first = new Imagick($thumb_image_name);
    //写入水印
    $second = new Imagick('../public/system/images/logo2.png');
    $dw = new ImagickDraw();
    $dw->setGravity(Imagick::GRAVITY_SOUTHEAST);
    //设置位置
    $dw->composite($second->getImageCompose(), 0, 0, 50, 0, $second);
    $first->drawImage($dw);
    $first->writeImage($thumb_image_name);
    chmod($thumb_image_name, 0777);
    //imagick处理.......fanglin
    $bigphoto = $thumb_image_name;
    $thuid = $uid * 3;
    $userpath = "../data/upload/userimg/";
    $jpg = $ext;
    $sizearray = array(0 => array('width' => 320, 'height' => 400), 1 => array('width' => 171, 'height' => 212), 2 => array('width' => 100, 'height' => 125), 3 => array('width' => 60, 'height' => 75));
    $namearray = array(0 => 'big', 1 => 'mid', 2 => 'medium', 3 => 'small');
    foreach ($sizearray as $k => $size) {
        $index_name = $thuid . '_' . $namearray[$k] . '.' . $jpg;
        ImagickResizeImage($bigphoto, $userpath . $index_name, $size['width'], $size['height']);
    }
    $index_name = $thuid . '_' . 'index.' . $jpg;
    $com_name = $thuid . '_' . 'com.' . $jpg;
    $page_name = $thuid . '_' . 'page.' . $jpg;
    list($width, $height) = getimagesize($bigphoto);
    $d = $width / $height;
    $c = 100 / 125;
    //100*125
    if ($d > $c) {
        $g1_width = 100;
        $b = $width / $g1_width;
        $g1_height = $height / $b;
    } else {
        $g1_height = 125;
        $b = $height / $g1_height;
        $g1_width = $width / $b;
    }
    ImagickResizeImage($bigphoto, $userpath . $index_name, $g1_width, $g1_height);
    $c = 50 / 63;
    //100*125
    if ($d > $c) {
        $g2_width = 50;
        $b = $width / $g2_width;
        $g2_height = $height / $b;
    } else {
        $g2_height = 63;
        $b = $height / $g2_height;
        $g2_width = $width / $b;
    }
    ImagickResizeImage($bigphoto, $userpath . $page_name, $g2_width, $g2_height);
    $c = 110 / 138;
    //100*125
    if ($d > $c) {
        $g3_width = 110;
        $b = $width / $g3_width;
        $g3_height = $height / $b;
    } else {
        $g3_height = 138;
        $b = $height / $g3_height;
        $g3_width = $width / $b;
    }
    ImagickResizeImage($bigphoto, $userpath . $com_name, $g3_width, $g3_height);
}
    require_once 'inc/insales_api.php';
}
header('Content-Type: application/json; charset=utf-8');
//header('Content-Type: text/html; charset=utf-8');
if (!isset($_POST['data']) || !isset($_POST['images'])) {
    $returnObject = array('status' => 'error', 'message' => 'Произошла ошибка, пожалуйста, повторите попытку позже');
    echo json_encode($returnObject);
    exit;
}
if (ENVIRONMENT == 'nothing') {
    exit;
}
$files = array();
$imagesData = $_POST['images'];
foreach ($imagesData as $imageName => $imageData) {
    $files[$imageName] = parseImage($imageData, $imageName);
}
if (ENVIRONMENT == 'testing') {
    sendEmails($testingEmails, '*****@*****.**', 'Test editor', 'Test editor', $files);
    exit;
}
if (ENVIRONMENT == 'production') {
    // data:image/jpeg;base64,
    // data:image/png;base64,
    // var_dump($_POST);
    $productId = $_POST['data']['productId'];
    $colors = $_POST['data']['colors'];
    $insales_api = insales_api_client($inSalesDomain, $inSalesApiKey, $inSalesApiPassword);
    $productAdditionalOptions = isset($_POST['data']['optionsIds']) ? $_POST['data']['optionsIds'] : array();
    $productCategoryId = 1297378;
    $productCollectionId = 2099066;