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; }
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数据 $img->clean(); //设定要用于合成的三张图片(如果重叠,排在后面的图片会盖住排在前面的图片)
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; } }
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; } }
<?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; }
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)); }