seddass

Import images from file system using the PW API

Recommended Posts

Hello guys, hope you could help me. I will appreciate it.

I am trying to migrate the data from one of my oldest sites to PW. It is about of hundreds of records, please, dont suggest to do it using the admin area. :) Based on Ryan's code (http://processwire.com/talk/index.php/topic,24.0.html) i have handled creating the pages, but I am still wandering how to "upload/import/insert" images from the file system and to attach them into the $page->images field using the PW API. PW is so scalable and powerful, there should to be a simple way of implementing that. I think i should create Pageimage objects and to "add" them in Pageimages but I missing something. Thanks in advance.

Briefly with sample code:

$myPage = wire('pages')->get('template=myPageTemplate');
$imagesOld = array('image1.jpg', 'image2.jpg', ....);

$myPage->images = $imagesOld; // i know this is wrong

$myPage->save();

Share this post


Link to post
Share on other sites

After the morning has come... i think i have figured out. It is simple as i expected.


$pageImages = new Pageimages($pageWithImagesField);

begin loop
$pageImages->add($fullPathToOldImage);
end loop

$pageWithImagesField->images = $pageImages;
$pageWithImagesField->setOutputFormatting(false); // not sure why but there was error without it while saving the page
$pageWithImagesField->save();

or alternate way in the loop

begin loop
$pageImage = new Pageimage($pageImages, $fullPathToOldImage);
$pageImage->rename(substr(basename($fullPathToOldImage), 0, -11) . ".jpg"); // some extra manipulations
$pageImages->add($pageImage);
end loop

Hope it will be helpful for someone.

Share this post


Link to post
Share on other sites

Thanks for your post. What you are doing looks right, except that you can skip creating the Pageimages field. PW would rather do that internally. So this is the entirety of what you should do (after you have your $page object):

<?php
$page->images->add("path or URL to image"); 
$page->save(); 

If you are doing this from a template file (as opposed to a shell script or admin module), you have to turn the output formatting off first, as you saw. This is to ensure that you don't accidentally add runtime-formatted content to a page and save it. By "runtime-formatted", I mean entity encoded, markdown, etc. This example might better explain it:

<?php
$mypage = $pages->get(123); 
echo $mypage->title; // value is "Products & Services"
$mypage->setOutputFormatting(false);
echo $mypage->title; // value is "Products & Services"

Now lets say that you did this instead:

<?php
$mypage = $pages->get(123);
$mypage->title = "Great " . $mypage->title; 
$mypage->save();

If ProcessWire let you save that page, the 'title' field would now have this value the next time you viewed it:

Great Products && Services

Basically, PW only wants to save content in an unformatted "clean" state. It throws that error asking you to set output formatting OFF in order to protect against you corrupting your content. So to follow up from the first example, this is what you should do if you are doing this import from a template file:

<?php
$page->setOutputFormatting(false); 
$page->images->add("path or URL to image"); 
$page->save(); 
  • Like 2

Share this post


Link to post
Share on other sites

hi naldrocks98,

on the api side image fields are always multi no matter what the settings say!

$society->society_logo->removeAll(); // add this line
$society->society_logo->add("http://www.thefilipi...ogo/accp.jpg");
$society->save();

explanation by ryan: https://processwire.com/talk/topic/2597-multiple-image-bug-when-using-api-resolved/?p=25452

  • Like 1

Share this post


Link to post
Share on other sites
$society->removeAll(); // add this line
$society->society_logo->add("http://www.thefilipinodoctor.com/medsoc_logo/".$get_soc['logo'].""); 
$society->save();

Thanks.. BernhardB

I edit your code bec. there's an error

$society->removeAll();

to

$society->society_logo->removeAll();

By the way thanks to you... you solved my problem...

How about file upload like doc, pdf ?

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.