简体   繁体   中英

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. 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. Could you please help me out. Here is my Php Script:

<?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

Simply use getimagesize and pass the filename. 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.";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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