We should take some care whenever we use the sizes of an image through the height and width attributes. The main one is the ratio between the width and the height of an image.
If these attributes are not known, first we should get them through the function PHP getimagesize.
list($width, $height, $type, $attr) = getimagesize("../images/Imagem005.jpg");
Returns an array with 4 elements. Index 0 contains the image width in pixels. Index 1 contains the height. Index 2 is an indication of the image type: 1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF(intel byte order), 8 = TIFF(Motorola byte order), 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC, 14 = IFF, 15 = WBMP, 16 = XBM. These values correspond to the IMAGETYPE constants that were added in PHP 4.3. Index 3 is a string with the correct height="yyy" width="xxx" that can be used directly in an IMG tag. getimagesize
As we only care about the height and width attributes we will use
list($width, $height) = getimagesize("../images/Imagem005.jpg");
//nova largura
$newWidth = 200;
//nova altura
$newHeight = ($newWidth*$height)/$width;
echo "<img src=\"../images/Imagem005.jpg\" width=\"".$newWidth."\" height=\"".$newHeight."\"/>";
In your case the img tag would look like this:
echo '<img src="data:image/jpeg;base64,'.base64_encode( $books->Image ).'" height='.$newHeight.' width='.$newWidth.'/>';
On the other hand, leaving PHP aside, just indicate the width in the img tag that browsers automatically calculate the height.
It seems too much to me. I would only use CSS, it’s more performant, and the result is the same. With auto height the browser calculates for us the height, hence you need less processing, and as it is on the client side is "cachable".
– foxtrot