【问题标题】:unable in creating thumb IMAGE IS BLACK [duplicate]无法创建拇指图像为黑色 [重复]
【发布时间】:2015-01-10 04:21:42
【问题描述】:

我正在上传来自单个输入的多个图像并创建拇指表单所有上传的图像但是当我运行代码时我得到只有黑色图像但原始图像与上传的相同

    $orig_directory = "$desired_dir";    //Full image folder
$thumb_directory =  "thumb/";    //Thumbnail folder

/* Opening the thumbnail directory and looping through all the thumbs: */
$dir_handle = @opendir($orig_directory); //Open Full image dirrectory
if ($dir_handle > 1){ //Check to make sure the folder opened

$allowed_types=array('jpg','jpeg','gif','png');
$file_type=array();
$ext='';
$title='';
$i=0;

while ($file_name = @readdir($dir_handle)) {
    /* Skipping the system files: */
    if($file_name=='.' || $file_name == '..') continue;

    $file_type = explode('.',$file_name);    //This gets the file name of the images
    $ext = strtolower(array_pop($file_type));

    /* Using the file name (withouth the extension) as a image title: */
    $title = implode('.',$file_type);
    $title = htmlspecialchars($title);

    /* If the file extension is allowed: */
    if(in_array($ext,$allowed_types)) {

        /* If you would like to inpute images into a database, do your mysql query here */

        /* The code past here is the code at the start of the tutorial */
        /* Outputting each image: */

        $nw = 100;
        $nh = 100;
        $source = "$desired_dir{$file_name}";
        $stype = explode(".", $source);
        $stype = $stype[count($stype)-1];
        $dest = "thumb/{$file_name}";

        $size = getimagesize($source);
        $w = $size[0];
        $h = $size[1];

        switch($stype) {
            case 'gif':
                $simg = imagecreatefromgif($source);
                break;
            case 'jpg': 
                $simg = imagecreatefromjpeg($source);
                break;
            case 'png':
                $simg = imagecreatefrompng($source);
                break;
        }

        $dimg = imagecreatetruecolor($nw, $nh);
        $wm = $w/$nw;
        $hm = $h/$nw;
        $h_height = $nh/2;
        $w_height = $nw/2;

        if($w> $h) {
            $adjusted_width = $w / $hm;
            $half_width = $adjusted_width / 2;
            $int_width = $w / $hm;
            imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
        } else {
            imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
        }
            imagejpeg($dimg,$dest,100);
        }
}

/* Closing the directory */
@closedir($dir_handle);

}
?>

当我运行代码时我只得到黑色图像我不知道会发生什么我在上面得到错误拇指是黑色的

【问题讨论】:

    标签: php image thumbnails


    【解决方案1】:

    使用下面的代码

        <html>
        <body>
            <pre><?
                print_r($_FILES); //SHOW THE ARRAY
    
                foreach ($_FILES as $file) {
                    if (!$file['error']) {
                        //PROCESS THE FILE HERE
                        echo $file['name'];
                    }
                }
            ?></pre>
    
            <script src="http://code.jquery.com/jquery-1.11.1.min.js" type="text/javascript"></script>
            <script type="text/javascript">
    
                var fd = new FormData(); 
                $(document).ready(function() {
    
                    //submit dragdropped by ajax
                    $("#dropsubmit").on("click", function(e) {
                         $.ajax({           
                            data: fd,
                            processData: false,
                            contentType: false,
                            type: 'POST',
                            success: function(data){
                                //FILES POSTED OK! REDIRECT
                                console.log(data);
                            }
                          });
                    })
    
                    var dropzone = $("#dropzone");
                    dropzone.on('dragenter', function (e) {
                        $(this).css('background', 'lightgreen');
                    });
    
                    //dropped files, store as formdata
                    dropzone.on('dragover', stopPropagate);
                    dropzone.on('drop', function (e) {
                        stopPropagate(e)
                        $(this).css('background', 'white');
                         var files = e.originalEvent.dataTransfer.files;
    
                         for (var i = 0; i < files.length; i++)  {
                            preview(files[i])
                            fd.append('file' + (i + 1), files[i]);
                            if (i >= 5) continue
                         }
    
                    });
                    function stopPropagate(e) {
                        e.stopPropagation();
                        e.preventDefault();
                    } 
    
    
                    if (window.File && window.FileList && window.FileReader) {
                        $("input[type=file]").on("change", function(e) {
                            preview(e.target.files[0])
                        });
                    } else {
                        alert("Your browser doesn't support to File API")
                    }
    
                    function preview(item) {
                        var fileReader = new FileReader();
                        fileReader.onload = (function(e) {
                            var file = e.target;
                            $("<img></img>", {
                                class: "imageThumb",
                                src: file.result,
                                title: file.name,
                            }).appendTo("#images");
                        });
                        fileReader.readAsDataURL(item);
                    }
                });
            </script>
    
            <div id="dropzone" style="width:100px;height:100px;border:2px dashed gray;padding:10px">Drag & Drop Files Here</div>
            <input type="button" id="dropsubmit" value="Submit dropped files" />
            <hr>
            <form method="post" enctype="multipart/form-data">
                <input id="file1" name="file1" type="file"/><br>
                <input id="file2" name="file2" type="file"/><br>
                <input id="file3" name="file3" type="file"/><br>
                <input id="file4" name="file3" type="file"/><br>
                <input id="file5" name="file3" type="file"/><br>
                <input name="submit" type="submit" value="Upload files" />
            </form><div id="images"></div>
        </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 2014-11-06
      • 1970-01-01
      • 1970-01-01
      • 2023-01-04
      • 1970-01-01
      • 2015-06-20
      • 2023-04-05
      • 1970-01-01
      • 2014-12-23
      相关资源
      最近更新 更多