上传和缩放图像没有高度参数

时间:2018-05-04 08:45:51

标签: php

CONVERT(date, CONVERT(datetime,DocDate))

我想上传<input type='file' id='inpfile' accept="image/jpeg, image/png, image/gif" hidden> 张图片,最大宽度jpg, png, gif并使用新数据更新数据库。

PHP

960px

似乎有效,但有两个问题:

  • 有更短的方法吗?
  • Documentation对于imagescale(新高度)说:
    $file = $_FILES['inpfile']; $filename = $file['name']; $tempname = $file['tmp_name']; $arr = explode('.', $filename); $ext = strtolower(end($arr)); $info = getimagesize($tempname); $origw = $info[0]; $origh = $info[1]; $mime = $info['mime']; if ($mime !== 'image/jpeg' and $mime !== 'image/png' and $mime !== 'image/gif') { echo "ONLY JPG-PNG-GIF ALLOWED !"; exit(); } if ($mime == 'image/jpeg') {$newimg = imagecreatefromjpeg($tempname);} elseif ($mime == 'image/png') {$newimg = imagecreatefrompng($tempname);} elseif ($mime == 'image/gif') {$newimg = imagecreatefromgif($tempname);} if ($origw <= 960) { $uniq = uniqid() . "." . $ext; $targ = '../images/' . $uniq; move_uploaded_file($tempname, $targ); } else { $finalimg = imagescale($newimg, 960, -1, IMG_BICUBIC); $uniq = uniqid() . "." . $ext; $targ = '../images/' . $uniq; if ($mime == 'image/jpeg') {imagejpeg($finalimg, $targ);} elseif ($mime == 'image/png') {imagepng($finalimg, $targ);} elseif ($mime == 'image/gif') {imagegif($finalimg, $targ);} } list($w, $h) = getimagesize($targ); $st = $db->query("insert into images (fname, width, height) values('" . $uniq . "', " . $w . ", " . $h . ")");

我试过了:

If omitted or negative, the aspect ratio will be preserved.

最终图片为$finalimg = imagescale($newimg, 960, IMG_BICUBIC);

1 个答案:

答案 0 :(得分:1)

  

有更短的方法吗?

说到GD /图像功能:我不这么认为。但是,您可以简化pr_lm <- partial(lm, formula = Sepal.Length ~ Sepal.Width) get_rsquared <- compose(as_mapper("r.squared"), summary, pr_lm) iris_n %>% mutate(lm = map_dbl(data, get_rsquared)) # A tibble: 3 x 3 Species data lm <fct> <list> <dbl> 1 setosa <tibble [50 × 4]> 0.551 2 versicolor <tibble [50 × 4]> 0.277 3 virginica <tibble [50 × 4]> 0.209 内容以确定图像类型。

  

imagescale(新高度)的文档说:   如果省略或为负,则将保留纵横比。

传递第四个参数(if/else)时,不能省略第三个参数($new_height)。如果要设置模式,还需要设置高度。所以这样做像这样:

$mode