我正在尝试使用图像缩放器,我得到了以下代码
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);
}
现在,原始图像和缩略图将放在同一个文件夹中。
让我知道..
答案 0 :(得分:2)
嗯,答案是:
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);
}
但也许你想在使用之前了解一下你从网上复制和过去的代码。使用$ _ vars而不转义系统并使用@来隐藏错误并不是真正需要信任...
编辑:我提出建议,但也许最好给出一些解释。// 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);
}
答案 1 :(得分:0)
嘿,伙计,这看起来非常艰难,但可以简单地使用Thumbnailer库及其上传助手:
function callback(& $thumb) {
$thumb->thumbSquare(100)->save("/tmp/thumb/".$thumb->filename);
}
Thumbnailer::upload('ulimage', 'callback');
非常简单:)