我正在尝试使用 PHP 调整图像大小,但我不知道如何让它正确命名新图像。当我在数据库中查看图像的表格时,有一个带有 ID 但没有名称的条目。
如果您还有什么需要看的,请告诉我。截至目前,我假设我没有正确的输入进行图像复制重新采样。当我试图呼应这个名字时$formattedImage什么也没发生。如果我在使用 imagejpeg 之前进一步备份它并回显$blankImage的名称,也不会发生任何事情。
这是我的代码。
function chgSize($image, $width, $height){
$name = $image["name"]; //this is displaying the file name correctly
$temp = $image["tmp_name"];
$imgContents = imagecreatefromstring(file_get_contents($temp));
$blankImage = imagecreatetruecolor(100,100);
if(imagecopyresampled($blankImage, $imgContents, 0, 0, 0, 0, 100, 100, $width, $height)){
$formattedImage = imagejpeg($blankImage, $name);
$this->saveImage($formattedImage);
}
}
function saveImage($image){
$newName = $image["name"];
move_uploaded_file($image['tmp_name'], "/Applications/XAMPP/xamppfiles/htdocs/images/" . $newName);
mysql_query("insert into images VALUES('null','$newName')");
echo "Stored in: " . "/Applications/XAMPP/xamppfiles/htdocs/images/" . $newName;
}
仅供参考:'null'
不是空值。它是一个包含世界"null"的字符串。要插入实际的 sql null,必须指定单词 null 不带引号,例如裸null
。
您的代码也容易受到 SQL 注入的影响,而且您的move_uploaded_file调用使用用户提供的数据作为目标文件名,因此您允许远程用户在服务器上的任何位置指定要涂鸦的任何文件。
您显然也没有检查上传是成功还是失败,并且像到处进行一样。不好。插入查询也是如此。假设查询成功是您可能做的最糟糕的事情。查询成功只有一种方法,失败的方法有无数种。