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;
}
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'm trying to use the WideImage plugin and loading an image into it, resizing it, and then outputting it in the following form:
<img class="image" src="image.jpg" />
I have this so far:
<?php
$image = WideImage::load('image.jpg');
$resizedImage = $image->resize(150, 150, 'outside')->crop('center', 'middle', 150, 150);
?>
How can I output the resized image so that it's in the form above? Help!
Resize
You can resize images by passing a few parameters to the resize() method. The first two are the new dimensions of the image, and can be smart coordinate values. If one dimension isn’t specified (or null is given), it’s calculated from the ratio of the other dimension.
Resize an image into a 400×300 box. By default, resizing keeps the original image’s aspect ratio and the resulting image fits the given dimensions from the inside.
$resized = $image->resize(400, 300);
This is equal to passing ‘inside’ as $fit value.
$resized = $image->resize(400, 300, 'inside');
Resize an image to fit a 400×300 box from the outside by passing ‘outside’ to $fit parameter. This means that the image will be at least as big as 400×300, and aspect ratio will be kept.
$resized = $image->resize(400, 300, 'outside');
Resize an image to exactly fit a 400×300 box by passing ‘fill’ as the value of $fit parameter. The image will be stretched as necessary, aspect ratio may not be kept.
$resized = $image->resize(400, 300, 'fill');
The fourth parameter ($scale) determines when to scale an image. Possible values include any (default), down and up:
down – resize if image is larger than the new dimensions
up – resize if image is smaller than the new dimensions
any – resize regardless of the image size
There are two aliases for the resize method: resizeUp and resizeDown. These two are equal to calling resize() with $scale = ‘up’ and $scale = ‘down’ respectively.
$resized = $image->resize(350, 500, 'inside', 'down');
// is equal to
$resized = $image->resizeDown(350, 500, 'inside');
in your HTML add
<img src= "<?= $resized ?>"> – Moises Zaragoza just now edit
The problem is that the browser attempts to read the image as if it was text, more specifically an html document.
To solve it I would create a new php script for the image processing, let's say image.php, with a function taking the image path and also the parameters of the modification.
In the end just show the image as you did before, just be sure to change the header of the content to something like:
header('Content-type: image/jpg');
or
header('Content-type: image/png');
With that the browser will know how to interpret the data received.
First you have to save the resized image as a file.
In you php put:
<?php
$image = WideImage::load('image.jpg');
$resizedImage = $image->resize(150, 150, 'outside')->crop('center', 'middle', 150, 150);
$resizedImage ->saveToFile('resized_image.jpg');
?>
Then make your image reference the new file:
<img class="image" src="resized_image.jpg" />
I scored my website on gtmetrix.com.
It recommends adding image dimensions to all my images. The thing is, I need some way of adding these automatically, so if the image changes, the dimentions are updated.
What I have
<img src="bla.png">
What I'd like to have
<img src="bla.png" width="{width of image}" height="{height of img}">
Is there a php way or a jquery way to do this?
For a wordpress site.
PHP:
<?php
list($width, $height) = getimagesize("bla.png");
echo "<img src='bla.png' width='$width' height='$height'>";
?>
You can use image onload event to calculate the dimensions of image. Pure javascript approach, something like ;
var image = new Image();
image.onload = function()
{
//after load complete you will get the image dimensions here from
//image.width and image.height
}
image.src = 'image url';
In php it's getimagesize($file) if you are outputting images dynamically.
You should do it with PHP not with javascript. If you add it with jQuery / javascript, the width and height would be added after loading. The main reason of specifying width and height is to let the engine render the page faster. It would not be of any help if it as added after loading.
You can use the index 3 of the returned array from getimagesize() directly in the image tag. It contains a string with height="yyy" width="xxx" an can be used directly in an IMG tag.
<?php
list($width, $height, $type, $attr) = getimagesize("file.png");
echo "<img src='file.png' $attr />";
?>
You can visit http://php.net/manual/en/function.getimagesize.php for more details on this function.
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;
<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.