Wednesday 15 August 2012

php - Get image custom post image size - Wordpress -



php - Get image custom post image size - Wordpress -

i have next code:

<div class="content project clearfix"> <?php if( have_rows('galeria') ): while ( have_rows('galeria') ) : the_row(); ?> <img class="project-item" src="<?php the_sub_field('imagen'); ?>"> <?php endwhile; endif; ?> </div>

the thing every img ("imagen") has different sizes, , output is:

<img class="project-item" src="http://test.local/wp-content/uploads/2014/11/project-5.jpg">

as can see no width or height set. how can width , height of image on attr?

you can utilize getimagesize() function - think it's gdlib function, installed php installation:

$fileinfo = getimagesize(the_sub_field('imagen'));

which produce array similar this:

array ( [0] => 800 [1] => 799 [2] => 2 [3] => width="800" height="799" [bits] => 8 [channels] => 3 [mime] => image/jpeg )

use like:

<?php $img = the_sub_field('imagen'); $fileinfo = getimagesize($img); ?> <img class="project-item" src="<?php echo $img; ?>" <?php echo $fileinfo[3]; ?> />

should produce:

<img class="project-item" src="http://test.local/wp-content/uploads/2014/11/project-5.jpg" width="800" height="799" />

php wordpress

No comments:

Post a Comment