static function clip_pic($width, $height, $pic, $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('获取属性失败!'); } //图片实际高宽比 $he = $image_size[1]; $we = $image_size[0]; $x1 = $he / $we; //图片实际高宽比和预设高宽比之比 $x12 = $x1 / $x2; if ($x12 < 1) { //如果实际图片比预设值高 if ($we > $w) { $img->resize($w); } //按预设宽度等比缩小 } else { //如果实际图片比预设值宽 if ($he > $h) { $img->resize(0, $h); } //按预设高度等比缩小 } $result = $img->exec(); if ($result == false) { echo "剪切失败!"; var_dump($img->errno(), $img->errmsg()); } else { return $result; } }
exit_status(0, 'Error! Wrong HTTP method!'); } if (array_key_exists('file', $_FILES) && $_FILES['file']['error'] == 0) { $file = $_FILES['file']; $ext = get_extension($file['name']); /* 后缀名判断不安全 */ $allowed_ext = array('png', 'jpg', 'jpeg', 'gif'); if (!in_array($ext, $allowed_ext)) { exit_status('error', 'Only Image can upload!'); } if ($file['size'] > 10 * 1024 * 1024) { exit_status('error', 'Max file size is 5MB!'); } $form_data = $file['tmp_name']; $dumpdata = file_get_contents($form_data); $img = new SaeImage(); $img->setData($dumpdata); $img->resize(600); // 等比缩放到600宽 $new_data = $img->exec('jpg'); // 执行处理并返回处理后的二进制数据 //转换失败 if ($new_data === false) { exit_status('error', 'p1:' . $img->errmsg()); } $size = $img->getImageAttr(); //覆盖加入3张图片 $logo_ten_year_img = file_get_contents('../res/images/logo_ten_year110.png'); $logo_zfb_img = file_get_contents('../res/images/logo_zfb100.png'); $text_bg_img = file_get_contents('../res/images/text_back350.png'); //清空$img数据
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 saeimage() { //从网络上抓取要合成的多张图片 $img1 = file_get_contents('http://ss2.sinaimg.cn/bmiddle/53b05ae9t73817f6bf751&690'); $img2 = file_get_contents('http://timg.sjs.sinajs.cn/miniblog2style/images/common/logo.png'); $img3 = file_get_contents('http://i1.sinaimg.cn/home/deco/2009/0330/logo_home.gif'); //实例化SaeImage并取得最大一张图片的大小,稍后用于设定合成后图片的画布大小 $img = new SaeImage($img1); $size = $img->getImageAttr(); //清空$img数据 $img->clean(); //设定要用于合成的三张图片(如果重叠,排在后面的图片会盖住排在前面的图片) $img->setData(array(array($img1, 0, 0, 1, SAE_TOP_LEFT), array($img2, 0, 0, 0.5, SAE_BOTTOM_RIGHT), array($img3, 0, 0, 1, SAE_BOTTOM_LEFT))); //执行合成 $img->composite($size[0], $size[1]); //输出图片 $img->exec('jpg', true); }
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;*/ } } }
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; } }
public static function thumb($image, $thumbname, $domain = 'public', $maxWidth = 200, $maxHeight = 50, $interlace = true) { // 获取原图信息 $info = self::getImageInfo($image); if ($info !== false) { $srcWidth = $info['width']; $srcHeight = $info['height']; $type = strtolower($info['type']); $interlace = $interlace ? 1 : 0; unset($info); $scale = min($maxWidth / $srcWidth, $maxHeight / $srcHeight); // 计算缩放比例 if ($scale >= 1) { // 超过原图大小不再缩略 $width = $srcWidth; $height = $srcHeight; } else { // 缩略图尺寸 $width = (int) ($srcWidth * $scale); $height = (int) ($srcHeight * $scale); } //sae平台上图片处理 if (class_exists('SaeStorage')) { $saeStorage = new SaeStorage(); $saeImage = new SaeImage(); $saeImage->setData(file_get_contents($image)); $saeImage->resize($width, $height); $thumbname = str_replace(array('../', './'), '', $thumbname); return $saeStorage->write($domain, $thumbname, $saeImage->exec()); } // 载入原图 $createFun = 'ImageCreateFrom' . ($type == 'jpg' ? 'jpeg' : $type); $srcImg = $createFun($image); //创建缩略图 if ($type != 'gif' && function_exists('imagecreatetruecolor')) { $thumbImg = imagecreatetruecolor($width, $height); } else { $thumbImg = imagecreate($width, $height); } // 复制图片 if (function_exists("ImageCopyResampled")) { imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight); } else { imagecopyresized($thumbImg, $srcImg, 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, $interlace); } $dir = dirname($thumbname); if (!is_dir($dir)) { @mkdir($dir, 0777, true); } // 生成图片 $imageFun = 'image' . ($type == 'jpg' ? 'jpeg' : $type); $imageFun($thumbImg, $thumbname); imagedestroy($thumbImg); imagedestroy($srcImg); return $thumbname; } return false; }
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; } }
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; } }
<?php if (isset($_FILES["userAvatar"])) { $files = $_FILES['userAvatar']; $name = 'avatar-' . time() . '.jpg'; $form_data = $files['tmp_name']; $s2 = new SaeStorage(); $img = new SaeImage(); $img_data = file_get_contents($form_data); //获取本地上传的图片数据 $img->setData($img_data); $img->resize(260, 260); //图片缩放 $img->improve(); //提高图片质量的函数 $new_data = $img->exec(); // 执行处理并返回处理后的二进制数据 $s2->write('upload', $name, $new_data); //将upload修改为自己的storage $url = $s2->getUrl('upload', $name); exit("{'state':'200','headimgurl':'" . $url . "'}"); }
/** * Scale down an image to fit a particular size and save a new copy of the image. * * The PNG transparency will be preserved using the function, as well as the * image type. If the file going in is PNG, then the resized image is going to * be PNG. The only supported image types are PNG, GIF, and JPEG. * * Some functionality requires API to exist, so some PHP version may lose out * support. This is not the fault of WordPress (where functionality is * downgraded, not actual defects), but of your PHP version. * * @since 2.5.0 * * @param string $file Image file path. * @param int $max_w Maximum width to resize to. * @param int $max_h Maximum height to resize to. * @param bool $crop Optional. Whether to crop image or resize. * @param string $suffix Optional. File suffix. * @param string $dest_path Optional. New image file path. * @param int $jpeg_quality Optional, default is 90. Image quality percentage. * @return mixed WP_Error on failure. String with new destination path. */ function image_resize($file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 90) { if (is_numeric($file)) { $file = get_attached_file($file); } $imagecontent = file_get_contents($file); if (!$imagecontent) { return new WP_Error('error_loading_image', sprintf(__('File “%s” doesn’t exist?'), $file), $file); } $localtmp = tempnam(SAE_TMP_PATH, 'WPIMG'); file_put_contents($localtmp, $imagecontent); $size = @getimagesize($localtmp); if (!$size) { return new WP_Error('invalid_image', __('Could not read image size'), $file); } list($orig_w, $orig_h, $orig_type) = $size; $dims = image_resize_dimensions($orig_w, $orig_h, $max_w, $max_h, $crop); if (!$dims) { return new WP_Error('error_getting_dimensions', __('Could not calculate resized image dimensions')); } list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims; $saeimage = new SaeImage(); $saeimage->setData($imagecontent); $saeimage->resize($dst_w, $dst_h); // $suffix will be appended to the destination filename, just before the extension if (!$suffix) { $suffix = "{$dst_w}x{$dst_h}"; } $info = pathinfo($file); $dir = $info['dirname']; $ext = $info['extension']; $name = wp_basename($file, ".{$ext}"); if (!is_null($dest_path) and $_dest_path = realpath($dest_path)) { $dir = $_dest_path; } $destfilename = "{$dir}/{$name}-{$suffix}.{$ext}"; if (IMAGETYPE_GIF == $orig_type) { $retcontent = $saeimage->exec('gif'); if (!$retcontent || !file_put_contents($destfilename, $retcontent)) { return new WP_Error('resize_path_invalid', __('Resize path invalid')); } } elseif (IMAGETYPE_PNG == $orig_type) { $retcontent = $saeimage->exec('png'); if (!$retcontent || !file_put_contents($destfilename, $retcontent)) { return new WP_Error('resize_path_invalid', __('Resize path invalid')); } } else { // all other formats are converted to jpg if ('jpg' != $ext && 'jpeg' != $ext) { $destfilename = "{$dir}/{$name}-{$suffix}.jpg"; } $retcontent = $saeimage->exec('jpg'); if (!$retcontent || !file_put_contents($destfilename, $retcontent)) { return new WP_Error('resize_path_invalid', __('Resize path invalid')); } } return $destfilename; }
<?php $link = mysql_connect(SAE_MYSQL_HOST_M . ':' . SAE_MYSQL_PORT, SAE_MYSQL_USER, SAE_MYSQL_PASS); if ($link) { mysql_select_db(SAE_MYSQL_DB, $link); $sql = "select count(*) from loli_images"; $result = mysql_query($sql); $rand_id_max = mysql_fetch_row($result); $random_id = rand(1, $rand_id_max[0]); $sql = "SELECT loli_images_url FROM loli_images WHERE loli_images_id='{$random_id}'"; $result = mysql_query($sql); //执行sql语句 $row = mysql_fetch_row($result); } echo $row[0]; $img = new SaeImage(); $img->setData($row[0]); $img->resize(200); // 等比缩放到200宽 if ($new_data === false) { var_dump($img->errno(), $img->errmsg()); } print_r($img); echo "===============================================\n"; echo $row[0];
function create_nails($name, $name_nail, $s, $f, $domain, $aimage) { $s = new SaeStorage(); $f = new SaeFetchurl(); $img_data = $f->fetch($aimage); $img = new SaeImage(); $img->setData($img_data); $img->resize(400); $new_data = $img->exec('jpg'); if ($new_data) { $success = $s->write($domain, $name, $new_data); //写入用于显示的图片 } if ($success) { //如果写入成功 # code... $s_nail = new SaeStorage(); $img_nail = new SaeImage(); $img_nail->setData($img_data); $img_nail->resize(100); $new_data_nail = $img_nail->exec('jpg'); $success_nail = $s_nail->write($domain, $name_nail, $new_data_nail); if ($success && $success_nail) { //如果写入同时成功 $result = true; } else { $result = false; } } return $result; }
private static function imageNull() { if (is_array(self::$_img_data) && count(self::$_img_data) == 0) { self::$_errno = SAE_ErrParameter; self::$_errmsg = "image data cannot be empty"; return true; } if (!is_array(self::$_img_data) && self::$_img_data == "") { self::$_errno = SAE_ErrParameter; self::$_errmsg = "image data cannot be empty"; return true; } 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)); }