public function setavatar() { $upload = new \Core\UploadImage(); if ($filedata = $upload->saveImage()) { $filedata['uid'] = $this->uid; $filedata['username'] = $this->username; $filedata['uptime'] = time(); $filedata['photoid'] = $this->t('photo')->insert($filedata, true); //裁剪图片 $sourceImg = C('ATTACHDIR') . $filedata['attachment']; $avatardir = C('AVATARDIR') . $this->uid; $avatarsmall = $this->uid . '_avatar_small.jpg'; $avatarmiddle = $this->uid . '_avatar_middle.jpg'; $avatarbig = $this->uid . '_avatar_big.jpg'; @mkdir($avatardir, 0777, true); $image = new Image($sourceImg); if ($image->width() > $image->height()) { $w = $image->height(); $x = ($image->width() - $image->height()) / 2; $y = 0; } else { $w = $image->width(); $x = 0; $y = ($image->height() - $image->width()) / 2; } $image->crop($w, $w, $x, $y, 150, 150); $image->save($avatardir . '/' . $avatarbig); $image = new Image($sourceImg); $image->crop($w, $w, $x, $y, 50, 50); $image->save($avatardir . '/' . $avatarmiddle); $image = new Image($sourceImg); $image->crop($w, $w, $x, $y, 30, 30); $image->save($avatardir . '/' . $avatarsmall); $this->t('member')->where(array('uid' => $this->uid))->update(array('avatarstatus' => 1)); $this->showAppData(array('url' => image($filedata['attachment']))); } else { $this->showAppError(-1, 'upload error', array('errno' => $upload->error)); } }
public function snap() { $filename = md5(time() . rand(100, 999)) . '.jpg'; $filepath = date('Y') . '/' . date('m') . '/' . $filename; $attachment = 'photo/' . $filepath; $thumb = 'thumb/' . $filename; @mkdir(dirname(C('ATTACHDIR') . $attachment), 0777, true); $content = file_put_contents(C('ATTACHDIR') . $attachment, file_get_contents('php://input')); if (!$content) { $this->showAjaxError(-1, "ERROR: Failed to write data to {$filepath}, check permissions"); } else { $image = new Image(C('ATTACHDIR') . $attachment); $photo = array('uid' => $this->uid, 'name' => $filename . '.jpg', 'attachment' => $attachment, 'thumb' => $thumb, 'width' => $image->width(), 'height' => $image->height(), 'type' => $image->type(), 'filesize' => filesize($savepath), 'uptime' => time()); $image->thumb(210, 210); $image->save(C('ATTACHDIR') . $thumb); $this->t('photo')->insert($photo, true); $this->showAjaxReturn(array('url' => C('ATTACHURL') . $attachment, 'attachment' => $attachment)); } }