<img src="......" width="....." height="...."/>
If I have a function generating the above code, how can I get the width and height attributes with php?
You can use getimagesize()
<?php
list($width, $height, $type, $attr) = getimagesize("http://example.com/image.gif");
?>
<img src="..." width="<?= $width ?> height="<?= $height ?>" />
Do you mean something like this ?
So you would need to use
preg_match_all('/(alt|title|src)=("[^"]*")/i',$img_tag, $img[$img_tag]);
And then foreach them to array.
Related
I need to get the height and width of an image in order to set stretch it according to the size of the div. I could have used max-width:100% and max-height:100% but there are some images that are smaller than the div so I decided I would just manually assign the width and height.
I prefer to get these dimensions in php imagesy() and imagesx().
<?php $h=imagesy($list['Image1']);
$ w = imagesy($list['Image1'])?>
<img src="<?php echo $list['Image1']; ?>"
<?php if($h>$w) echo"style='height:100%;'";
else echo"style='width:100%;'"; ?> />
PHP functions like getimagesize() will return the dimensions of an image. But you are first going to have the load the image into the function. In other words you're going to have to fetch the image first.
If you have allow_url_fopen enabled then you should be able to invoke it with an absolute URL:
$size = getimagesize("http://www.example.com/gifs/logo.gif");
Okay, I was able to read just now that getimagesize returns an array with the width at [0] and height at [1]. So I was still able to compare them directly, hence, dynamically resizing them to fit the div containing the image. ;)
<?php $h=(getimagesize($list['Image1']));?>
<img src="<?php echo $list['Image1']; ?>"
style="<?php
if($h[0]>$h[1])
{
echo'width:100%;margin:0 auto;';
}
else
{
echo'height:100%;vertical-alignment:auto;';
} ?>"
alt="<?php echo $list['Image1']; ?>" />
I am using the jQuery Flexslider plugin in order to display a little over 100 images. I have gotten them all to display using a foreach loop in php as seen below:
<?php foreach (glob('images/glob/*') as $filename): ?>
<li> <img src="<?= $filename ?>"/> </li>
<?php endforeach; ?>
My images all have the same height in 600px, but their widths are varying. Some are portrait images while others are landscape. I'm wondering if there is a way in php that I can add classes to style the two accordingly based on their widths.
For example:
if
( $img width > $img height ).addClass (landscape)
else
( $img height > $img width ).addClass (portrait)
Obviously that statement above won't work but can someone please show me how to add that into my foreach statement so I can add classes to the varying images?
Use getimagesize: http://www.php.net/manual/en/function.getimagesize.php
foreach (glob('images/*') as $filename) {
$size = getimagesize($filename);
if ( $size[0] > $size[1] ) {
$class="landscape";
}
else {
$class="portrait";
}
print '<li> <img src="'. $filename.'" '.$size[3].' class="'.$class.'" /> </li>';
}
$size[3] being there to add proper width= and height= attributes to img element.
I'm looking to reduce image sizes using PHP on page load. I'm not really to sure how to achieve this, i've gotten as far as to obtain the dimensions but how would I reduce the sizes of these using only PHP?Here is my current code:
<?php
$stmt = $db->query('SELECT * FROM img_gallery ORDER BY list');
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) :
$image = $row['url'];
list($width, $height) = getimagesize($image); //grab the image dimensions here
?>
<img src="" width="<?=$width ?>px" height="<?=$height ?>px" /> //image here
So the dimensions are reduced by a few hundred pixels each?
try php imagejpeg
imagejpeg (resource $image [,string $filename [,int $quality ] ] );
For more info click here,
You need to create a new canvas with the desired size and copy the resampled canvas there:
$newcanvas = imagecreatetruecolor($width,$height);
$oldcanvas = imagecreatefromjpeg($imagePath);
imagecopyresampled($newcanvas,$oldcanvas,0,0,0,0,$width, $height,$originalwidth,$originalheight);
I was wondering if it is possible to do some kind of function which will get the height of a constrained width image? I can get the original height by using:
list($width, $height, $type, $attr) = getimagesize($path."/images/".$image->image);
But my images on the page are constrained to be 580px wide:
<img src="/images/<?php echo $image->image; ?>" width="580" height="????" border="0" />
I was wondering if it were at all possible to get the new height of the constrained width image?
Its all about ratios. You already have the ratio of the width/height for the original. Now you just need to get it in terms of your 580px width. width/height is to 580/X
function getHeight($width,$height){ //originals
return (580*$height)/$width;
}
I am writing down a function and it takes an argument which is the physical path of an image on server. I was wondering if it is possible in any way to obtain its original size in pixels.
In one variable I would like to store its width and in other variable its height. The challenge that made me ask this is because I have to obtain it on server-side thus any client-side solution would not help.
Try to use something like this:
<?php
$size = getimagesize ("img.jpg");
echo "<img src=\"img.jpg\" {$size[3]}>";
?>
An example will help you to get image height and width parameter in their separate variables like $width and $height.
list($width, $height, $type, $attr) = getimagesize('image/testimage.jpg', $info);
echo $width;
echo $height;