Philipp Posted October 26, 2014 Share Posted October 26, 2014 I'm currently building an Textformatter to embed images with a srcset attribute. In order to do this, I extract images from the string, then search for the image itself and rebuild the <img> before inserting it back into the string. My problem is now finding the right image inside a given page. I have The image file name, e.g. dcim_2299.jpg, called $imageName The page of the image, called $p Multiple PageImage fields assigned to one or more templates. How can I access/get the right PageImage from the right image field on the page? At the moment I've hardcoded the field name into the module. A look at another, similar module doesn't help because there is a bug. When there are multiple image fields assigned to the template, It will always select the first image from the first field. I have no clue why this happens. Code from the TextformatterImageInterceptor that is not working as expected. The bug is also reported here. // basename of image. $imageName = preg_replace("/\d+x\d+\./i","", $variationName); // All image fields of the page $imageFields = $p->fields->find("type=FieldtypeImage|FieldtypeCropImage"); // find the image object foreach($imageFields as $imageField) { // if Maximum files allowed, just grab it. if($imageField->maxFiles == 1) { $image = $p->$imageField; // dealing with files array } else { $image = $p->$imageField->get("$imageName"); } if($image instanceof Pageimage) break; } I would appreciate any help. Thanks. Link to comment Share on other sites More sharing options...
horst Posted October 26, 2014 Share Posted October 26, 2014 Hi Philipp, at first, to get the original name from a variations name, you simply have to drop all after the first dot in the basename: (this works with all PW versions, 2.2, 2.3, 2.4 and also with the changed NamingScheme in PW 2.5.+) $imageName = pathinfo($variationName, PATHINFO_BASENAME); // I do not use PATHINFO_FILENAME here because this way it also matches original filenames $imageName = substr($imageName, 0, strpos($imageName, '.')) . '.' . pathinfo($variationName, PATHINFO_EXTENSION); // determines where the first dot is and drops all after that position, and simply add the extension // the only way to find all image fields, also those coming in the future, is to check for the instance of FieldtypeImage, what also is true if a field extends FieldtypeImage: foreach($page->fields as $field) { if (! (bool) ($field->type instanceof FieldtypeImage)) continue; // if no images field, go on //if (count($page->get($field->name)) == 0) continue; // this isn't needed I think because the next check with ->has() covers that case too // find the field that holds the image if (!$page->get($field->name)->has('name=' . $imageName)) continue; // if you reach this line, you have your field and your original image If you are in template scope, you may test to switch of outputformatting when asking with ->has(), or it could raise an error if the imagefield only allows a single image and doesn't return a wireArray. (I'm not sure on this, - just want to note not to forgett to test on this) 3 Link to comment Share on other sites More sharing options...
Martijn Geerts Posted October 26, 2014 Share Posted October 26, 2014 Phillip, thanks pointing out to a bug. Didn't know about it. I will dive into this one. Thanks you reported it on GitHub to. Link to comment Share on other sites More sharing options...
horst Posted October 26, 2014 Share Posted October 26, 2014 As a sidenote, regarding the detection of imagefields if (! (bool) ($field->type instanceof FieldtypeImage)) continue; // the only way I know to find all image fields, also those coming in the future, is to check for the instance of FieldtypeImage, what also is true if a field extends FieldtypeImage . As of today (date of this post) we allready have those known image fieldtypes that extend the core images fieldtype: Cropimage -> (also known as Thumbnails) ImageFocusArea -> (formerly known as ImageFocusrect) ImageExtra -> ImageExtraLanguage -> CroppableImage (a fork of Cropimage, coming soon) Images section is growing! 3 Link to comment Share on other sites More sharing options...
Philipp Posted October 26, 2014 Author Share Posted October 26, 2014 Thank you all! The code from horsts first post was nearly the solution. I needed the filename + the extension. With a uploaded file, e.g. example.jpg I've extracted the variation name from the src-attribute (example.321x0.jpg). Making it example.jpg again and then use horst code it worked. $tmp1= explode('.',$variationName); $meta['imageName'] = $tmp1[0]; $meta['ext'] = $tmp1[count($tmp1)-1]; //Thanks to Horst Nogajski for the following snippet foreach($p->fields as $field) { if (! (bool) ($field->type instanceof FieldtypeImage)) continue; // if no images field, go on // find the field that holds the image if (!$p->get($field->name)->has('name=' . $meta['imageName'] . '.' . $meta['ext'])) continue; $image = $p->get($field->name)->get('name=' . $meta['imageName'] . '.' . $meta['ext']); } 2 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now