Jump to content
rash

[SOLVED] How to add remote images if their filename is an URL parameter

Recommended Posts

Hi all,

I would like to copy external images to PW image fields via API. Under "normal" circumstances the process is absolutely simple and working without the slightest problem. Now in a current project there are image urls where the filename is just a parameter in a generic URL like so:

$img_url = "https://example.com/index.php?cust=foo&id=1234&filename=d9b88153-89e4.jpg";

$page->of(FALSE);
$page->imgs->add($img_url);
$page->save();
$page->of(TRUE);

Result: The image gets copied "correctly", means: the file physically reaches it’s target directory, but it’s filename is expectedly "index.php", therefore the data field in the db image table stays empty. Of course I can isolate the filename d9b88153-89e4.jpg of the URL quite easily with parse_url() and parse_str() but don’t find a way to transfer the filename to the image. $page->imgs->data = $filename, $page->imgs->name = $filename and $page->imgs->filename = $filename are not working.

Share this post


Link to post
Share on other sites

Have you tried to get the last added item from imgs after you saved the page, use the rename method and maybe save it again? (Don't know if a second save must be done or if it is implizit)

$img_url = "https://example.com/index.php?cust=foo&id=1234&filename=d9b88153-89e4.jpg";
$img_basename = "GET-THE-NAME-dot-extension-HERE";

$page->of(FALSE);
$page->imgs->add($img_url);
$page->save("imgs");

$page->imgs->rename($page->imgs->last(), $img_basename);
$page->save("imgs");  // maybe this is not needed ??

Also check that the ->last() item is the one you added before.

Share this post


Link to post
Share on other sites

Thanks @horst – sadly your suggestion is not working, no matter whether I do $page->save('imgs') once or twice, the filename remains index.php persistently. I’ve also tried to select the image with imgs->eq(n) instead of $imgs->last, but no success.

Share this post


Link to post
Share on other sites

@rash, you first must (double)check what you get from $imgs->eq() or any other method. What do you see when you debug it, maybe with var_dump()?
You also may try to use $imgs = $page->getUnformatted("imgs"); 

What objects / values do you get from $page->imgs and from $page->getUnformatted("imgs")? And if you have a multiple based object, how many items are in it, which one is the latest added one?

$page->of(FALSE);
$page->imgs->add($img_url);
$page->save("imgs");

$imgs1 = $page->imgs;
$imgs2 = $page->getUnformatted('imgs');

echo "<pre>";
var_dump($imgs2);
die();

 

  • Like 1

Share this post


Link to post
Share on other sites

Okay @horst, you asked for it 😅, this is $imgs1:

object(ProcessWire\Pageimages)#359 (6) {
  ["count"]=>int(2)
  ["page"]=>string(7) "/probe/"
  ["field"]=>string(4) "imgs"
  ["url"]=>string(24) "/site/assets/files/1338/"
  ["path"]=>string(67) "/www/htdocs/w0097e14/example.de/site/assets/files/1338/"
  ["items"]=>array(2) {
    ["index.php"]=>array(14) {
      ["url"]=>string(33) "/site/assets/files/1338/index.php"
      ["filename"]=>string(76) "/www/htdocs/w0097e14/example.de/site/assets/files/1338/index.php"
      ["filesize"]=>int(0)
      ["description"]=>string(0) ""
      ["tags"]=>string(0) ""
      ["created"]=>string(19) "04-04-2021 13:18:54"
      ["modified"]=>string(19) "04-04-2021 13:18:54"
      ["created_users_id"]=>int(41)
      ["modified_users_id"]=>int(41)
      ["filemtime"]=>string(19) "01-01-1970 01:00:00"
      ["width"]=>int(0)
      ["height"]=>int(0)
      ["suffix"]=>string(0) ""
      ["variations"]=>array(1) {
        [0]=>  string(15) "index.0x260.php"
      }
    }
    ["kunde_example_datensatz_10497_filename_d9b88153-89e4-4129-aa70-06ab8b137181.jpg"]=>array(13) {
      ["url"]=>string(115) "/site/assets/files/1338/kunde_example_datensatz_10497_filename_d9b88153-89e4-4129-aa70-06ab8b137181.jpg"
      ["filename"]=>string(158) "/www/htdocs/w0097e14/example.de/site/assets/files/1338/kunde_example_datensatz_10497_filename_d9b88153-89e4-4129-aa70-06ab8b137181.jpg"
      ["filesize"]=>int(0)
      ["description"]=>string(0) ""
      ["tags"]=>string(0) ""
      ["created"]=>string(19) "04-04-2021 18:13:05"
      ["modified"]=>string(19) "04-04-2021 18:13:05"
      ["created_users_id"]=>int(41)
      ["modified_users_id"]=>int(41)
      ["filemtime"]=>string(19) "01-01-1970 01:00:00"
      ["width"]=>int(0)
      ["height"]=>int(0)
      ["suffix"]=>string(0) ""
    }
  }
}

And this is $imgs2:

object(ProcessWire\Pageimages)#365 (6) {
  ["count"]=>int(2)
  ["page"]=>string(7) "/probe/"
  ["field"]=>string(4) "imgs"
  ["url"]=>string(24) "/site/assets/files/1338/"
  ["path"]=>string(67) "/www/htdocs/w0097e14/example.de/site/assets/files/1338/"
  ["items"]=>array(2) {
    ["index.php"]=>array(14) {
      ["url"]=>string(33) "/site/assets/files/1338/index.php"
      ["filename"]=>string(76) "/www/htdocs/w0097e14/example.de/site/assets/files/1338/index.php"
      ["filesize"]=>int(0)
      ["description"]=>string(0) ""
      ["tags"]=>string(0) ""
      ["created"]=>string(19) "04-04-2021 13:18:54"
      ["modified"]=>string(19) "04-04-2021 13:18:54"
      ["created_users_id"]=>int(41)
      ["modified_users_id"]=>int(41)
      ["filemtime"]=>string(19) "01-01-1970 01:00:00"
      ["width"]=>int(0)
      ["height"]=>int(0)
      ["suffix"]=>string(0) ""
      ["variations"]=>array(1) {
        [0]=>string(15) "index.0x260.php"
      }
    }
    ["kunde_example_datensatz_10497_filename_d9b88153-89e4-4129-aa70-06ab8b137181.jpg"]=>array(13) {
      ["url"]=>string(115) "/site/assets/files/1338/kunde_example_datensatz_10497_filename_d9b88153-89e4-4129-aa70-06ab8b137181.jpg"
      ["filename"]=>string(158) "/www/htdocs/w0097e14/example.de/site/assets/files/1338/kunde_example_datensatz_10497_filename_d9b88153-89e4-4129-aa70-06ab8b137181.jpg"
      ["filesize"]=>int(0)
      ["description"]=>string(0) ""
      ["tags"]=>string(0) ""
      ["created"]=>string(19) "04-04-2021 18:08:41"
      ["modified"]=>string(19) "04-04-2021 18:08:41"
      ["created_users_id"]=>int(41)
      ["modified_users_id"]=>int(41)
      ["filemtime"]=>string(19) "01-01-1970 01:00:00"
      ["width"]=>int(0)
      ["height"]=>int(0)
      ["suffix"]=>string(0) ""
    }
  }
}

Obviously there is something unwanted happening after $page->images->add(), as the whole URL gets transformed to a string with _ dividers and affects as well as filename I can see this is crap but not how to prevent it and why it doesn't work to at least rename filename (renaming url should be not problem after). (Btw: Don’t ask me why this text appears as a link …)

Share this post


Link to post
Share on other sites

Yep, there are two images added. Why?

If you want to rename that one that is named "index.php", you can try to get it by name from the Pageimages object.

$img = $page->imgs->get("name=index.php");

// during testing, fisrt check what you have get in $img !!

$page->getUnformatted("imgs")->rename($img, 'newbasename.jpg');
$page->save();

If this doesn't work, maybe because php is no valid image extension, you have to get the image files manually from the URL, beautify  the file(base)name and add it after that to the images field.

  • Like 1

Share this post


Link to post
Share on other sites

@horst: Thanks a lot, that was very helpful. First I lost overview a bit, then I found this older thread with you being involved too, that brought the final kick. Now this is working fine:

$img_url = "https://example.com/index.php?cust=foo&id=1234&filename=d9b88153-89e4.jpg";
$img_query = parse_url($img_url, PHP_URL_QUERY);
parse_str($img_query, $params);
$filename = $params["filename"]; // result: d9b88153-89e4.jpg

$page->of(FALSE);
$page->imgs->add($img_url); // add image to multi-image field $imgs
$page->save("imgs"); // save added image with unwanted name index.php

// w/o trackChange() file seems to get renamed in file system but not in db
$page->imgs->trackChange("basename");

// eq(0) is the right image in my case, other selectors are possible
$image = $page->imgs->eq(0);
$image->removeVariations(); // delete variations (thumbnails etc.)
$image->rename($filename); // rename index.php to d9b88153-89e4.jpg

$page->save("imgs"); // save changes to $imgs field

I’m pretty glad to have that annoyance solved, so thank you very much once more.

PS: The mess in the dumps above resulted from a typo in deleteAll() before saving the new image, so more than one image was saved.

  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...