Jump to content

[solved] storing image properties description


rushy
 Share

Recommended Posts

Bit of a newbie type question, even though I've been using PW for quite a while, I've not had to manipulate assets from the front end before.  I now have a need to update image properties from the front end and I'm trying to update an image description and tags when clicking on a link. I get the selected image and for example I can delete it with the code below. But I am missing some basic understanding when updating image description field as nothing happens, no errors but the description field remains empty. Any idea what steps I am missing? Many thanks. 

// how to update image description?
$al = $pages->get($album);
$pgfile = $al->images->getFile($file);

$al->of(false);
$pgfile->description = "Test description";
$al->save();

// to delete an image - this works
$al->of(false);
$al->images->delete($file);
$al->save();

 

Link to comment
Share on other sites

Hi, here is some code written in the browser that may do it. You should doublecheck if your $file variable is the right file basename!

$p = $pages->get($album);								// get the page
$p->of(false);

$p->images->trackChange('description');       			// prepare page to keep track for changes

$selectedImage = $p->images->getFile($fileBasename);  	// Get the Pagefile having the given !basename!, or null if not found.
$selectedImage->description = "Test description";		// set new description

$p->save('images');                           			// save the page
$p->of(true);

 

  • Like 2
Link to comment
Share on other sites

Thanks Horst.  My file basename is correct as I am able to echo out all the properties of the image, but I did not know about trackChange perhaps that is the missing bit.

$pgfile = $al->images->getFile($file);
if($pgfile) {
	echo "basename => $pgfile->basename \n";		
	echo "description => $pgfile->description \n";
	echo "ext => $pgfile->ext \n";
	echo "filename => $pgfile->filename \n";
	echo "filesize => $pgfile->filesize \n";
	echo "filesizeStr => $pgfile->filesizeStr \n";
}

The above echoes out all the properties as expected , but assigning description to something does not update it when I save page. I guess I'm only changing a local copy here and it's not "connected" to the page. I imagine the trackChange makes that happen. I will try it and report back.

Many thanks

  • Like 1
Link to comment
Share on other sites

I confirm that this works - when I called trackChange before save the change to the image description field was savedA. Thanks again Horst.

As a footnote, when updating tags I found that I can use $image->addTag('foo,bar,baz') without trackChange and tags were saved because it's a dedicated function to add tags.

Regards

  • Like 1
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Similar Content

    • By JeevanisM
      Hi all,
      just another topic over the beer. I started using Bootstrap from the version 3 but really loved it. BS 5 seems really powerful but I cannot ignore flashy trend of  TailwindCSS. I tried to learn it on a new project but I am not able to make any progress so far. Every one saying TailwindCSS is super easy and whatnot.  What are your thoughts on these ?  which one you like and why ? 
    • By spercy16
      These issues should be fairly easy for any intermediate to advanced ProcessWire developer to answer. I'm new to PHP and relatively new to ProcessWire and just need a bit of help. What I'm trying to do is bring in a couple of cards from my Projects page to display on my home page. I finally got the code right to bring in the cards but right now they're using my original images instead of my resized "variations". So firstly, I would like to know how to reference the variations of my images instead of using the original. Secondly, I need to grab only four of the cards from the Project page and not import in all ten. It should be just two small changes to my code to do these things (I would imagine). Here is the code I currently have for that section:
      <?php // https://processwire.com/api/arrays/ // check if the array of images has items if (count($pages->get("/projects/")->images)) : // get array of images from the field $images = $pages->get("/projects/")->images; $count = 0; // iterate over each one foreach ($images as $image) : $count++; $sectionText = $pages->get("/projects/")->get("paragraph_$count"); $img = $image; $buttonCode = $pages->get("/projects/")->get("url_$count"); ?> <span id="card<?php echo $count?>" class="card"> <img class="cardThumb" src="<?php echo $img->url; ?>" alt="<?php echo $image->description; ?> Thumbnail" /> <div class="cardBody"> <div class="cardText"> <h2><?php echo $img->description; ?></h2> <?php echo $sectionText; ?> </div> <div class="primaryBtn"> <a href="https://www.paypal.com/donate?hosted_button_id= <?php echo $buttonCode; ?> &source=url"> <button> <i class="fas fa-donate"></i> Donate </button> </a> </div> </div> </span> <?php endforeach; endif; ?> Thanks in advance for any help!
    • By picarica
      so hello there i have fiel field type i have lots of stuff there, and also an image i can easily get image from that field using
                                                        
      $options = array('quality' => 85, 'upscaling' => true, 'cropping' => 'north', 'sharpening' => 'medium'); $word = ".png"; // Test if string contains the word foreach($childgames->subor_hry as $file) { if(strpos($file, $word) !== false){ /* $imger = $file->size(473, 266, $options); */ echo $file; /* echo $file->url; */ } } so i get the fiel i tried invoking size on it like the commented out part and it doesnt work i get error Oh snizzle… Error:     Exception: Method Pagefile::size does not exist or is not callable in this context (in
      what am i doing wrong? can you guys help me by all logic this should work
    • By Max Esnée
      Hi,
      When I upload a large(ish) SVG (≈120ko in this case) into an image field, it tends to get stuck at 100% and block the whole website. If I force refresh the admin page, the svg has not been uploaded. The file is correctly copied into the assets folder though. Other svg tends to be uploaded fine albeit I've noticed that pages containing svg images in the admin are very slow to load generally. Other pages using the same template but with jpeg or png images are much faster to load.
      I get the following error in the JS console:

      I develop locally using MAMP on macOS and I'm using Firefox. I'm using the last ProcessWire version (3.0.165).
      I also tested in Chrome and got the same error.
    • By Marvin
      Hello, my name Marvin, i want to ask something. I'm new at processwire, and still learn it, i try yo showing an image, at a table, the image was show, but i can't resize the image
      please HELP
      Here i attach, my code belor
      <?php $num = 1; foreach($pages->get("/files/")->children as $child) { $current = $child === $page ? " class='current'" : ''; $result = $child->images; // $result->width(900); // $result->height(100); foreach($result as $items){ foreach($pages->get($child->name)->files as $file) { // $file = $child->files; // echo $file->name; echo "<tr><td>".$num++.".</td><td>".$child->title."</td><td>".$child->text_1."</td><td>".$child->text_2."</td><td>".$child->text_3."</td><td><a href='".$file->httpUrl."'>".$file->name."</a></td><td><img src='".$items->url."'></td></tr>"; } } } ?>  
×
×
  • Create New...