Jump to content
rushy

[solved] storing image properties description

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();

 

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

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.

  • Similar Content

    • By olafgleba
      Hi,
      maybe kind of related to https://processwire.com/talk/topic/13286-image-description-not-saving/ but with other environment bits.
      While adding a new page/edit a existing page containing a image field, the image itself gets stored into the database but strangely the entered description does not. More strangely this only happens on my staging server. Within my local dev environment all is fine. And beside of this particular problem, all parts of the PW instance works fine. On both environments (s. below).
      First suspicion was the varying PHP/mysl versions of the local <-> staging environment. But, there are many working projects with the identical setting (s. below).  And i never encounter this problem so far.
      No error/log/console output. It just fails to get stored.
      Local Environment: PHP  7.4.6, mysql 5.7.29, PW 3.0.148
      Staging Environment*: PHP 7.0.33, maria DB 10.1.46, PW 3.0.148
      Maybe someone has a hint what to try?  Thanks in advance.
      *) I cannot change the staging server php/mysql settings because its part of a shared hosting account.
    • By arnd
      Hi,
      I have a repeater with some images in it.
      As Superuser I can work with the Actions (rotate...). But my Users with lower Rights can't use this Actions.
      The same User can use the Actions on images outsite a Repeter-Field. So I think it's not a Problem of Rights-Management but from Images in Repeater Fields.
      ProcessWire 3.0.164.  Bug? Or can I manage this in the settings?
      Thanks in advance
    • By Mats
      BETA: SplashAndGrab
      https://github.com/madebymats/InputfieldSplashAndGrab
      This module attaches a search input to selected image fields that lets you search and download images from Unsplash.
      (Unsplash is a stock photo service where you can download images for free and use as you wish. No strings attached.)   
      You can search by string, colors, orientation/crop and order by relevance or time published
      I find Unsplash useful both for placeholder images when building sites but also as a time saver for editors if they don’t have any images at hand, just search, download and publish.

      Thanks to @apeisa for building the  FlickrInputField Module and @Robin S for AddImageUrls,  took a lot ideas and code from those modules.
    • By markus_blue_tomato
      We have created a module to create BlurHash strings for images while uploading in ProcessWire. This blurry images will be saved in the database because they are very small (20-30 characters) and can be used for Data-URL's  as placeholders for image-lazy loading.
      https://github.com/blue-tomato/ImageBlurhash
      E.g. where we use this in production:
      https://www.blue-tomato.com/en-INT/blue-world/ https://www.blue-tomato.com/en-INT/blue-world/products/girls-are-awesome/ https://www.blue-tomato.com/en-INT/buyers-guides/skateboard/skateboard-decks/ https://www.blue-tomato.com/en-INT/team/anna-gasser/
×
×
  • Create New...