function getThumbImage($filename, $width = 100, $height = 'auto', $type = 0, $replace = false) { $UPLOAD_URL = ''; $UPLOAD_PATH = ''; $filename = str_ireplace($UPLOAD_URL, '', $filename); //将URL转化为本地地址 $info = pathinfo($filename); $oldFile = $info['dirname'] . DIRECTORY_SEPARATOR . $info['filename'] . '.' . $info['extension']; $thumbFile = $info['dirname'] . DIRECTORY_SEPARATOR . $info['filename'] . '_' . $width . '_' . $height . '.' . $info['extension']; $oldFile = str_replace('\\', '/', $oldFile); $thumbFile = str_replace('\\', '/', $thumbFile); $filename = ltrim($filename, '/'); $oldFile = ltrim($oldFile, '/'); $thumbFile = ltrim($thumbFile, '/'); //兼容SAE的中心裁剪缩略 if (strtolower(C('PICTURE_UPLOAD_DRIVER')) == 'sae') { $storage = new SaeStorage(); $thumbFilePath = str_replace(C('UPLOAD_SAE_CONFIG.rootPath'), '', $thumbFile); if (!$storage->fileExists(C('UPLOAD_SAE_CONFIG.domain'), $thumbFilePath)) { $f = new SaeFetchurl(); $img_data = $f->fetch($oldFile); $img = new SaeImage(); $img->setData($img_data); $info_img = $img->getImageAttr(); if ($height == "auto") { $height = $info_img[1] * $height / $info_img[0]; } $w = $info_img[0]; $h = $info_img[1]; /* 居中裁剪 */ //计算缩放比例 $w_scale = $width / $w; if ($w_scale > 1) { $w_scale = 1 / $w_scale; } $h_scale = $height / $h; if ($h_scale > $w_scale) { //按照高来放缩 $x1 = (1 - 1.0 * $width * $h / $w / $height) / 2; $x2 = 1 - $x1; $img->crop($x1, $x2, 0, 1); $img_temp = $img->exec(); $img1 = new SaeImage(); $img1->setData($img_temp); $img1->resizeRatio($h_scale); } else { $y1 = (1 - 1 * 1.0 / ($width * $h / $w / $height)) / 2; $y2 = 1 - $y1; $img->crop(0, 1, $y1, $y2); $img_temp = $img->exec(); $img1 = new SaeImage(); $img1->setData($img_temp); $img1->resizeRatio($w_scale); } $img1->improve(); $new_data = $img1->exec(); // 执行处理并返回处理后的二进制数据 if ($new_data === false) { return $oldFile; } // 或者可以直接输出 $thumbed = $storage->write(C('UPLOAD_SAE_CONFIG.domain'), $thumbFilePath, $new_data); $info['width'] = $width; $info['height'] = $height; $info['src'] = $thumbed; //图片处理失败时输出错误码和错误信息 } else { $info['width'] = $width; $info['height'] = $height; $info['src'] = $storage->getUrl(C('UPLOAD_SAE_CONFIG.domain'), $thumbFilePath); } return $info; } //原图不存在直接返回 if (!file_exists($UPLOAD_PATH . $oldFile)) { @unlink($UPLOAD_PATH . $thumbFile); $info['src'] = $oldFile; $info['width'] = intval($width); $info['height'] = intval($height); return $info; //缩图已存在并且 replace替换为false } elseif (file_exists($UPLOAD_PATH . $thumbFile) && !$replace) { $imageinfo = getimagesize($UPLOAD_PATH . $thumbFile); $info['src'] = $thumbFile; $info['width'] = intval($imageinfo[0]); $info['height'] = intval($imageinfo[1]); return $info; //执行缩图操作 } else { $oldimageinfo = getimagesize($UPLOAD_PATH . $oldFile); $old_image_width = intval($oldimageinfo[0]); $old_image_height = intval($oldimageinfo[1]); if ($old_image_width <= $width && $old_image_height <= $height) { @unlink($UPLOAD_PATH . $thumbFile); @copy($UPLOAD_PATH . $oldFile, $UPLOAD_PATH . $thumbFile); $info['src'] = $thumbFile; $info['width'] = $old_image_width; $info['height'] = $old_image_height; return $info; } else { if ($height == "auto") { $height = $old_image_height * $width / $old_image_width; } if ($width == "auto") { $width = $old_image_width * $width / $old_image_height; } if (intval($height) == 0 || intval($width) == 0) { return 0; } require_once 'ThinkPHP/Library/Vendor/phpthumb/PhpThumbFactory.class.php'; $thumb = PhpThumbFactory::create($UPLOAD_PATH . $filename); if ($type == 0) { $thumb->adaptiveResize($width, $height); } else { $thumb->resize($width, $height); } $res = $thumb->save($UPLOAD_PATH . $thumbFile); $info['src'] = $UPLOAD_PATH . $thumbFile; $info['width'] = $old_image_width; $info['height'] = $old_image_height; return $info; //内置库缩略 /* $image = new \Think\Image(); $image->open($UPLOAD_PATH . $filename); //dump($image);exit; $image->thumb($width, $height, $type); $image->save($UPLOAD_PATH . $thumbFile); //缩图失败 if (!$image) { $thumbFile = $oldFile; } $info['width'] = $width; $info['height'] = $height; $info['src'] = $thumbFile; return $info;*/ } } }
function avatar_update() { if (!empty($_SESSION['avatar'])) { $targ_w = intval($_POST['w']); $targ_h = intval($_POST['h']); $x = $_POST['x']; $y = $_POST['y']; $jpeg_quality = 90; $avatar = $_SESSION['avatar']; $avatar_dir = C("UPLOADPATH") . "avatar/"; if (sp_is_sae()) { //TODO 其它存储类型暂不考虑 $src = C("TMPL_PARSE_STRING.__UPLOAD__") . "avatar/{$avatar}"; } else { $src = $avatar_dir . $avatar; } $avatar_path = $avatar_dir . $avatar; if (sp_is_sae()) { //TODO 其它存储类型暂不考虑 $img_data = sp_file_read($avatar_path); $img = new \SaeImage(); $size = $img->getImageAttr(); $lx = $x / $size[0]; $rx = $x / $size[0] + $targ_w / $size[0]; $ty = $y / $size[1]; $by = $y / $size[1] + $targ_h / $size[1]; $img->crop($lx, $rx, $ty, $by); $img_content = $img->exec('png'); sp_file_write($avatar_dir . $avatar, $img_content); } else { $image = new \Think\Image(); $image->open($src); $image->crop($targ_w, $targ_h, $x, $y); $image->save($src); } $userid = sp_get_current_userid(); $result = $this->users_model->where(array("id" => $userid))->save(array("avatar" => $avatar)); $_SESSION['user']['avatar'] = $avatar; if ($result) { $this->success("头像更新成功!"); } else { $this->error("头像更新失败!"); } } }
public function cropPicture($uid, $crop = null, $ext = 'jpg') { //如果不裁剪,则发生错误 if (!$crop) { $this->error('必须裁剪'); } $path = "/Uploads/Avatar/" . $uid . "/original." . $ext; //获取文件路径 $fullPath = substr($path, 0, 1) == '/' ? '.' . $path : $path; $savePath = str_replace('original', 'crop', $fullPath); $returnPath = str_replace('original', 'crop', $path); $returnPath = substr($returnPath, 0, 1) == '/' ? '.' . $returnPath : $returnPath; //解析crop参数 $crop = explode(',', $crop); $x = $crop[0]; $y = $crop[1]; $width = $crop[2]; $height = $crop[3]; //是sae则不需要获取全路径 if (strtolower(C('PICTURE_UPLOAD_DRIVER')) == 'local') { //本地环境 $image = ImageWorkshop::initFromPath($fullPath); //生成将单位换算成为像素 $x = $x * $image->getWidth(); $y = $y * $image->getHeight(); $width = $width * $image->getWidth(); $height = $height * $image->getHeight(); //如果宽度和高度近似相等,则令宽和高一样 if (abs($height - $width) < $height * 0.01) { $height = min($height, $width); $width = $height; } //调用组件裁剪头像 $image = ImageWorkshop::initFromPath($fullPath); $image->crop(ImageWorkshopLayer::UNIT_PIXEL, $width, $height, $x, $y); $image->save(dirname($savePath), basename($savePath)); //返回新文件的路径 return $returnPath; } elseif (strtolower(C('PICTURE_UPLOAD_DRIVER')) == 'sae') { //sae //载入临时头像 $f = new \SaeFetchurl(); $img_data = $f->fetch($fullPath); $img = new \SaeImage(); $img->setData($img_data); $img_attr = $img->getImageAttr(); //生成将单位换算成为像素 $x = $x * $img_attr[0]; $y = $y * $img_attr[1]; $width = $width * $img_attr[0]; $height = $height * $img_attr[1]; //如果宽度和高度近似相等,则令宽和高一样 if (abs($height - $width) < $height * 0.01) { $height = min($height, $width); $width = $height; } $img->crop($x / $img_attr[0], ($x + $width) / $img_attr[0], $y / $img_attr[1], ($y + $height) / $img_attr[1]); $new_data = $img->exec(); $storage = new \SaeStorage(); $thumbFilePath = str_replace(C('UPLOAD_SAE_CONFIG.rootPath'), '', dirname($savePath) . '/' . basename($savePath)); $thumbed = $storage->write(C('UPLOAD_SAE_CONFIG.domain'), $thumbFilePath, $new_data); //返回新文件的路径 return $thumbed; } elseif (strtolower(C('PICTURE_UPLOAD_DRIVER')) == 'qiniu') { $imageInfo = file_get_contents($fullPath . '?imageInfo'); $imageInfo = json_decode($imageInfo); //生成将单位换算成为像素 $x = $x * $imageInfo->width; $y = $y * $imageInfo->height; $width = $width * $imageInfo->width; $height = $height * $imageInfo->height; $new_img = $fullPath . '?imageMogr2/crop/!' . $width . 'x' . $height . 'a' . $x . 'a' . $y; //返回新文件的路径 return $new_img; } }
static function clip_topic($width, $height, $pic, $location = 'center', $domain) { $s = new SaeStorage(); $img = new SaeImage(); $w = $width; $h = $height; if (!($img_data = $s->read($domain, $pic))) { echo $domain . "&&&&" . $pic; die('图片读取失败!'); } $img->setData($img_data); //获取预设剪裁高宽比 $x2 = $h / $w; if (!($image_size = $img->getImageAttr())) { die('获取属性失败!'); } //图片实际高宽比 $x1 = $image_size[1] / $image_size[0]; //图片实际高宽比和预设高宽比之比 $x12 = $x1 / $x2; if ($x12 < 1) { //如果实际图片比预设值高 if ($location == 'center') { //用halb值决定其剪裁位置 $halb = 0.5 - $x12 / 2; } elseif ($location == 'right') { $halb = 1 - $x12; } else { $halb = 0; } $img->resize(0, $h); //按预设高度等比缩小 $img->crop($halb, $x12 + $halb, null, null); //图片居中预设宽度剪裁 } else { //如果实际图片比预设值宽 $x21 = 1 / $x12; //将比例反转 if ($location == 'center') { //用halb值决定其剪裁位置 $halb = 0.5 - $x21 / 2; } elseif ($location == 'right') { $halb = 1 - $x12; } else { $halb = 0; } $img->resize($w); //按预设宽度等比缩小 $img->crop(null, null, $halb, $x21 + $halb); //图片中间预设高度剪裁 } if (!$img->improve()) { die('图片优化失败!'); } $result = $img->exec(); if ($result == false) { echo "剪切失败!"; var_dump($img->errno(), $img->errmsg()); } else { return $result; } }
private function cropAvatar($path, $crop = null) { //如果不裁剪,则发生错误 if (!$crop) { $this->error = '必须裁剪'; return false; } //是sae则不需要获取全路径 if (strtolower(APP_MODE) != 'sae') { //本地环境 //获取头像的文件路径 $fullPath = $this->getFullPath($path); //生成文件名后缀 $postfix = substr(md5($crop), 0, 8); $savePath = preg_replace('/\\.[a-zA-Z0-9]*$/', '-' . $postfix . '$0', $fullPath); $returnPath = preg_replace('/\\.[a-zA-Z0-9]*$/', '-' . $postfix . '$0', $path); //解析crop参数 $crop = explode(',', $crop); $x = $crop[0]; $y = $crop[1]; $width = $crop[2]; $height = $crop[3]; //载入临时头像 $image = ImageWorkshop::initFromPath($fullPath); //如果宽度和高度近似相等,则令宽和高一样 if (abs($height - $width) < $height * 0.01) { $height = min($height, $width); $width = $height; } else { $this->error = $height . '图像必须为正方形'; return false; } //确认头像足够大 if ($height < 256) { $this->error = '头像太小'; return false; } //调用组件裁剪头像 $image = ImageWorkshop::initFromPath($fullPath); $image->crop(ImageWorkshopLayer::UNIT_PIXEL, $width, $height, $x, $y); $image->save(dirname($savePath), basename($savePath)); //返回新文件的路径 return $returnPath; } else { //sae $fullPath = $path; $postfix = substr(md5($crop), 0, 8); $savePath = preg_replace('/\\.[a-zA-Z0-9]*$/', '-' . $postfix . '$0', $fullPath); $returnPath = preg_replace('/\\.[a-zA-Z0-9]*$/', '-' . $postfix . '$0', $path); //解析crop参数 $crop = explode(',', $crop); $x = $crop[0]; $y = $crop[1]; $width = $crop[2]; $height = $crop[3]; //载入临时头像 $f = new \SaeFetchurl(); $img_data = $f->fetch($fullPath); $img = new \SaeImage(); $img->setData($img_data); $img_attr = $img->getImageAttr(); //生成将单位换算成为像素 $x = $x * $img_attr[0]; $y = $y * $img_attr[1]; $width = $width * $img_attr[0]; $height = $height * $img_attr[1]; //如果宽度和高度近似相等,则令宽和高一样 if (abs($height - $width) < $height * 0.01) { $height = min($height, $width); $width = $height; } else { $this->error = '图像必须为正方形'; return false; } //确认头像足够大 if ($height < 128) { $this->error = '头像太小'; return false; } /* dump('x='.$x); dump('y='.$y); dump('w='.$width); dump('h='.$height);exit;*/ $img->crop($x / $img_attr[0], ($x + $width) / $img_attr[0], $y / $img_attr[1], ($y + $height) / $img_attr[1]); $new_data = $img->exec(); $storage = new \SaeStorage(); $thumbFilePath = str_replace(C('UPLOAD_SAE_CONFIG.rootPath'), '', dirname($savePath) . '/' . basename($savePath)); $thumbed = $storage->write(C('UPLOAD_SAE_CONFIG.domain'), $thumbFilePath, $new_data); //返回新文件的路径 return $thumbed; } }
function thumb_url($stor, $file, $size = '200') { global $storage; if ($storage->fileExists($stor, $size . '/' . $file)) { return $storage->getUrl($stor, $size . '/' . $file); } elseif ($storage->fileExists($stor, 'original/' . $file)) { // 读取原始图片信息 $img_data = $storage->read($stor, 'original/' . $file); $img_url = $storage->getUrl($stor, 'original/' . $file); //$img_info = getimagesize($img_url); // 生成缩略图 $img = new SaeImage(); $img->setData($img_data); $img_info = $img->getImageAttr(); $img->resize(200); $resizeRa = 200 / $img_info[0]; $cropYend = $img_info[1] * $resizeRa; if ($cropYend > 150) { $cropy = 150 / $cropYend; $img->crop(0, 1, 0, $cropy); } $storage->write($stor, $size . '/' . $file, $img->exec()); $img->clean(); return $storage->getUrl($stor, $size . '/' . $file); } else { return false; } }
public function actionUpdateHeadimg() { $model = $this->loadModel(Yii::app()->user->id); if (isset($_POST['Member'])) { $model->headimg = CUploadedFile::getInstance($model, 'headimg'); $this->performAjaxValidation($model, array('headimg')); if ($model->validate(array('headimg'))) { if (empty($model->headimg)) { header('Content-Type: text/html; charset=utf-8;'); echo "<script language='javascript'>alert('上传文件不能为空');window.location.href='updateHeadimg';</script>"; exit; } list($usec, $sec) = explode(" ", microtime()); $usec = substr($usec, 2, 2); $newFileName = 'headimg' . $sec . $usec . '.jpg'; $uploadimage = new SaeStorage(); $uploadimage->upload('headimg', $newFileName, $model->headimg->tempName); $newFile = "http://918s-headimg.stor.sinaapp.com/" . $newFileName; $f = new SaeFetchurl(); $img_data = $f->fetch($newFile); $img = new SaeImage(); $img->setData($img_data); if ($_POST['w'] && $_POST['h']) { //用户选中截图工具,按照对应坐标进行截图 $x1 = $_POST['x1'] / 200; $w = ($_POST['x1'] + $_POST['w']) / 200; $y1 = $_POST['y1'] / 300; $h = ($_POST['y1'] + $_POST['h']) / 300; $img->crop($x1, $w, $y1, $h); $img->resize(100, 100); } else { //用户直接提交,直接对图片进行缩放 $img->resize(100, 100); } $data = $img->exec(); $uploadimage->write('headimg', $newFileName, $data); $model->headimg = $newFileName; if ($model->saveAttributes(array('headimg'))) { $this->redirect(array('/member/index')); } } } $this->render('updateHeadimg', array('model' => $model)); }