如何使缩略图去到/ tmp /拇指/并保留原始图像上的/ tmp /与图像调整?

StackOverflow https://stackoverflow.com/questions/990275

  •  13-09-2019
  •  | 
  •  

我试图用图像缩放玩,我得到了下面的代码

if (is_uploaded_file(@$_FILES['ulimage']['tmp_name'])){
        $targetfilename = ImageHelper::treatFilename(uniqid() . "_" . $_FILES['ulimage']['name']);
        move_uploaded_file($_FILES['ulimage']['tmp_name'], dirname(__FILE__) . "/tmp/" . $_FILES['ulimage']['name']);
        ImageHelper::resizeImage(dirname(__FILE__) . "/tmp/" . @$_FILES['ulimage']['name'], dirname(__FILE__) . "/tmp/" . $targetfilename, $width, $height);
    }

有关现在,原来的图像和缩略图将放置在相同的文件夹。

让我知道..

有帮助吗?

解决方案

那么,答案是:

if (is_uploaded_file(@$_FILES['ulimage']['tmp_name']))
{
        $targetfilename = ImageHelper::treatFilename(uniqid() . "_" . $_FILES['ulimage']['name']);
        move_uploaded_file($_FILES['ulimage']['tmp_name'], dirname(__FILE__) . "/tmp/" . $_FILES['ulimage']['name']);
        ImageHelper::resizeImage(dirname(__FILE__) . "/tmp/" . @$_FILES['ulimage']['name'], dirname(__FILE__) . "/tmp/thumb/" . $targetfilename, $width, $height);
}

但是,也许你想了解一点在使用它之前从网上复制的代码和过去。使用$ _瓦尔没有逃逸系统和@隐藏的错误是不是真的呼唤信任......

编辑:我给意见,但也许这是更好地给一些解释以及

// first you check if the is done uploading in the tmp directory with is tmp name
if (is_uploaded_file(@$_FILES['ulimage']['tmp_name'])) 
{
     // here, you rebuild a explicit name using the original filename and a 
     // unique ID to avoid erasing another one   
     $targetfilename = ImageHelper::treatFilename(uniqid() . "_" . $_FILES['ulimage']['name']);

     // you rename the file an put it in ./tmp, a subdir of the 
     // script file (because of dirname(__FILE__))
     move_uploaded_file($_FILES['ulimage']['tmp_name'], dirname(__FILE__) . "/tmp/" . $_FILES['ulimage']['name']);

    // Here create a rezided copy
    // so it's here you can decide to make it go to ./tmp/thumb
    // make sure the dir exists before because you have no clue here
    // if ImageHelper will create it for you if not
    ImageHelper::resizeImage(dirname(__FILE__) . "/tmp/thumb/" . @$_FILES['ulimage']['name'], dirname(__FILE__) . "/tmp/thumb/" . $targetfilename, $width, $height);
}

其他提示

嗨,哥们,这看起来很强悍,但可以简单地使用 thumbnailer的库及其助手上传进行:

function callback(& $thumb) {
   $thumb->thumbSquare(100)->save("/tmp/thumb/".$thumb->filename);
}

Thumbnailer::upload('ulimage', 'callback');

很容易:)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top