Jón Árnason
Jón Árnason

Reputation: 77

Adding img attributes with php

This works:

<?php 
     if($user->image > 0)
     {
          $imgSrc = "/uploads/".$user->image;
          echo "Your Current Image:" . "<br>";
          echo "<img src= $imgSrc >";
     }

I need to add height and width attributes to the $imgSrc but have been unsuccessful so far.

The image is showing at its original dimensions and I want to change this.

Upvotes: 0

Views: 695

Answers (1)

user1850421
user1850421

Reputation:

Ok so it transpires that you want to change the size of the image from its original

echo "<img src='$imgSrc' style='width: 100px; height:100px;' />";

and maybe you want to maintain proportions

echo "<img src='$imgSrc' style='width: 100px; height:auto;' />";

If you were looking to add the exact size from the image using PHP you would do this

if($user->image > 0)
 {
      $imgSrc = "/uploads/".$user->image;
      list($width, $height) = getimagesize($imgSrc);
      $style = 'width: ' . $width . 'px; height: ' . $height . 'px;';
      echo 'Your Current Image: <br />';
      echo '<img src="' . $imgSrc . '" style="' . $style . '" alt="User image" />';
 }

Upvotes: 2

Related Questions