我有一个问题。我需要使用PHP从文件夹中的图像获取图像尺寸。我知道这可能是使用PHP函数getimagesize
执行此操作的最佳方法,但我不知道将该段代码放入我的脚本中以便保存到DB。你能帮帮我吗?
这是我的Php脚本:
<?php
$server = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'dbname';
$connect = mysql_connect($server,$dbuser,$dbpass);
mysql_select_db($dbname,$connect);
$path = "gb-bilder-pics/augen/";
$files = array_map('mysql_real_escape_string',array_filter(glob("{$path}*.*"),'is_file'));
if(empty($files)){
echo "There were no matching files to insert into the database.";
}else{
$insertValues = array();
foreach($files as $file)
{
$insertValues[] = "('Titel', 'augen', '{$file}', '{$width????}', '{$height????}')";
}$query = "INSERT INTO `gbpics` (`gbpictitel`, `gbpiccat`, `gbpicurl`, `gbpicwidth`, `gbpicheight`) VALUES " . implode(', ', $insertValues);
if(!mysql_query($query)){
echo "There was a problem inserting the data.";
trigger_error("Query failed: $query<br />Error: " . mysql_error());
} else {
echo "The data was inserted successfully.";
}
}?>
答案 0 :(得分:1)
(PHP 4,PHP 5,PHP 7)getimagesize - 获取图像的大小
只需使用getimagesize
并传递文件名即可。它将返回尺寸以及文件类型和高度/宽度文本字符串。
我建议您在计算机上的图像上尝试此功能,然后查看该方法的行为方式。我确定你能够弄清楚如何从结果中提取宽度和高度。
如果您之后仍然需要帮助在您自己的代码中实施它,那么不要害怕问。
答案 1 :(得分:0)
我认为这很简单,这是修改脚本以获取没有db连接的图像尺寸,如果你想在服务器上使用脚本,你必须放置它。
$path = "gb-bilder-pics/augen/";
$files = array_map('mysql_real_escape_string',array_filter(glob("{$path}*.*"),'is_file'));
if(empty($files)){
echo "There were no matching files to insert into the database.";
} else {
$insertValues = array();
foreach($files as $file)
{
$data = getimagesize($file);
$width = $data[0];
$height = $data[1];
$insertValues[] = "('Titel', 'augen', '{$file}', '$width', '$height')";
}$query = "INSERT INTO `gbpics` (`gbpictitel`, `gbpiccat`, `gbpicurl`, `gbpicwidth`, `gbpicheight`) VALUES " . implode(', ', $insertValues);
if(!mysql_query($query)){
echo "There was a problem inserting the data.";
trigger_error("Query failed: $query<br />Error: " . mysql_error());
} else {
echo "The data was inserted successfully.";
}
}