【问题标题】:Get image dimension from images in folder with php使用php从文件夹中的图像获取图像尺寸
【发布时间】:2017-03-23 08:29:00
【问题描述】:

我有一个问题。我需要使用 PHP 从文件夹中的图像中获取图像尺寸。我知道这可能是使用 PHP 函数 getimagesize 执行此操作的最佳方法,但我不知道将这段代码放在我的脚本中以保存到数据库的位置。你能帮帮我吗? 这是我的 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.";
    }
}?>

【问题讨论】:

  • 你用谷歌搜索过“php 图像大小”吗? MySQL 扩展在 PHP 5.5.0 中被弃用,并在 PHP 7.0.0 中被删除。
  • 是的,我用谷歌搜索过,我使用的是 php 5.6,所以没关系
  • 或者有其他选择吗?当我切换到 php 7 时?
  • getimagesize 根据 PHP.net 在 PHP 7.0 上可以正常工作。来源:php.net/manual/en/function.getimagesize.php
  • 显而易见的替代方法是使用 php 版本 7 并将您的代码移植到更安全的 mysql 适配器 mysqli 或 PDO。无论如何你都必须这样做,所以现在就开始。

标签: php mysql dimensions getimagesize


【解决方案1】:

(PHP 4, PHP 5, PHP 7) getimagesize — 获取图像的大小

只需使用getimagesize 并传递文件名。它将返回尺寸以及文件类型和高度/宽度文本字符串。

我建议在您计算机上的图像上尝试此功能,然后查看该方法的行为方式。我相信您将能够弄清楚如何从结果中提取宽度和高度。

如果您之后仍然需要帮助在您自己的代码中实现它,那么不要害怕问。

Source

【讨论】:

  • 嘿有一个功能可以完成这项工作,但我不知道如何实现他给了我一条错误消息getimagesize() expects parameter 1 to be string, array given in /var/www/ud20_182/html/gbpicscom/upload-from-folder-in-database.php on line 27@Arjan
【解决方案2】:

我发现它真的很简单,这是修改后的脚本以在没有数据库连接的情况下获取图像尺寸,如果你想在服务器上使用脚本,你必须先放置它。

$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.";
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-27
    • 1970-01-01
    • 1970-01-01
    • 2011-11-19
    • 1970-01-01
    • 2021-05-19
    • 1970-01-01
    相关资源
    最近更新 更多