static function avatar($picture, $size_data = array(200, 100, 75, 50, 32), $save_dir = '') { $image = img::open($picture); $true_size = img::info($image, 'width'); $save_name = basename($picture, pathinfo($picture, PATHINFO_EXTENSION)); if ($true_size >= $size_data[0]) { $arr = null; foreach ($size_data as $target_size) { $target_img = img::create($target_size, $target_size, 'ffffff', true); $arr = array(0, 0, 0, 0, $target_size, $target_size, $true_size, $true_size); $copy_result = img::copy($target_img, $image, 5, $arr); if ($copy_result) { img::save($target_img, $save_name . '.gif', $save_dir . $target_size . '/'); } } return true; } }
static function avatar($img, $size_data = array(100, 75, 50, 32), $save_dir = '') { $res = img::open($img); $true_size = img::info($res, 'width'); $save_name = basename($img); $save_name = substr($save_name, 0, strpos($save_name, '.')); if ($true_size >= $size_data[0]) { $arr = null; foreach ($size_data as $target_size) { $target_img = img::create($target_size, $target_size, 'ffffff', true); $arr = array(0, 0, 0, 0, $target_size, $target_size, $true_size, $true_size); $copy_res = img::copy($target_img, $res, 5, $arr); if ($copy_res) { img::save($target_img, $save_name . '_' . $target_size . '.gif', $save_dir); } } return true; } }
/** * Process images * @return string */ protected function imageFile($path) { $params = array_intersect_key($_GET, array('x' => '', 'y' => '', 'adapt' => '', 'crop' => '')); $params['path'] = $path; /* Adaptive imgs */ $resMax = 0; if (isset($params['adapt']) && isset($_COOKIE['DW']) && isset($_COOKIE['DH']) && isset($_COOKIE['DPR'])) { $resMax = max($_COOKIE['DW'], $_COOKIE['DH']) * (int) $_COOKIE['DPR']; $resMax = $params['adapt'] = ceil($resMax / 100) * 100; /* to limit amount of cached images versions */ if (isset($params['x']) && $params['x'] > $resMax) { $params['x'] = $resMax; } } $cachePath = 'var/cache/' . str_replace('/', '_', http_build_query($params, '', '_')); /* get only allowed vars and secure generated path */ if (!is_file($cachePath)) { /* if cache doesn't exists */ include 'modules/core/classes/img.php'; $img = new img($path); if (isset($params['x']) && isset($params['y'])) { if (isset($params['crop'])) { $img->crop($params['x'], $params['y']); } else { $img->resize($params['x'], $params['y']); } } elseif ($resMax > 0) { /* If there isn't x and y params we resize img to max Resolution of user's screen */ $img->resize($resMax, 9999); } $img->save($cachePath, 80); } return $cachePath; }
static function transform_image($spath, $dpath, $data) { $datas = explode(':', $data); $srcWidth = $datas[0]; $srcHeight = $datas[1]; $srcX = $datas[2]; $srcY = $datas[3]; $dstWidth = $datas[4]; $dstHeight = $datas[5]; img::load($spath); $origWidth = img::getWidth(); $origHeight = img::getHeight(); $xRatio = $origWidth / $srcWidth; $yRatio = $origHeight / $srcHeight; $srcWidth = $dstWidth * $xRatio; $srcHeight = $dstHeight * $yRatio; $srcX = $srcX * $xRatio; $srcY = $srcY * $yRatio; img::transform($srcX, $srcY, $srcWidth, $srcHeight, $dstWidth, $dstHeight); img::save($dpath); img::unload(); }
static function edit() { $img_name = post('img_name', 'title'); list($width, $height) = getimagesize($img_name); $p_width = post('p_w', 'int'); $p_height = post('p_h', 'int'); //先缩放 $res = imagecreatetruecolor($p_width, $p_height); $img = img::open($img_name); imagecopyresampled($res, $img, 0, 0, 0, 0, $p_width, $p_height, $width, $height); img::clear($img); //再裁切 $new_img = imagecreatetruecolor(post('n_w', 'int'), post('n_h', 'int')); imagecopyresampled($new_img, $res, 0, 0, post('t_x', 'int'), post('t_y', 'int'), $p_width, $p_height, $p_width, $p_height); $img_name = basename($img_name); img::save($new_img, $img_name, dc_file_create); http::json(array('error' => 0, 'info' => $img_name), true); }