<commit_before><?php
/**
 * Created by PhpStorm.
 * User: Nigel
 * Date: 2015/12/9
 * Time: 9:32
 */
/**
 * 调用微软人脸识别API
 * @param $url
 * @return array 失败返回false
 */
function detect($url) {
    $faceKey    = "<KEY>";
    $requestUrl = "https://api.projectoxford.ai/face/v0/detections?analyzesFaceLandmarks=true&analyzesAge=true&analyzesGender=true";
    $data       = array("url" => $url);
    $data       = json_encode($data);
    $header     = array(
        'Content-Type:application/json',
        'Ocp-Apim-Subscription-Key:' . $faceKey,
        'Content-Length:' . strlen($data));
    try {
        $ch = curl_init();
        if (false === $ch) {
            throw new Exception('failed to initialize');
        }

        curl_setopt($ch, CURLOPT_URL, $requestUrl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);

        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        //信任任何ssl证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $output = curl_exec($ch);
        if (false === $output) {
            throw new Exception(curl_error($ch), curl_errno($ch));
        }

        $output = json_decode($output, true);

        if (!isset($output['code'])) {
            return $output;
        }
        // ...process $content now
    } catch (Exception $e) {

        trigger_error(sprintf(
                          'Curl failed with error #%d: %s',
                          $e->getCode(), $e->getMessage()),
                      E_USER_ERROR);

    }

    return false;
}

/**
 * 在图片上画出矩形，并返回保存之后的URL
 * @param $url       string 图片的URL
 * @param $rectangle array 微软返回的识别数据加上性别
 * @return string 返回保存后的图片URL
 */
function draw($url, &$rectangle) {
    $img = imagecreatefromstring(getImg($url));

    if (!is_resource($img)) {
        sae_log("img不是资源文件");
    }

    foreach ($rectangle as &$rec) {
        drawRec($rec, $img);
    }
    $filename = str_replace("/", "", parse_url($url, PHP_URL_PATH)) . ".jpg";
    //just for test
    //    $stor = new SaeStorage("n353jmy031","zwwkm3wjxmmkxkhwzlyjhxz3lh2xkyj3zhx014lh");
    //imagepng这样的函数不支持wrapper,用临时文件解决
    $bool = imagejpeg($img, SAE_TMP_PATH . $filename);//保存为临时文件
//    sae_log("保存的文件名：" . $filename);
    file_put_contents("saestor://wechatimg/$filename",
                      file_get_contents(SAE_TMP_PATH . $filename));
    //    $bool = imagejpeg($img, "saestor://wechatimg/$filename");
    if (!$bool) {
        sae_log("保存文件失败");
    }
    $stor = new SaeStorage();

    return $stor->getUrl("wechatimg", $filename);
}


function drawRec(&$rec, &$img) {

    $x1     = $rec['left'];
    $y1     = $rec['top'];
    $width  = $rec['width'];
    $height = $rec['height'];
    $gender = $rec['gender'];
    $x2     = $x1 + $width;
    $y2     = $y1 + $height;
    $points = array(
        'pupilLeft'  => $rec['pupilLeft'],
        'pupilRight' => $rec['pupilRight'],
        'nostTip'    => $rec['noseTip'],
        'mouthLeft'  => $rec['mouthLeft'],
        'mouthRight' => $rec['mouthRight']);

    $color_male   = imagecolorallocate($img, 13, 163, 238);
    $color_female = imagecolorallocate($img, 186, 11, 147);
    $color        = ($gender == "male") ? $color_male : $color_female;
    //设置笔画的粗细
    imagesetthickness($img, 3);
    //画一个矩形
    $bool = imagerectangle($img, $x1, $y1, $x2, $y2, $color);
    //画点
    imagesetthickness($img, 8);
    foreach ($points as $point) {
        $bool = imageline($img, $point['x'], $point['y'], $point['x'] - 1, $point['y'] - 1, $color);
    }

    if (!$bool) {
        sae_log("画图失败");
    }
}

//欺骗浏览器，输出图片<commit_msg>reference<commit_after><?php
/**
 * Created by PhpStorm.
 * User: Nigel
 * Date: 2015/12/9
 * Time: 9:32
 */
/**
 * 调用微软人脸识别API
 * @param $url
 * @return array 失败返回false
 */
function detect($url) {
    $faceKey    = "<KEY>";
    $requestUrl = "https://api.projectoxford.ai/face/v0/detections?analyzesFaceLandmarks=true&analyzesAge=true&analyzesGender=true";
    $data       = array("url" => $url);
    $data       = json_encode($data);
    $header     = array(
        'Content-Type:application/json',
        'Ocp-Apim-Subscription-Key:' . $faceKey,
        'Content-Length:' . strlen($data));
    try {
        $ch = curl_init();
        if (false === $ch) {
            throw new Exception('failed to initialize');
        }

        curl_setopt($ch, CURLOPT_URL, $requestUrl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);

        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        //信任任何ssl证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $output = curl_exec($ch);
        if (false === $output) {
            throw new Exception(curl_error($ch), curl_errno($ch));
        }

        $output = json_decode($output, true);

        if (!isset($output['code'])) {
            return $output;
        }
        // ...process $content now
    } catch (Exception $e) {

        trigger_error(sprintf(
                          'Curl failed with error #%d: %s',
                          $e->getCode(), $e->getMessage()),
                      E_USER_ERROR);

    }

    return false;
}

/**
 * 在图片上画出矩形，并返回保存之后的URL
 * @param $url       string 图片的URL
 * @param $rectangle array 微软返回的识别数据加上性别
 * @return string 返回保存后的图片URL
 */
function draw($url, &$rectangle) {
    $img = imagecreatefromstring(getImg($url));

    if (!is_resource($img)) {
        sae_log("img不是资源文件");
    }

    foreach ($rectangle as &$rec) {
        drawRec($rec, $img);
    }
    $filename = str_replace("/", "", parse_url($url, PHP_URL_PATH)) . ".jpg";
    //just for test
    //    $stor = new SaeStorage("n353jmy031","zwwkm3wjxmmkxkhwzlyjhxz3lh2xkyj3zhx014lh");
    //imagepng这样的函数不支持wrapper,用临时文件解决
    $bool = imagejpeg($img, SAE_TMP_PATH . $filename);//保存为临时文件
    //    sae_log("保存的文件名：" . $filename);
    file_put_contents("saestor://wechatimg/$filename",
                      file_get_contents(SAE_TMP_PATH . $filename));
    //    $bool = imagejpeg($img, "saestor://wechatimg/$filename");
    if (!$bool) {
        sae_log("保存文件失败");
    }
    $stor = new SaeStorage();

    return $stor->getUrl("wechatimg", $filename);
}


function drawRec(&$rec, &$img) {

    $x1     = $rec['left'];
    $y1     = $rec['top'];
    $width  = $rec['width'];
    $height = $rec['height'];
    $gender = $rec['gender'];
    $x2     = $x1 + $width;
    $y2     = $y1 + $height;
    $points = array(
        'pupilLeft'  => $rec['pupilLeft'],
        'pupilRight' => $rec['pupilRight'],
        'nostTip'    => $rec['noseTip'],
        'mouthLeft'  => $rec['mouthLeft'],
        'mouthRight' => $rec['mouthRight']);

    $color_male   = imagecolorallocate($img, 13, 163, 238);
    $color_female = imagecolorallocate($img, 186, 11, 147);
    $color        = ($gender == "male") ? $color_male : $color_female;
    //设置笔画的粗细
    imagesetthickness($img, 3);
    //画一个矩形
    $bool = imagerectangle($img, $x1, $y1, $x2, $y2, $color);
    //画点
    foreach ($points as $point) {
        $bool = imagerectangle($img, $point['x'] - 2, $point['y'] - 2, $point['x'] + 2, $point['y'] + 2, $color);
    }

    if (!$bool) {
        sae_log("画图失败");
    }
}

//欺骗浏览器，输出图片
{'fraction_non_alphanumeric': 0.17849815346737793, 'fraction_numerical': 0.015456161947749967, 'mean_word_length': 2.453944260746339, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2311342', 'n_tokens_mistral': 2873, 'n_tokens_neox': 2769, 'n_words': 467}