I want to check if the height or the width of an image is bigger. Depending on that, a maximum value is defined. If it is a landscape image $limit = 1000; if it is a vertical format $limit = 600;
After this the image can be scaled correctly (maximum width 1000px or 600px depending on the format of the image). If the image is smaller than that, the image values are used. (Just downscaling).
Depending on Get dimensions to transform image to a maximum value I don't know how to realize these two different limit values:
$tmp = max($width, $height); # give me the bigger value
$limit = min($limit, $tmp); # if image dimension is lower, take that value
$factor = $tmp / $limit; # calculate factor to get the new dimensions
$new_width = $width / $factor;
$new_height = $height / $factor;
So this works great if there is only one limit-value (ie. 1000).
I just need the values, no real transforming of the image...
min(1, is only needed if you want only reduce big images and not resize smaller images:
$maxWidth = 1000;
$maxWidthIfVertical = 600;
$factor = $width > $height ? min(1, $maxWidth / $width) : min(1, $maxWidthIfVertical / $width);
$newWidth = $width * $factor;
$newHeight = $height * $factor;
Caution with this method, see the examples I added here: http://jsfiddle.net/Wornet/z3z6fcr3/4/ I think it's not realy what you want.
So I recommand this (limit 1000 in width, 800 in height):
http://jsfiddle.net/Wornet/z3z6fcr3/6/
Related
I would like manipulate/resize images in a similar way to Pinterest but I am not sure what is the best way to approach it. The goal is to allow a mix of both portrait and landscape images but put some restrictions on the maximum height and width.
The problem i can see is that if I resize to a width, a portrait image may become too thin, and the opposite it true for a landscape image.
Any ideas on how to achieve those sort of results with PHP?
You just need to understand which of the two edges of the image is longer, and compute the other dimension proportionally. If the maximum long-egde is 1024, then if one of the two edges is larger you will set that to 1024, and compute the other to fit the proportions. Then you will pass those two values to your image management functions.
Like here:
http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/
Or here:
http://www.9lessons.info/2009/03/upload-and-resize-image-with-php.html
try with this
$needheight = 1000;
$needwidth = 1000;
$arrtest = getimagesize($upload_image_physical_path);
$actualwidth = $arrtest[0];
$actualheight = $arrtest[1];
if($needwidth > $actualwidth || $needheight > $actualheight){
//uplaod code
}
cheers
Check for a max size and then resize based on a ratio. Here's a pseudo code example:
if($imageHeight > $maxHeight) {
$newHeight = $maxHeight;
$newWidth = $imageWidth * ($maxHeight / $imageHeight);
}
if($imageWidth > $maxWidth) {
$newWidth = $maxWidth;
$newHeight = $imageHeight * ($maxWidth / $imageWidth);
}
resize($image, $newWidth, $newHeight);
It first checks the height and if the height is greater, it scales it down. Then it checks the width. If the width is too big, it scales it down again. The end result, both height and width will be with in your bounds. It uses the ratio to do the scaling.
Note, this is pseudocodish. The actual resize function call will depend on your image manipulation library -- same goes for calls to obtain image size.
I am trying to find a way to calculate an image width/height ratio
to resize it and keep the proportions.
For example, I want to resize a 500x750 image and reduce its width to 350. What height should I use that is proportional to 350?
Use Javascript.
See this tutorial:
http://www.ajaxblender.com/howto-resize-image-proportionally-using-javascript.html
Using the function the other guy posted:
<?PHP
$imagePath = "images/your_image.png";
list($oldWidth, $height, $type, $attr) = getimagesize($image_path);
$percentChange = $newWidth / $oldWidth;
$newHeight = round( ( $percentChange *$height ) );
echo '<img src="'.$imagePath.'" height="'.$new_height.'" width="'.$newWidth.'">';
?>
I think this is the php function you're looking for: getimagesize
From the manual:
Returns an array with 7 elements.
Index 0 and 1 contains respectively the width and the height of the
image.
Here's a short example how to work with this for your problem:
// get the current size of your image
$data = getimagesize('link/your/image.jpg');
// your defined width
$new_width = 350;
// calculate the ratio
$ratio = $data[0] / $new_width;
// apply the ratio to get the new height of your image
$new_height = round($data[1] / $ratio);
...done!
use getImagesize and get the new height by dividing by the aspect ratio.
list($width, $height, $type, $attr) = getimagesize("image.jpg");
$aspect = $width / $height;
$newWidth = 350;
$newHeight = $newWidth / $aspect;
You tagged your question with the PHP tag, so assuming you want to use PHP:
To get an image's height or width from an image resource, use imagesx() and imagesy(). http://www.php.net/manual/en/function.imagesx.php
http://www.php.net/manual/en/function.imagesy.php
To get an image's height and width from an image file, use getimagesize(). Items 0 and 1 in the array returned by that function are the width and height of the image.
http://www.php.net/manual/en/function.getimagesize.php
If you have an image that is 500 pixels wide and 750 pixels high, and you have a container that is 350 pixels wide, you can calculate the ratio by dividing the desired width by the actual width: 350/500 which is 0.7. So to calculate the height, multiply it by that ratio (750 * 0.7 or 525).
I want to do the following :
I need to resize images to fit and fill enterly 100x100 pixel box
not all image are perfect squares, and thats my problem for this task
I want to determine wich side (width or height) is the smallest.
Bring that Side to 100px while scaling down; Center back
Crop the overflow on both side
I've tried the width/height :100% technic but that doesnt give the result i want, (it is streched and ugly). Also i've seen some method but in C# ... I need in PHP.
I would appreciate any advice, directions, input, or ready script...
Thanks
using PHP 5.3.0
You must be able to calculate the aspect ratio by knowing the image dimensions beforehand. For more info, see getimagesize()
$width = 268;
$height = 300;
$MAX_SIZE = 100;
if($width > $MAX_SIZE || $height > $MAX_SIZE) {
$aspect = $width / $height;
if($width > $height) {
$width = $MAX_SIZE;
$height = intval($MAX_SIZE / $aspect);
} else {
$height = $MAX_SIZE;
$width = intval($MAX_SIZE * $aspect);
}
}
Afterwards, the scaled down height will be available in $height and the scaled down width will be available in $width.
I'm working on an image upload script with PHP, I found one someone was offering and tried modifying it, however, i'm running into a few problems.
I want to do the following:
Detect the longest side of the image (ie. portrait or landscape)
And then resize the image, with the longest side being 800px AND keep proportions.
Here is the code I have so far.. For landscape images it works fine, but with portrait ones it distorts them like crazy.
PS. I'm making a larger image as well as a thumbnail.
list($width,$height)=getimagesize($uploadedfile);
if($width > $height){
$newwidth=800;
$newheight=($height/$width)*$newwidth;
$newwidth1=150;
$newheight1=($height/$width)*$newwidth1;
} else {
$newheight=800;
$newwidth=($height/$width)*$newheight;
$newheight1=150;
$newwidth1=($height/$width)*$newheight;
}
$tmp=imagecreatetruecolor($newwidth,$newheight);
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);
You're probably mistaking:
When $width > $height that means it's landscape. Setting maxwidth to 800 means (height/width)*800 = new height. On the other hand $height > $width means setting maxheight to 800 and thus having (width/height)*800 is new width.
Right now your using both the height/width ratio instead of the other way around. Example:
Image: 1600 (w) x 1200 (h)
Type: Landscape
New Width: 800
New Height: (1200 (h) / 1600(w) * 800 (nw) = 600
Image 1200 (w) x 1600 (h)
Type: Portrait
New Height: 800
New Width: (1200 (w) / 1600(h) * 800 (nh) = 600
Hope you get what I'm saying, you just switched them :) Also notice that you multiply with $newheight instead of $newheight1 for the portrait thumbnail
You can take a look in this function I use in my Image class:
public function ResizeProportional($MaxWidth, $MaxHeight)
{
$rate = $this->width / $this->height;
if ( $this->width / $MaxWidth > $this->height / $MaxHeight )
return $this->Resize($MaxWidth, $MaxWidth / $rate);
else
return $this->Resize($MaxHeight * $rate, $MaxHeight);
}
Basically it first calculates the image's proportions in $rate based on width/height. Then it checks if the width is going to get out of bounds when resized ( $this->width / $MaxWidth > $this->height / $MaxHeight ) and if it is - sets width to the desired maximum width and calculates the height accordingly.
$this->width / $MaxWidth is the percentage of the image's width based on the maximum one. So if $this->width / $MaxWidth is larger than $this->height / $MaxHeight the width should be set to maxwidth and the height should be calculated based on it. If the comparison is the other way around just set height to maxheight and calculate the new width.
You should switch height and width in the second part, note the ($width/$height) part:
} else {
$newheight=800;
$newwidth=($width/$height)*$newheight;
$newheight1=150;
$newwidth1=($width/$height)*$newheight;
}
I have an area to display an image that has a max size of 240x180
If I have an image of 400x423 how can I calculate a new width and height for that image that best fits into my 240x180 box? (In this case it would be 170x180)
there are probably some image libraries that do this well, but the math is pretty simple.
ratio = orig_x * 1.0 / orig_y;
x_oversized = (orig_x > MAX_X);
y_oversized = (orig_y > MAX_Y);
if (x_oversized OR y_oversized)
{
new_x = min(MAX_X, ratio * MAX_Y);
new_y = min(MAX_Y, MAX_X / ratio);
}
Like this?
$newheight = 180;
$newwidth = $width * $newheight / $height;