简体   繁体   English

使用php从文件夹中的图像获取图像尺寸

[英]Get image dimension from images in folder with php

I got a question. 我有一个问题。 I need to get the image dimensions from images in folder with PHP. 我需要使用PHP从文件夹中的图像获取图像尺寸。 I know that probably would be the best way to do this with PHP function getimagesize but I have no idea where to put the piece of code into my script in order to save to DB. 我知道这可能是使用PHP函数getimagesize执行此操作的最佳方法,但是我不知道在哪里将代码片段放入脚本中以保存到数据库。 Could you please help me out. 你能帮我一下吗。 Here is my Php Script: 这是我的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 4, PHP 5, PHP 7) getimagesize — Get the size of an image (PHP 4,PHP 5,PHP 7)getimagesize-获取图像的大小

Simply use getimagesize and pass the filename. 只需使用getimagesize并传递文件名。 It will return the dimensions along with the file type and height/width text string. 它将返回尺寸以及文件类型和高度/宽度文本字符串。

I suggest trying out this function on an image you have on your computer and just look at how the method behaves. 我建议您在计算机上的图像上试用此功能,然后看看该方法的行为。 I'm sure you will be able to figure out how to extract the width and height from the result. 我相信您将能够弄清楚如何从结果中提取宽度和高度。

If you afterwards still need help implementing it in your own code then don't be afraid to ask. 如果之后您仍然需要帮助在自己的代码中实现它,那么不要害怕问。

Source 资源

I figured it out was really easy this is the script modified to get image dimensions without the db connection which u have to place before if u want to use the script on your server. 我发现这真的很容易,这是修改脚本以获取图像尺寸而无需数据库连接,如果要在服务器上使用脚本,则必须先放置该数据库。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM