上传后调整图片大小,而不是上传后?

时间:2017-04-30 17:10:07

标签: php image upload imagemagick gd

我正在使用HTML和PHP上传图片。

<form action="" method="post">
    <input type="file" name="image" id="image">
</form>

如果图像大小超过1500(宽度)x700(高度),我将如何使用imagemagick调整图像大小,以较大者为准,然后驻留图像。

据我所知,imagemagick只能在上传后调整图片大小。是否可以在上传时调整图像大小,然后存储到目录/文件夹中?

1 个答案:

答案 0 :(得分:1)

您可以调整临时文件的大小,然后在文件完成后保存。

以下是我通常如何处理它。请注意你需要做更多的事情来保护它!确保您正在检查上传允许的类型,大小等..

我使用此功能调整大小..

constructor(private mark: Mark) { }

然后我用temp文件调用该函数..

function img_resize($target, $newcopy, $w, $h, $ext) {
list($w_orig, $h_orig) = getimagesize($target);
$scale_ratio = $w_orig / $h_orig;
if (($w / $h) > $scale_ratio) {
    $w = $h * $scale_ratio;
} else {
    $h = $w / $scale_ratio;
}
$img = "";
$ext = strtolower($ext);
if ($ext == "gif"){ 
    $img = imagecreatefromgif($target);
} else if($ext =="png"){ 
    $img = imagecreatefrompng($target);
} else { 
    $img = imagecreatefromjpeg($target);
}
$tci = imagecreatetruecolor($w, $h);
// imagecopyresampled(dst_img, src_img, dst_x, dst_y, src_x, src_y, dst_w, 
dst_h, src_w, src_h)
imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
imagejpeg($tci, $newcopy, 80);
}
相关问题