Jump to content

Programmed page creation: import image from URL

Adam Kiss

Recommended Posts

Hi all,

I need to transfer 150+ page website from wordpress to PW CMS, although there are things that I'd like to change in PW (but that is another topic).

Anyway, since I used two or three plugins with page-specific content and WP export is not very nice (It basically exports internal values, rather than computed values, e.g. if tag 'news' is nr. 7, it exports 7 rather than 'news'), so I'll write my own WP wrapper to create my own XML format.

95% of these pages contain PNG image (exactly one), and I'm too lazy to create 150 pages and 150 times import images, so my question is this: Could anyone come up with code, which basically does this:

  • get image URL from XML (dead-simple)
  • transfer image into some temporary location
  • set the value in the $page object
  • save page
  • delete temp image

The major problem I have is this: I'm not sure about all the mechanics of PHP-populated imagefields, e.g.: will the image be stored in the files/page-id/ dir, or will it be just linked? is the population operation 'move' the image (no delete of tmp image needed) or is it rather 'copy' (need to delete orig. tmp image)?

This question goes probably to Ryan or Antti, although any other power users here are welcome to join the discussion.

Link to comment
Share on other sites

Just a wild guess, but probably image field accepts url also (of course url needs to be public), so you don't have to do that transfer / temporary location  / delete temp image stuff at all.

And to my understanding if you add image and save page, then that image is copied to /site/assets/files/page-id/ folder. So you would have to remove temp file if you don't want to keep duplicates.

If image field doesn't accept urls then you have to go harder way. But I probably wouldn't add that downloading / removing to script (at least if original png files are in single folder) - it would be easy to just remove the folder where all temp images are saved after import is successful.

Link to comment
Share on other sites

Well, the problem is, that the PNG are managed by WP, so both originals and resized versions are in one folder (save for one image), so actually this might be quite easy – just export file name... and there we go... I'll go and lookup programmed fieldtype file/s population.

Link to comment
Share on other sites

When importing images to pages, make sure the page exists first. So if you are creating a new page, save it in your API code ($page->save()) before adding the image to it. Otherwise, PW won't know where to put the image(s) since the /site/assets/files/* directories are based on the page ID.

If you are adding an image to a single-image field, you can just set the value of it to the URL, i.e.

$page->image = 'http://www.something.com/some-image.png';

If you are adding an image to a mult-image field, you can add() it:


Either of the above makes PW copy the image to it's assets and create the proper object.

And then don't forget to:

  • Like 4
Link to comment
Share on other sites

  • 1 year later...

Sorry to resurrect this thread, I've done a fair bit of searching around the forums and this thread outline my situation almost perfectly. Up to a point.

I have a single image field (set to accept only one image in the setting) and each time I run my import script a new duplicate image is added to this "thumbnail" field.

I have followed Ryans post as it is above, using

 $page->thumbnail = $sanitizer->url($entry->images->profile);

Is there something I'm missing, possibly something that has changed in the API since this post? I tried using:


on a whim but that didn't work (couldn't hurt to try :rolleyes: )

EDIT: if I use


before adding the image I end up with just one image, but this seems kinda wrong... but I'm new, so maybe its fine and I'm just wrong

Link to comment
Share on other sites

I think the problem may be $entry->images->profile. The "->profile" portion of that doesn't match up with any ProcessWire 2 syntax (though would have in ProcessWire 1, but thats years ago). Where is "profile" coming from? If your image field is actually named "profile" then you should be referencing $entry->profile. Or, if your images field is literally named "images" then you would access it by $entry->images.

Also the code examples you attributed to me are different enough from the ones I posted that I don't understand them. :) Based on the examples you are posting, I'm guessing maybe what you want is instead this?

$width = 100;
$height = 100; 
if($page->profile) {
 $thumbnail = $page->profile->size($width, $height); 
 echo "<img src='$thumbnail->url' alt='$thumbnail->description' />";

The code example above assumes 'profile' is the name of an image field set to contain a max of 1 image. If you want it to contain more than 1 image, then you'd need to adjust the code example to pull one image from it, like:

if(count($page->profile)) { 
 $thumbnail = $page->profile->first()->size($width, $height); 
 // ...

If I'm on the wrong track here, us know exactly what you are trying to do (without code examples) and we should be able to reply with a code example of how to do it.

Link to comment
Share on other sites

Sorry Ryan, I wasn't very clear, the


part is from the xml object that I am referencing, this is for an import from a live wordpress site, but I am using a custom export XML structure because of limitation in the default wordpress export xml.

xml example


Basically a "listing" has various images (profile, banner, etc). So basically I'm just sending a image URL through to the import script (www.site.com/image.jpg) and on first run it fine, but on second run of the import script I have a second "profile" image. Instead of the image just being updated I get incremental additions (1_image.jpg, 2_image.jpg and so on).

From my understanding of what you posted above I should get this if the imagefield is set to accept one file. What you just posted looks like conditional code to only output the image if it exists, I'm trying to save the image to the page or replace it if it already has one.

Thanks for taking a look at this, I appreciate it.

Link to comment
Share on other sites

It sounds like you are adding images to a PW page. Here's how you'd do that below. When you are manipulating values on a page, you usually want to have output formatting off. When that is the case, an image field will always behave as an array of images rather than 1 image. So the code below would be the same regardless of whether dealing with a single image field or a multi image field. The example also assumes your images field has the name 'images', but it can be whatever you want it to be.

$page->of(false); // turn of output formatting if in template context, not necessary otherwise
$page->images->add('/path/to/file.jpg or http://domain.com/path/to/file.jpg'); 

Note that $page has to exist (and have an 'id') before an file/image is added to it. So if you were creating a new Page, you'd have to do it like this:

$page = new Page();
$page->template = 'name-of-template';
$page->parent = '/path/to/parent/';

If you want to delete an image, do the delete and save before adding another:

$page->save(); // commit the deletion

Or if you want to delete a specific image:

$image = $page->images->first();
$page->save(); // commit the deletion

Note that when you add() an image, if you are pulling from an http:// address then your PHP must support allow_url_fopen. Some web hosts have this disabled, though most don't.

  • Like 4
Link to comment
Share on other sites

  • 7 years later...

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