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

标签 php jquery resize

我有一个多重 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/

相关文章:

jquery - 动画粘性导航?

ios - 正确裁剪和缩放 UIImage

php - 如何在 RHEL 上永久启用 PHP 5.4? (系统上还安装了PHP 5.3.3)

javascript - CSS/JS 覆盖缩放问题

javascript - 我可以使用 jquery contentWindow 调用命名空间函数吗?

java - Java 8 中的默认 ArrayList 大小是多少

python - 如何通过拖动使 PyQt 小部件可调整大小?

php - 为什么 $variable = 0759 给出 61?

Php排行榜问题

php - 使用 eregi() 但现在使用 preg_match 进行 URL 验证