PHP-两次上传单个图像

时间:2018-10-07 19:14:49

标签: php image upload

我想上传一张图片(因此,一个$ _FILES项)两次,因此可以将其调整为2种尺寸。

我认为我可以为此做一个循环,只需将上传库循环两次,就像这样:

                for ($k = 0; $k < 2; $k++) {
                    if($k == 0) { // small
                        $image = new Bulletproof\Image($_FILES);
                        $image->setName(uniqid() . '-small'); 
                        $image->setMime(array('jpg', 'png', 'jpeg'));
                        $image->setLocation('../../assets/images/usercontent/site/tutorials/');

                        if($image["banner"]){
                            if($image->upload()) {
                                $create = $engine->runQuery("INSERT INTO site_uploads(hook_id, image_id, type, category) VALUES(:hook_id, :image_id, :type, :category)");
                                $create->execute(array(':hook_id'=>$_POST['id'], ':image_id'=>$image->getName(), ':type'=>$image->getMime(), ':category'=>'0'));

                                bulletproof\utils\resize($image->getFullPath(), $image->getMime(), $image->getWidth(), $image->getHeight(), 190, 175);
                            }
                        }
                    } elseif($k == 1) { // large
                        $image = new Bulletproof\Image($_FILES);
                        $image->setName(uniqid() . '-large'); 
                        $image->setMime(array('jpg', 'png', 'jpeg'));
                        $image->setLocation('../../assets/images/usercontent/site/tutorials/');

                        if($image["banner"]){
                            if($image->upload()) {
                                $create = $engine->runQuery("INSERT INTO site_uploads(hook_id, image_id, type, category) VALUES(:hook_id, :image_id, :type, :category)");
                                $create->execute(array(':hook_id'=>$_POST['id'], ':image_id'=>$image->getName(), ':type'=>$image->getMime(), ':category'=>'0'));

                                bulletproof\utils\resize($image->getFullPath(), $image->getMime(), $image->getWidth(), $image->getHeight(), 250, 400);
                            }
                        }
                    }
                }

但是出于某种原因,它仅上载if($k == 0),而忽略后面的每个循环。

有人知道它为什么这样做吗,我该如何解决?

1 个答案:

答案 0 :(得分:0)

文件上传后,它会进入temp目录。防弹性将此文件从临时目录中移动到您所需的位置。因此,您将无法再访问临时文件,因为您的文件已不存在。。您可以保存然后使用gd或其他图像库进行复制,调整大小。

我没有测试它,但是我认为它可以完美运行
(需要GD库才能工作)

$image = new Bulletproof\Image($_FILES);
$image->setName(uniqid() . '-large');
$image->setMime(array('jpg', 'png', 'jpeg'));
$image->setLocation('../../assets/images/usercontent/site/tutorials/');

if($image["banner"]){
    if($image->upload()) {
        $create = $engine->runQuery("INSERT INTO site_uploads(hook_id, image_id, type, category) VALUES(:hook_id, :image_id, :type, :category)");
        $create->execute(array(':hook_id'=>$_POST['id'], ':image_id'=>$image->getName(), ':type'=>$image->getMime(), ':category'=>'0'));
        bulletproof\utils\resize($image->getFullPath(), $image->getMime(), $image->getWidth(), $image->getHeight(), 250, 400);

        $file = $image->getFullPath();
        $mime = getimagesize($file)["mime"];
        if ($mime == "image/png"){$image = imagecreatefrompng($file);}
        else if ($mime == ("image/jpg" or "image/jpeg" or "image/pjpeg")){$image = imagecreatefromjpeg($file);}
        else {$image = false;}

        if ($image !== false){
            // edit -- I forget to add variable
            $image = imagescale($image, 190, 175);

            imagejpeg($image, str_replace("-large", "-small", $image->getFullPath()));
            // image saved to same directory as uniqid()-large.***
            // So now you can do sql things
            imagedestroy($image);
        }else {
            // Can't open image
        }
    }
}



编辑 我没有注意到,但是可能可以使用bulletproof \ utils \ resize来实现。

$newname = str_replace("-large", "-small", $image->getFullPath());
if (copy($image->getFullPath(), $newname)) {
    bulletproof\utils\resize($newname, $image->getMime(), $image->getWidth(), $image->getHeight(), 175, 190);
}else {
    // Can't copy file
}