How can I get the width or height of an image field image on Octobercms
up vote
0
down vote
favorite
I try this method to get image dimensions:
{% set image_width = fields.sec_port_image.width %}
{% set image_height = fields.sec_port_image.height %}
<a href="{{ fields.sec_port_image|media }}" width="{{ image_width }}" height="{{ image_height }}">
but without success. I Also tried:
{% set image_width = fields.sec_port_image.width|media %}
and
{% set image_width = fields.sec_port_image|media.width %}
but also without success.
Is there a way on Octobercms to get image dimensions using twig?
image twig octobercms twig-filter
add a comment |
up vote
0
down vote
favorite
I try this method to get image dimensions:
{% set image_width = fields.sec_port_image.width %}
{% set image_height = fields.sec_port_image.height %}
<a href="{{ fields.sec_port_image|media }}" width="{{ image_width }}" height="{{ image_height }}">
but without success. I Also tried:
{% set image_width = fields.sec_port_image.width|media %}
and
{% set image_width = fields.sec_port_image|media.width %}
but also without success.
Is there a way on Octobercms to get image dimensions using twig?
image twig octobercms twig-filter
What kind of object issec_port_image
? Does it have the public propertieswidth
andheight
or the public gettergetWidth()
orgetHeight()
?
– DarkBee
Nov 8 at 14:13
sec_port_image is a Octobercms mediafinder widget field (octobercms.com/docs/api/backend/formwidgets/mediafinder).
– Charis
Nov 8 at 15:46
As seen in the source the properties are calledimageWidth
andimageHeight
– DarkBee
Nov 8 at 15:49
Yes, i also try {% set image_width = fields.sec_port_image.imageWidth %} without success. imageWidth points to image preview on Octobercms backend and not to original image uploaded.
– Charis
Nov 8 at 15:59
Ah you should mention you are looking for the original measurements. I'd suggest adding aTwig_Filter
or something that does this for you then as you can't retrieve the originals one straight from that object
– DarkBee
Nov 8 at 16:03
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I try this method to get image dimensions:
{% set image_width = fields.sec_port_image.width %}
{% set image_height = fields.sec_port_image.height %}
<a href="{{ fields.sec_port_image|media }}" width="{{ image_width }}" height="{{ image_height }}">
but without success. I Also tried:
{% set image_width = fields.sec_port_image.width|media %}
and
{% set image_width = fields.sec_port_image|media.width %}
but also without success.
Is there a way on Octobercms to get image dimensions using twig?
image twig octobercms twig-filter
I try this method to get image dimensions:
{% set image_width = fields.sec_port_image.width %}
{% set image_height = fields.sec_port_image.height %}
<a href="{{ fields.sec_port_image|media }}" width="{{ image_width }}" height="{{ image_height }}">
but without success. I Also tried:
{% set image_width = fields.sec_port_image.width|media %}
and
{% set image_width = fields.sec_port_image|media.width %}
but also without success.
Is there a way on Octobercms to get image dimensions using twig?
image twig octobercms twig-filter
image twig octobercms twig-filter
asked Nov 8 at 14:07
Charis
4228
4228
What kind of object issec_port_image
? Does it have the public propertieswidth
andheight
or the public gettergetWidth()
orgetHeight()
?
– DarkBee
Nov 8 at 14:13
sec_port_image is a Octobercms mediafinder widget field (octobercms.com/docs/api/backend/formwidgets/mediafinder).
– Charis
Nov 8 at 15:46
As seen in the source the properties are calledimageWidth
andimageHeight
– DarkBee
Nov 8 at 15:49
Yes, i also try {% set image_width = fields.sec_port_image.imageWidth %} without success. imageWidth points to image preview on Octobercms backend and not to original image uploaded.
– Charis
Nov 8 at 15:59
Ah you should mention you are looking for the original measurements. I'd suggest adding aTwig_Filter
or something that does this for you then as you can't retrieve the originals one straight from that object
– DarkBee
Nov 8 at 16:03
add a comment |
What kind of object issec_port_image
? Does it have the public propertieswidth
andheight
or the public gettergetWidth()
orgetHeight()
?
– DarkBee
Nov 8 at 14:13
sec_port_image is a Octobercms mediafinder widget field (octobercms.com/docs/api/backend/formwidgets/mediafinder).
– Charis
Nov 8 at 15:46
As seen in the source the properties are calledimageWidth
andimageHeight
– DarkBee
Nov 8 at 15:49
Yes, i also try {% set image_width = fields.sec_port_image.imageWidth %} without success. imageWidth points to image preview on Octobercms backend and not to original image uploaded.
– Charis
Nov 8 at 15:59
Ah you should mention you are looking for the original measurements. I'd suggest adding aTwig_Filter
or something that does this for you then as you can't retrieve the originals one straight from that object
– DarkBee
Nov 8 at 16:03
What kind of object is
sec_port_image
? Does it have the public properties width
and height
or the public getter getWidth()
or getHeight()
?– DarkBee
Nov 8 at 14:13
What kind of object is
sec_port_image
? Does it have the public properties width
and height
or the public getter getWidth()
or getHeight()
?– DarkBee
Nov 8 at 14:13
sec_port_image is a Octobercms mediafinder widget field (octobercms.com/docs/api/backend/formwidgets/mediafinder).
– Charis
Nov 8 at 15:46
sec_port_image is a Octobercms mediafinder widget field (octobercms.com/docs/api/backend/formwidgets/mediafinder).
– Charis
Nov 8 at 15:46
As seen in the source the properties are called
imageWidth
and imageHeight
– DarkBee
Nov 8 at 15:49
As seen in the source the properties are called
imageWidth
and imageHeight
– DarkBee
Nov 8 at 15:49
Yes, i also try {% set image_width = fields.sec_port_image.imageWidth %} without success. imageWidth points to image preview on Octobercms backend and not to original image uploaded.
– Charis
Nov 8 at 15:59
Yes, i also try {% set image_width = fields.sec_port_image.imageWidth %} without success. imageWidth points to image preview on Octobercms backend and not to original image uploaded.
– Charis
Nov 8 at 15:59
Ah you should mention you are looking for the original measurements. I'd suggest adding a
Twig_Filter
or something that does this for you then as you can't retrieve the originals one straight from that object– DarkBee
Nov 8 at 16:03
Ah you should mention you are looking for the original measurements. I'd suggest adding a
Twig_Filter
or something that does this for you then as you can't retrieve the originals one straight from that object– DarkBee
Nov 8 at 16:03
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Extend twig
in order to get the original size of your image. Something like this should work (not tested but you'll get the idea)
First create a registration class, see here for a reference.
<?php
class Registration {
public function registerMarkupTags()
{
return [
'filters' => [
// A global function, i.e str_plural()
'image_width' => [$this, 'getImageWidth'],
'image_height' => [$this, 'getImageHeight'],
],
];
}
private $images = ;
public function getImageWidth($url) {
return $this->getImageSize($url) ? $this->getImageSize($url)['width'] : null;
}
public function getImageHeight($url) {
return $this->getImageSize($url) ? $this->getImageSize($url)['height'] : null;
}
private function getImageSize($url) {
if (!isset($this->images[$url])) {
$data = @getimagesize($url);
if ($data) {
$this->images[$url] = [
'width' => $data[0],
'height' => $data[1],
];
}else{
$this->images[$url] = false;
}
}
}
return $this->images[$url];
}
If done correctly you then could use the new filters inside your template like this
{% set source = fields.sec_port_image|media %}
<a href="{{ source }}" width="{{ source|image_width }}" height="{{ source|image_height }}">
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Extend twig
in order to get the original size of your image. Something like this should work (not tested but you'll get the idea)
First create a registration class, see here for a reference.
<?php
class Registration {
public function registerMarkupTags()
{
return [
'filters' => [
// A global function, i.e str_plural()
'image_width' => [$this, 'getImageWidth'],
'image_height' => [$this, 'getImageHeight'],
],
];
}
private $images = ;
public function getImageWidth($url) {
return $this->getImageSize($url) ? $this->getImageSize($url)['width'] : null;
}
public function getImageHeight($url) {
return $this->getImageSize($url) ? $this->getImageSize($url)['height'] : null;
}
private function getImageSize($url) {
if (!isset($this->images[$url])) {
$data = @getimagesize($url);
if ($data) {
$this->images[$url] = [
'width' => $data[0],
'height' => $data[1],
];
}else{
$this->images[$url] = false;
}
}
}
return $this->images[$url];
}
If done correctly you then could use the new filters inside your template like this
{% set source = fields.sec_port_image|media %}
<a href="{{ source }}" width="{{ source|image_width }}" height="{{ source|image_height }}">
add a comment |
up vote
0
down vote
Extend twig
in order to get the original size of your image. Something like this should work (not tested but you'll get the idea)
First create a registration class, see here for a reference.
<?php
class Registration {
public function registerMarkupTags()
{
return [
'filters' => [
// A global function, i.e str_plural()
'image_width' => [$this, 'getImageWidth'],
'image_height' => [$this, 'getImageHeight'],
],
];
}
private $images = ;
public function getImageWidth($url) {
return $this->getImageSize($url) ? $this->getImageSize($url)['width'] : null;
}
public function getImageHeight($url) {
return $this->getImageSize($url) ? $this->getImageSize($url)['height'] : null;
}
private function getImageSize($url) {
if (!isset($this->images[$url])) {
$data = @getimagesize($url);
if ($data) {
$this->images[$url] = [
'width' => $data[0],
'height' => $data[1],
];
}else{
$this->images[$url] = false;
}
}
}
return $this->images[$url];
}
If done correctly you then could use the new filters inside your template like this
{% set source = fields.sec_port_image|media %}
<a href="{{ source }}" width="{{ source|image_width }}" height="{{ source|image_height }}">
add a comment |
up vote
0
down vote
up vote
0
down vote
Extend twig
in order to get the original size of your image. Something like this should work (not tested but you'll get the idea)
First create a registration class, see here for a reference.
<?php
class Registration {
public function registerMarkupTags()
{
return [
'filters' => [
// A global function, i.e str_plural()
'image_width' => [$this, 'getImageWidth'],
'image_height' => [$this, 'getImageHeight'],
],
];
}
private $images = ;
public function getImageWidth($url) {
return $this->getImageSize($url) ? $this->getImageSize($url)['width'] : null;
}
public function getImageHeight($url) {
return $this->getImageSize($url) ? $this->getImageSize($url)['height'] : null;
}
private function getImageSize($url) {
if (!isset($this->images[$url])) {
$data = @getimagesize($url);
if ($data) {
$this->images[$url] = [
'width' => $data[0],
'height' => $data[1],
];
}else{
$this->images[$url] = false;
}
}
}
return $this->images[$url];
}
If done correctly you then could use the new filters inside your template like this
{% set source = fields.sec_port_image|media %}
<a href="{{ source }}" width="{{ source|image_width }}" height="{{ source|image_height }}">
Extend twig
in order to get the original size of your image. Something like this should work (not tested but you'll get the idea)
First create a registration class, see here for a reference.
<?php
class Registration {
public function registerMarkupTags()
{
return [
'filters' => [
// A global function, i.e str_plural()
'image_width' => [$this, 'getImageWidth'],
'image_height' => [$this, 'getImageHeight'],
],
];
}
private $images = ;
public function getImageWidth($url) {
return $this->getImageSize($url) ? $this->getImageSize($url)['width'] : null;
}
public function getImageHeight($url) {
return $this->getImageSize($url) ? $this->getImageSize($url)['height'] : null;
}
private function getImageSize($url) {
if (!isset($this->images[$url])) {
$data = @getimagesize($url);
if ($data) {
$this->images[$url] = [
'width' => $data[0],
'height' => $data[1],
];
}else{
$this->images[$url] = false;
}
}
}
return $this->images[$url];
}
If done correctly you then could use the new filters inside your template like this
{% set source = fields.sec_port_image|media %}
<a href="{{ source }}" width="{{ source|image_width }}" height="{{ source|image_height }}">
answered Nov 9 at 7:00
DarkBee
8,51942741
8,51942741
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53209420%2fhow-can-i-get-the-width-or-height-of-an-image-field-image-on-octobercms%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What kind of object is
sec_port_image
? Does it have the public propertieswidth
andheight
or the public gettergetWidth()
orgetHeight()
?– DarkBee
Nov 8 at 14:13
sec_port_image is a Octobercms mediafinder widget field (octobercms.com/docs/api/backend/formwidgets/mediafinder).
– Charis
Nov 8 at 15:46
As seen in the source the properties are called
imageWidth
andimageHeight
– DarkBee
Nov 8 at 15:49
Yes, i also try {% set image_width = fields.sec_port_image.imageWidth %} without success. imageWidth points to image preview on Octobercms backend and not to original image uploaded.
– Charis
Nov 8 at 15:59
Ah you should mention you are looking for the original measurements. I'd suggest adding a
Twig_Filter
or something that does this for you then as you can't retrieve the originals one straight from that object– DarkBee
Nov 8 at 16:03