留言板

how can I display image dimensions in a web content

Balázs Csönge,修改在8 年前。

how can I display image dimensions in a web content

Regular Member 帖子: 107 加入日期: 14-11-10 最近的帖子
Hi,

We defined a web content structure and template to select an image from Documents and Media and display its properties for example, size, description, dimensions, etc.

I was able to get file entry via velocity script, and was able to display the file size for example, but was not able to display the image height and width.

In a java code I can do it the following way:
			DLFileEntry dlfe = DLFileEntryLocalServiceUtil.getDLFileEntry(Long.parseLong(testData));
			try {
				Image img = ImageToolUtil.getImage(dlfe.getContentStream());
				actionRequest.setAttribute("groupIdStr", "Height: " + img.getHeight());
				actionRequest.setAttribute("companyIdStr", "Wifth: " + img.getWidth()); //companyIdStr
				actionRequest.setAttribute("userIdStr", "Extension: " + dlfe.getExtension());
			} catch (IOException e) {
				log.error("Error occured during geting the image from file. Cause: " + e.getMessage(), e);
			}


The velocity code snippets:
#set($dlFileEntryLocalService = $serviceLocator.findService("com.liferay.portlet.documentlibrary.service.DLFileEntryLocalService"))
#set($imageToolUtil = $serviceLocator.findService("com.liferay.portal.kernel.image.ImageToolUtil"))

        	    #set($fileEntry = $dlFileEntryLocalService.getFileEntryByUuidAndGroupId($tomb2.get(0), $groupId))
        	    #set($fe = $fileEntry.getContentStream())
        	    #set($image = $imageToolUtil.getImage($fe))
        	    fe: $fe
        	    <br>
        	    image: $image
        	    <br>
        	    extension: $fileEntry.extension
        	    <br>
        	    File size: $fileEntry.size
        	    #if (!$fileEntry.description.isEmpty())
            	    <br>
            	    File description: $fileEntry.description
        	    #end


Please help me, how can I fix the problematic #set($image = $imageToolUtil.getImage($fe)) commad to work?
thumbnail
Andew Jardine,修改在8 年前。

RE: how can I display image dimensions in a web content

Liferay Legend 帖子: 2416 加入日期: 10-12-22 最近的帖子
Hi Balazs,

Do you have the "id" for the image? If you do, you could try using teh sericeLocator to load the ImageLocalService and then use the getImage(long id) method to get the image record from the database (in place of the physical file). The table appears to store the height and width properties with the record.
thumbnail
Ravi Kumar Gupta,修改在8 年前。

RE: how can I display image dimensions in a web content

Liferay Legend 帖子: 1302 加入日期: 09-6-24 最近的帖子
Try to get the metadata of the file. Use DLFileEntryMetadataLocalServiceUtil.getFileEntryMetadata(). Check Document Library portlet for reference.
view_file_entry.jsp file in tomcat-7.0.40\webapps\ROOT\html\portlet\document_library

HTH
Balázs Csönge,修改在8 年前。

RE: how can I display image dimensions in a web content

Regular Member 帖子: 107 加入日期: 14-11-10 最近的帖子
All file in Docments and Media has a FileEntryType.
Metadata (document type) attributes can be get via this FileEntryType, like this:
			DLFileEntry dlfe = DLFileEntryLocalServiceUtil.getDLFileEntry(Long.parseLong(testData));
			DLFileEntryType fileEntryType = DLFileEntryTypeLocalServiceUtil.getDLFileEntryType(dlfe.getFileEntryTypeId());
			List<ddmstructure> ddmStructures = fileEntryType.getDDMStructures();
			for (DDMStructure ddmStructure : ddmStructures) {
				DLFileEntryMetadata fileEntryMetadata = DLFileEntryMetadataLocalServiceUtil.getFileEntryMetadata(ddmStructure.getStructureId(),dlfe.getFileVersion().getFileVersionId());
				Fields fields = StorageEngineUtil.getFields(fileEntryMetadata.getDDMStorageId());
				for (Field f : fields) {
					f.getName();
					f.getType();
					f.getValue();
					f.getDataType();
				}
			}
</ddmstructure>


When a fileEntryType is a BASIC_DOCUMENT, then ZERO DDMStructure belongs to it, so no DLFileEntryMetadata can be received.

Any anyway, metadata (document type) will contain height and width for a document, if you specify those attributes in the metadata structure of the given document type, and you fill them at upload.
thumbnail
Prakash Khanchandani,修改在8 年前。

RE: how can I display image dimensions in a web content

Expert 帖子: 329 加入日期: 11-2-10 最近的帖子
Ravi Kumar Gupta:
Try to get the metadata of the file. Use DLFileEntryMetadataLocalServiceUtil.getFileEntryMetadata(). Check Document Library portlet for reference.
view_file_entry.jsp file in tomcat-7.0.40\webapps\ROOT\html\portlet\document_library


I am not sure if this would help in getting the height or width for the image since this deals with the DDM structure i.e. the Document Type (Basic Document etc) fields which would displayed when we view an image or file.

Thanks
Balázs Csönge,修改在8 年前。

RE: how can I display image dimensions in a web content

Regular Member 帖子: 107 加入日期: 14-11-10 最近的帖子
No, i not have image Id because the image was uploaded into Documents and Media, and it stored as DLFileEntry, not as Image.
I examined the content of the IMAGE table...
So ImageLocalService will not get the image. emoticon
thumbnail
Prakash Khanchandani,修改在8 年前。

RE: how can I display image dimensions in a web content

Expert 帖子: 329 加入日期: 11-2-10 最近的帖子
You can write the following code to find out the height and width of the file from the InputStream:
BufferedImage bufImg = ImageIO.read(dlfe.getContentStream());
int width = bufImg.getWidth();
int height = bufImg.getHeight();


But how would you do this in velocity?

Write the above code in any of your custom service classes and fetch it using the finderService or you can create a custom java utility as explained here to make it available in your velocity template.

Hope this helps.
Balázs Csönge,修改在8 年前。

RE: how can I display image dimensions in a web content

Regular Member 帖子: 107 加入日期: 14-11-10 最近的帖子
Ok, in my code #set($imageToolUtil = $serviceLocator.findService("com.liferay.portal.kernel.image.ImageToolUtil")) found the requested util.

But not understand, why the $imageToolUtil.getImage($fe) method call not working...
$imageToolUtil is exists, $fe is exists too.
thumbnail
Prakash Khanchandani,修改在8 年前。

RE: how can I display image dimensions in a web content

Expert 帖子: 329 加入日期: 11-2-10 最近的帖子
oops! ImageToolUtil is essentially doing the same thing which I was suggesting emoticon

Can you pass null instead of $fe in $imageToolUtil.getImage(null), just to check if the method is called and you get some output and not $image.

Also please check if there are no errors (IOException - Unable to decode image) in the logs.

Also try using the overloaded method public Image getImage(InputStream is, boolean cleanUpStream)

HTH
Balázs Csönge,修改在8 年前。

RE: how can I display image dimensions in a web content

Regular Member 帖子: 107 加入日期: 14-11-10 最近的帖子
Hi,

Tried:
$imageToolUtil.getImage(null)
$imageToolUtil.getImage($fe,false)
$imageToolUtil.getImage($fe,"false")
$imageToolUtil.getImage($fe,true)

None of them worked... emoticon

Log did not changed emoticon
thumbnail
Prakash Khanchandani,修改在8 年前。

RE: how can I display image dimensions in a web content

Expert 帖子: 329 加入日期: 11-2-10 最近的帖子
How about:
$imageToolUtil.getImage(null, true)
Balázs Csönge,修改在8 年前。

RE: how can I display image dimensions in a web content

Regular Member 帖子: 107 加入日期: 14-11-10 最近的帖子
emoticon
And nothing in logs, no error, no warning, no info...
Balázs Csönge,修改在8 年前。

RE: how can I display image dimensions in a web content

Regular Member 帖子: 107 加入日期: 14-11-10 最近的帖子
$imageToolUtil.getImage($getterUtil.getObject($fe))
$getterUtil.getObject($imageToolUtil.getImage($fe))

did not worked either
thumbnail
Prakash Khanchandani,修改在8 年前。

RE: how can I display image dimensions in a web content

Expert 帖子: 329 加入日期: 11-2-10 最近的帖子
The method is not executing at all due to some compilation issue I think (since it is printing the method as it is), or else it would have returned some value.

Not sure what might be causing it. Can you try the same code in the theme-template if it works.

Also which version of Liferay are you using?
Balázs Csönge,修改在8 年前。

RE: how can I display image dimensions in a web content

Regular Member 帖子: 107 加入日期: 14-11-10 最近的帖子
The folder of my Liferay is: liferay-portal-6.2-ce-ga2
Balázs Csönge,修改在8 年前。

RE: how can I display image dimensions in a web content

Regular Member 帖子: 107 加入日期: 14-11-10 最近的帖子
Hi, I am very interested in the solution, what you linked before. But the guide was writen to verson 6.1 and I using 6.2.
Because I am very newbee in liferay world, I cannot jump the gap without help.

We have some different maven project which are relate to each other in a given way. One of them is a Liferay Portlet. Is it possible to make the ScriptUtil under that project? If yes, how can I do that exactly?

If not, guide says, I have to create a Hook project. In 6.2 exclipse offers a bit different list than I can see in the linked guide. Not sure, what can I choose, etc.
thumbnail
Prakash Khanchandani,修改在8 年前。

RE: how can I display image dimensions in a web content

Expert 帖子: 329 加入日期: 11-2-10 最近的帖子
But the guide was writen to verson 6.1 and I using 6.2


Nothing has changed regarding the scripting hook from 6.1 and 6.2. The steps remain same, it might be that the IDE has changed.

Is it possible to make the ScriptUtil under that project? If yes, how can I do that exactly?


Yes, its possible and you don't necessarily need a liferay-hook since this script-java does not use any liferay-hook features instead it just makes the bean available to the portal for use.

So what you can do is, you can skip the steps till Figure 1.8 in the link and follow all the steps after the Figure 18.1: Creating a new utilities project is easy if you use Liferay IDE or Liferay Developer Studio.

Hope this helps.
Balázs Csönge,修改在8 年前。

RE: how can I display image dimensions in a web content (答复)

Regular Member 帖子: 107 加入日期: 14-11-10 最近的帖子
Hi,

Yesterday, about 1 hour later, when my my last question has been made, I was able to build a working "setup", but had no time to post it here.

A remark:
I found the guide in the liferay 6.2 documentation with totally same name, and same first half, and thought, ooo, it is same, what you linked. But after second look, I relaized its is not same.
Differences starts after class creation, when 6.1 guide start talking about applicationContext.xml, but 6.2 guide about hook-spring.xml.

Stesp to success:
1. I created an interface and class file (as guide shows) under our portlet maven project in package hu.fornax.bmp.common.helper

2. I created a hook-spring.xml under META-INF (see linked pic)
<!--?xml version="1.0" encoding="UTF-8"?-->

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" default-destroy-method="destroy" default-init-method="afterPropertiesSet" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
        <bean id="hu.fornax.bmp.common.helper.VelocityRaptor" class="hu.fornax.bmp.common.helper.VelocityRaptorImpl" />
</beans>


3. web.xml still existed in our project, so I extended it with
    <context-param>
            <param-name>portalContextConfigLocation</param-name>
            <param-value>/WEB-INF/classes/META-INF/hook-spring.xml</param-value>
    </context-param>


4. Usage test from Control Panel → Server Administration → Script. with a groovy script (modified with our portlet name)
myUtil = com.liferay.portal.kernel.bean.PortletBeanLocatorUtil.locate("bmp-liferay-portlet", "hu.fornax.bmp.common.helper.VelocityRaptor")

println(myUtil.testOperation("Joe Bloggs"))


5. I made a test web content structure and template in order to try, am I able to call it from velocity too. It was success!!! emoticon

I made some additional test methods in order to check, am I able to pass String[], or DLFileEntry or HashMap<String,String> or custom class type result to velocity and am I able to use/display them. All went fine.emoticon

With this technic a big door opened, and we can gather/display many information much more easier.

Thank you for the help and for the tip.emoticon