gpt4 book ai didi

php - 如何在上传时调整多张图片的大小?

转载 作者:行者123 更新时间:2023-12-01 05:48:59 25 4
gpt4 key购买 nike

我有一个多重 uploader 的代码,该 uploader 运行良好。我要求提供额外的代码来在上传时调整上传图像的大小。

这是代码:

<div class="formItem">  
<div id="mulitplefileuploader">Upload</div>
<div id="status"></div>
<script>

$(document).ready(function()
{
var settings = {
url: "uploadnew.php",
method: "POST",
allowedTypes:"jpg,png,gif,doc,pdf,zip",
fileName: "myfile",
multiple: true,
onSuccess:function(files,data,xhr)
{
$("#status").html("<font color='green'>Upload is success</font>");

},
afterUploadAll:function()
{
alert("all images uploaded!!");
},
onError: function(files,status,errMsg)
{
$("#status").html("<font color='red'>Upload is Failed</font>");
}
}
$("#mulitplefileuploader").uploadFile(settings);

});
</script>
</div>

这是 uploadnew.php:

<?php
//If directory doesnot exists create it.
$output_dir = "uploads/";

if(isset($_FILES["myfile"]))
{
$ret = array();

$error =$_FILES["myfile"]["error"];
{

if(!is_array($_FILES["myfile"]['name'])) //single file
{
$RandomNum = time();

$ImageName = str_replace(' ','-',strtolower($_FILES['myfile']['name']));
$ImageType = $_FILES['myfile']['type']; //"image/png", image/jpeg etc.

$ImageExt = substr($ImageName, strrpos($ImageName, '.'));
$ImageExt = str_replace('.','',$ImageExt);
$ImageName = preg_replace("/\.[^.\s]{3,4}$/", "", $ImageName);
$NewImageName = $ImageName.'-'.$RandomNum.'.'.$ImageExt;

move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir. $NewImageName);
//echo "<br> Error: ".$_FILES["myfile"]["error"];

$ret[$fileName]= $output_dir.$NewImageName;


}
else
{
$fileCount = count($_FILES["myfile"]['name']);
for($i=0; $i < $fileCount; $i++)
{
$RandomNum = time();

$ImageName = str_replace(' ','-',strtolower($_FILES['myfile']['name'][$i]));
$ImageType = $_FILES['myfile']['type'][$i]; //"image/png", image/jpeg etc.

$ImageExt = substr($ImageName, strrpos($ImageName, '.'));
$ImageExt = str_replace('.','',$ImageExt);
$ImageName = preg_replace("/\.[^.\s]{3,4}$/", "", $ImageName);
$NewImageName = $ImageName.'-'.$RandomNum.'.'.$ImageExt;

$ret[$NewImageName]= $output_dir.$NewImageName;
move_uploaded_file($_FILES["myfile"]["tmp_name"][$i],$output_dir.$NewImageName );




}
}
}
echo json_encode($ret);
}
?>

最佳答案

您可以在已经必须循环浏览文件的“单个文件”代码周围添加一个 foreach($_FILES) {} 吗?因为它适用于一个或一百个文件。

此外,合理的谷歌搜索会找到相当多的结果,但这里有一个选项,可以通过以下网址进行一些调整以满足您的需求:

http://www.gilbertocortez.com/blog/php/image-resizing-with-php

<?PHP
//Image Target
$pic = ‘images/’.$_GET['pic'];
//Set Max Sizes
$max_height = 48;
$max_width = 48;
//Get Image Size
$size= getimagesize($pic);
//Set Ratios
$width_ratio = ($size[0] / $max_width);
$height_ratio = ($size[1] / $max_height);
if($width_ratio >=$height_ratio){
$ratio = $width_ratio;
} else {
$ratio = $height_ratio;
}
//Set new size
$new_width = ($size[0] / $ratio);
$new_height = ($size[1] / $ratio);
//Set header
header(“Content-Type: image/jpeg”);
//Create Image
$src_img = imagecreatefromjpeg($pic);
$thumb = imagecreatetruecolor($new_width,$new_height);
imagecopyresampled($thumb, $src_img, 0,0,0,0,$new_width,$new_height,$size[0],$size[1]);
imagejpeg($thumb);
imagedestroy($src_img);
imagedestroy($thumb);
?>

关于php - 如何在上传时调整多张图片的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24145866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com