简体   繁体   中英

Php making array of pictures with parameters from folder/picture name

So I'm trying to do an import of shop's products. In the database I have all the product info I need (ID, name). The tricky part - pictures.

There's a product picture folder, which has a structure like this:

Manufacturer folder -> .jpg's named "XXX - name_name_name" where XXX - product ID.

For example - "C:/pictures/nike/100 - shoes1.jpg"

There are about 20 manufacturers (folders), about 8K products total.

If I specify a path to the picture folder, how could I get data like "manufacturer name(folder name), product id (cut out from full name), picture url" for each picture that I could use to compare with the database entries and set the correct picture for the product? Is it even worth making such array with php?

Example product database entry:


ID Name

100 Nike_shoes1


What I get from select query (only ID usable, product name in DB and on the picture may differ)

array(25) {
  [0]=>
  array(8) {
    ["category"]=>
    string(10) "6"
    ["ID"]=>
    string(20) "1659"
    ["name"]=>
    string(70) "Relyx U100 TR 56839 (3M) - nebetiekiama"
    ["price"]=>
    string(6) "286.69"
    ["qnt"]=>
    string(1) "0"
    ["COL 6"]=>
    string(4) "vnt"
    ["pvm"]=>
    string(5) "21.00"
    ["COL 8"]=>
    string(9) "20140 "
  }

so the result should be something like: (pictures array)

["pictureUrl"]=>
    string(10) "C:/pictures/nike/100 - Nike_shoes1.jpg"
["manufacturer"]=>
    string(10) "nike"
["id"]=>
    string(10) "100"

and then I could compare it to what I have, like

foreach ($pictures as $key => $picture){
    if($product['id'] == $picture['id']) {
        $product['picture'] = $picture['pictureUrl'];
        $product['manufacturer'] = $picture['manufacturer'];
    } 
}

Let me know if the question is appropriate, or if I should add anything.

  1. Place the pictures folder in your php dir, assuming you have a project named products in your www, or home, or htdocs. ig C:/xampp/htdocs/products/images

  2. From what I see, you have all the info in the image name (I hope the images names are standardized the way you mentioned)

  3. Let's start operation:

Fetch images names from db:

pseudo code

$images_list = $images->getAllAssoc("id","image_name");

//prints: 
array(
[100]=>"Nike_shoes1"
[200]=>"Nike_shoes2"
[300]=>"Nike_shoes3"
)

//Now let's loop and generate the output
$image_base_url="pictures";//can change 
foreach ($images_list as $image_id => $image_name)
{
    $image_parts= explode("_", $image_name); // [0]=Nike, [1]=shoes1

    $img_arr["manufacturer"] = $image_parts[0];
    $img_arr["id"] = $image_id;
    $img_arr["pictureUrl"] = "{$image_base_url}/{$img_arr["manufacturer"]}/{$image_id} - {$image_name}";///pictures/nike/100 - Nike_shoes1.jpg

//compose in the final result array
$producs_images[] =$img_arr;

}

UPDATE:

Based on your comments I At least this is how I would do it if I were you. I will create a new table (or add a new column/field in the db) and call it 'manufacturer'. So by knowing the manufacturer of your product, you can easily locate the image.

My approach will be to create a new field in the products table and call it 'manufacturer ' and then go your picture directory and loop through all folders to 1-capture the manufacturer name. 2-get all the ids of products in it. I assume the products are the folders names under the manif folder. And then update the manufacturer column in your db. Be careful that the full url I used to access my local pic file in my windows machine. You need to edit to suites yours whether in localhost or later on a production server.

My structure is as the following:

在此处输入图片说明

Example code:

$main_dir = "\SERVER\htdocs\phpProject1\pictures";
$dir_list = new ParentIterator(new RecursiveDirectoryIterator($main_dir));
$it = new RecursiveIteratorIterator($dir_list, RecursiveIteratorIterator::SELF_FIRST);
// Optionally, limit the depth of recursion with $it->setMaxDepth($number)
$it = iterator_to_array($it, true);
$c = 1;
foreach ($it as $dir) {
    echo $dir;
    echo PHP_EOL;
    $chunks = explode("\\", $dir);

    $arr["manif"] = $chunks[5];
    if ($c !== 1) {
        $arr["prodcut_id"] = $chunks[6];
        $p_arr[] = $arr;
    }
    $c++;
}
print_r($p_arr);

Output:

\SERVER\htdocs\phpProject1\pictures\nike
\SERVER\htdocs\phpProject1\pictures\nike\100
\SERVER\htdocs\phpProject1\pictures\nike\200
\SERVER\htdocs\phpProject1\pictures\nike\300
Array
(
    [0] => Array
        (
            [mani] => nike
            [prodcut_id] => 100
        )

    [1] => Array
        (
            [mani] => nike
            [prodcut_id] => 200
        )

    [2] => Array
        (
            [mani] => nike
            [prodcut_id] => 300
        )

)
Done.

Now you have the product id with it's manufacturer together collected from the picture folder, what you need to do is to update the manufacturer column you added to the db based on the product id.

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