Exemplo n.º 1
0
    //Check if the file exists
    if (!file_exists($file)) {
        throw new Exception("Error: File at path {$file} does not exist.");
    }
    //Check to make sure the file is an image of the appropriate extension.
    if (!(preg_match('/.png/i', $file) || preg_match('/.jpg/i', $file) || preg_match('/.jpeg/i', $file))) {
        throw new Exception("Error: Invalid file type for {$file}. Must be jpeg or png");
    }
    // Main logic
    $f_name = basename($file);
    echo "Processing " . $f_name . "....\n\n";
    $path_info = pathinfo($file);
    $image = $path_info['extension'] == 'png' ? imagecreatefrompng($file) : imagecreatefromjpeg($file);
    $hex_array = get_color_values($image);
    $top_five = get_dominant_five($hex_array);
    $json_obj = get_img_info($f_name, $top_five);
    echo $json_obj . "\n\n";
}
// Returns an array representing all of the pixels and their respective #hex values.
function get_color_values($image)
{
    $image_x = imagesx($image);
    $image_y = imagesy($image);
    $hex_array = array();
    for ($x = 0; $x < $image_x; $x++) {
        for ($y = 0; $y < $image_y; $y++) {
            $color = imagecolorat($image, $x, $y);
            $red = $color >> 16 & 0xff;
            $green = $color >> 8 & 0xff;
            $blue = $color & 0xff;
            $hex = rgb_to_hex(array($red, $green, $blue));
Exemplo n.º 2
0
 function resize_img()
 {
     if (!($image = $_POST["img"])) {
         $result['result_code'] = 101;
         $result['result_des'] = "图片不存在";
     } else {
         $real_img = $_SERVER['DOCUMENT_ROOT'] . $image;
         $info = get_img_info($real_img);
         if (!$info) {
             $result['result_code'] = 102;
             $result['result_des'] = $image;
         } else {
             $max_width = 440;
             if ($info['type'] == 'jpg' || $info['type'] == 'jpeg') {
                 $im = imagecreatefromjpeg($real_img);
             }
             if ($info['type'] == 'gif') {
                 $im = imagecreatefromgif($real_img);
             }
             if ($info['type'] == 'png') {
                 $im = imagecreatefrompng($real_img);
             }
             if ($info['width'] <= $max_width) {
                 $rate = 1;
             } else {
                 $rate = $info['width'] / $max_width;
                 if ($info['width'] > $info['height']) {
                     $max_height = intval($info['height'] / ($info['width'] / $max_width));
                 } else {
                     $max_width = intval($info['width'] / ($info['height'] / $max_height));
                 }
             }
             $x = $_POST["x"];
             $y = $_POST["y"];
             $w = $_POST["w"];
             $h = $_POST["h"];
             $width = $srcWidth = $info['width'];
             $height = $srcHeight = $info['height'];
             $type = empty($type) ? $info['type'] : $type;
             $type = strtolower($type);
             unset($info);
             //创建缩略图
             if ($type != 'gif' && function_exists('imagecreatetruecolor')) {
                 $thumbImg = imagecreatetruecolor($width, $height);
             } else {
                 $thumbImg = imagecreate($width, $height);
             }
             // 复制图片
             if (function_exists("imagecopyresampled")) {
                 imagecopyresampled($thumbImg, $im, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);
             } else {
                 imagecopyresized($thumbImg, $im, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);
             }
             if ('gif' == $type || 'png' == $type) {
                 $background_color = imagecolorallocate($thumbImg, 0, 255, 0);
                 imagecolortransparent($thumbImg, $background_color);
             }
             // 对jpeg图形设置隔行扫描
             if ('jpg' == $type || 'jpeg' == $type) {
                 imageinterlace($thumbImg, 1);
             }
             if (!is_dir(get_save_path() . "emp_pic/")) {
                 mkdir(get_save_path() . "emp_pic/", 0777, true);
                 chmod(get_save_path() . "emp_pic/", 0777);
             }
             // 生成图片
             $imageFun = 'image' . ($type == 'jpg' ? 'jpeg' : $type);
             $id = $_REQUEST['id'];
             $thumbname = get_save_path() . "emp_pic/" . $id . "." . $type;
             $imageFun($thumbImg, $thumbname, 100);
             $thumbImg_120 = imagecreatetruecolor(120, 120);
             imagecopyresampled($thumbImg_120, $thumbImg, 0, 0, intval($x * $rate), intval($y * $rate), intval(120 * 1), intval(120 * 1), intval($w * $rate), intval($h * $rate));
             $imageFun($thumbImg_120, $thumbname, 100);
             imagedestroy($thumbImg);
             imagedestroy($im);
             $result['result_code'] = 1;
             $result['result_des'] = str_replace(get_save_path(), "", $thumbname);
         }
     }
     echo json_encode($result);
 }
Exemplo n.º 3
0
/**
 * 获取各种规格的图片,如果有同种规格的图片直接返回,没有将生成
 * @param string $img		// 要缩放的图片
 * @param int $width		// 是要缩放到的宽
 * @param int $height		// 要缩放到的高
 * @param string $type		// 缩放类型 thumb是缩放 merge是融图 cut是修剪,fixed是固定大小
 * @param string $default	// 如果图片为空时的默认图片
 * @return string $newName 	// 缩放后的文件名
 */
function get_img($id, $width = 100, $height = 100, $type = 'thumb', $default = '')
{
    $info = get_img_info($id);
    if (empty($info)) {
        return $default;
    }
    $img_path = 'Public' . ltrim($info['savepath'], '.') . $info['savename'];
    if (!file_exists($img_path)) {
        return $default;
    }
    $type = strtolower($type);
    $type_arr = array('thumb', 'merge', 'cut', 'fixed');
    if (!in_array($type, $type_arr)) {
        $type = 'thumb';
    }
    if (empty($width)) {
        $width = 100;
    }
    if (empty($height)) {
        $height = 100;
    }
    $dirname = dirname($img_path);
    $filename = array_shift(explode('.', $info['savename']));
    $name = $filename . '_' . $type . '_' . $width . $height . '.' . $info['ext'];
    $save = $dirname . '/' . $name;
    if (file_exists($save)) {
        return __ROOT__ . '/' . $save;
    }
    $image = new \Think\Image();
    $image->open($img_path);
    switch ($type) {
        case 'merge':
            // 生成一个缩放后填充的缩略图并保存
            $image->thumb($width, $height, \Think\Image::IMAGE_THUMB_FILLED)->save($save);
            break;
        case 'cut':
            // 生成一个缩放后填充的缩略图并保存
            $image->thumb($width, $height, \Think\Image::IMAGE_THUMB_CENTER)->save($save);
            break;
        case 'fixed':
            // 生成一个缩放后填充的缩略图并保存
            $image->thumb($width, $height, \Think\Image::IMAGE_THUMB_FIXED)->save($save);
            break;
        default:
            // 单纯的缩放
            $image->thumb($width, $height)->save($save);
            break;
    }
    return __ROOT__ . '/' . $save;
}