如何上传重新格式化的图像?

时间:2012-08-09 20:52:49

标签: php image-processing gd image-resizing

我知道这已经非常接近完成了。 我的目标是将用户上传的图像重新调整大小0.5倍。 我已经实现了返回上传图像的宽度和高度,并将这些值减半。代码如下:

//get image attributes
    $target = "Images/";
    $target = $target . basename($_FILES['myFile']['name']);

    $thumbnailsize = 0.5;

    //Get uploaded image width and height.
    list($width, $height) = getimagesize($target);

    //Half the current image in size.
    $newWidth = $width * $thumbnailsize;
    $newheight = $height * $thumbnailsize;

    $new_target = imagecreatefromjpeg($target);
    $image = imagecreate($newWidth, $newheight);

    imagecopyresized($image, $new_target, 0, 0, 0, 0, $newWidth, $newheight, $width, $height);

    $pic = $_FILES['myFile']['name'];
    move_uploaded_file($_FILES['myFile']['tmp_name'], $target);

我想我的变量use和UPDATE SQL语句现在出错了,见下文:

$tUser_SQLselect = "UPDATE User SET imageLocation='" . $pic . "' ";
$tUser_SQLselect .= "WHERE ID = '" . $userID . "' ";

任何建议都会表示赞赏,谢谢。

1 个答案:

答案 0 :(得分:2)

您没有在任何地方调用imagejpeg(),因此调整后的文件不会保存在任何地方。除非您计划将原始文件与调整大小的文件一起保存,否则不能在副本上使用move_uploaded_files() - m_u_l()专门用于对上载的文件应用某些安全检查,因此上传完成后不会发生任何篡改但在文件移动之前 - 调整大小的图像会触发安全检查。

您通过$pic变量对SQL injection attacks开放广泛 - $ _FILES数组中的['name']参数是用户提供的数据,可用于破坏您的服务器

相关问题