Add size attributes variable for all images

  • Idea
  • Updated 4 years ago
I ran into an issue where I needed to set the size of some images inline. To get something like this:



<img src="imgfile.jpg" width="300" height="200" />


All you need is add some code into app/asset-type/image.inc.php. These lines can go at the end of function set_extended_data($file_path), right after $large_relative_path statement.



# set dimensions for image
$img_path = preg_replace('/
# set dimensions for image
$img_path = preg_replace('/\.\.\//', '', $this->link_path);
$img_size = getimagesize($img_path);
$this->data['@sizeattrs'] = $img_size[3];
#46;
# set dimensions for image
$img_path = preg_replace('/\.\.\//', '', $this->link_path);
$img_size = getimagesize($img_path);
$this->data['@sizeattrs'] = $img_size[3];
#46;
# set dimensions for image
$img_path = preg_replace('/\.\.\//', '', $this->link_path);
$img_size = getimagesize($img_path);
$this->data['@sizeattrs'] = $img_size[3];
#47;/', '', $this->link_path);
$img_size = getimagesize($img_path);
$this->data['@sizeattrs'] = $img_size[3];


Now you can use @sizeattrs to output the size attributes in your image tag like so:



<img src="@url" @sizeattrs />
Photo of David DeSandro

David DeSandro

  • 8 Posts
  • 1 Like
  • looking forward to seeing others custom variables

Posted 5 years ago

  • 6
Photo of Max Fenton

Max Fenton

  • 33 Posts
  • 6 Likes
Hella helpful. Thanks.
Photo of Anthony Kolber

Anthony Kolber, Official Rep

  • 811 Posts
  • 157 Likes
Nice work David.
Photo of noxxiousN

noxxious

  • 2 Posts
  • 0 Likes
I cannot get this to work with "@thumb". I have the following code in my project-list page:

if $children do
foreach $children do
...a href="@url" title="@title"...
...img src="@thumb" @sizeattrs...
.../a...
endforeach
endif

I have added David's code but it looks like the variable is not recognized. It works with regular "@url" images on another page though.

Can you help me out please? Thanks,

nox
Photo of Anthony Kolber

Anthony Kolber, Official Rep

  • 811 Posts
  • 157 Likes
@thumb is a variable which exists for each page, but does not reference an actual image object.

I would try using an asset collection to achieve this, as any images within an asset collection will have access to your "@sizeattrs" variable.
See: http://staceyapp.com/documentation/ed...
Photo of noxxiousN

noxxious

  • 2 Posts
  • 0 Likes
Thanks Anthony, that works fine!
Photo of David DeSandro

David DeSandro

  • 8 Posts
  • 1 Like
With the 2.1.0 release, this idea is now duplicitous. Users may use the @width and @height variables.
Photo of lia.lowenthalL

lia.lowenthal

  • 1 Post
  • 0 Likes
I'm having some trouble with this (I don't know php at all). I added the code as instructed, but I'm failing to see where I can denote how I want to resize my images... help? :(

thanks in advance!