我正试图骑HTML5但我面临一个小问题.在HTML5之前,我们使用flash检查文件大小,但现在趋势是避免在Web应用程序中使用Flash.有没有办法使用HTML5检查客户端的文件大小?
我想写一些PHP代码,自动调整通过表单上传到147x147px的任何图像,但我不知道如何去做(我是一个相对的PHP新手).
到目前为止,我已成功上传图片,识别文件类型并清理名称,但我想将调整大小功能添加到代码中.例如,我有一个2.3MB的测试图像,尺寸为1331x1331,我希望代码可以缩小尺寸,我猜测它也会大大压缩图像的文件大小.
到目前为止,我有以下内容:
if ($_FILES) {
//Put file properties into variables
$file_name = $_FILES['profile-image']['name'];
$file_size = $_FILES['profile-image']['size'];
$file_tmp_name = $_FILES['profile-image']['tmp_name'];
//Determine filetype
switch ($_FILES['profile-image']['type']) {
case 'image/jpeg': $ext = "jpg"; break;
case 'image/png': $ext = "png"; break;
default: $ext = ''; break;
}
if ($ext) {
//Check filesize
if ($file_size < 500000) {
//Process file - clean up filename and move to safe location
$n = "$file_name";
$n = ereg_replace("[^A-Za-z0-9.]", "", $n);
$n = strtolower($n);
$n = "avatars/$n";
move_uploaded_file($file_tmp_name, $n); …Run Code Online (Sandbox Code Playgroud) 我制作了一个负责上传图像的文件,然后将这些图像移动到服务器中的文件夹中.我想我不能直接在$ _FILES数组中调整图像大小,所以我想我必须在服务器中调整图像大小,所以我的问题是,如何调整服务器中的图像?
这是我的代码的一部分:
//This is after getting target which is the file saved on the server
move_uploaded_file($_FILES[$str]['tmp_name'], $target);
scale_image($target);
Run Code Online (Sandbox Code Playgroud)
现在函数scale_image()
function scale_image($image)
{
if(!empty($image)) //the image to be uploaded is a JPG I already checked this
{
$source_image = imagecreatefromjpeg($image);
$source_imagex = imagesx($source_image);
$source_imagey = imagesy($source_image);
$dest_imagex = 300;
$dest_imagey = 200;
$image = imagecreatetruecolor($dest_imagex, $dest_imagey);
imagecopyresampled($image, $source_image, 0, 0, 0, 0,
$dest_imagex, $dest_imagey, $source_imagex, $source_imagey);
}
}
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用,它移动文件但没有调整大小.