I want to get a image size according to the width and also I'm resizing the width if more then 500px
Here is my code so far
list($width, $height) = getimagesize("MyImage.jpg");
if ($width<300){
$NewWidth = $width;
}else{
$NewWidth = '500';
}
Just like to know how to get new height according to the width. Really appreciate your help or any advice you can give.
You need to get current image scale and then use that to calculate the new height from the new width. Divide the width by the height to get the scale. Then divide the new width by the scale to get the new (proportional) height.
list($width, $height) = getimagesize("MyImage.jpg");
// Get the image scale.
$scale = $width / $height;
// Existing width modification.
if ($width<300){
$NewWidth = $width;
}else{
$NewWidth = '500';
}
// Get new height from new width.
$NewHeight = $NewWidth / $scale;
Try this formula to maintain 4:3 aspect ratio.
new height = ((width X 3) / 4)
Related
I am using this function to resize an uploaded image:
function imageResize($imageResourceId,$width,$height) {
$targetWidth =750;
$targetHeight =300;
$targetLayer = imagecreatetruecolor($targetWidth,$targetHeight);
imagecopyresampled($targetLayer,$imageResourceId,0,0,0,0,$targetWidth,$targetHeight, $width,$height);
return $targetLayer;
}
But this makes the image 750 width and 300 height. The height should be flexible and depends on the width.
So i am looking for something like this:
$targetWidth = 750;
$targetHeight = auto;
i really do not know how i should do this...
You can easily calculate the ratio of the original image and apply it to the resized one:
$targetHeight = ($height / $width) * $targetWidth
I am trying to scale down an uploaded image to a fixed with and height for example: width=200px and height 200px. I tried to scale down the width of the image with a fixed width and calculate the new height by its new width. What now i am trying to achieve is to scale down both width and height to a fixed size width=200px & height =200px
My HTML:
<form action="do_upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="upload_image">
<br/>
<br/>
<input type="submit" value="submit">
</form>
do_upload.php:
<?php
move_uploaded_file($_FILES["upload_image"]["tmp_name"], "uploads/" . $_FILES["upload_image"]["name"]);
$image_path = "uploads/" . $_FILES["upload_image"]["name"];
$src = imagecreatefromjpeg($image_path);
list($width, $height) = getimagesize($image_path);
$newwidth = 200;
$newheight = ($height / $width) * $newwidth;
$tmp = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($tmp, $src, 0, 0,0,0,$newwidth,$newheight, $width, $height);
imagejpeg($tmp, "uploads/small.jpeg", 100);
imagedestroy($src);
imagedestroy($tmp);
?>
This is working fine for me and scaling down the image. Kindly guide me how can i scale down all image (jpeg,png,gif etc) to a fixed width and height. Just guide me in the direction i will do it. If anyone can explain with an example or edit my code for me it will be great. Thanks in advance
You can see comment above to answer question about what to do with dimension that is less than 200 and aspect ration. That being said, I can go ahead and answer the question on how to scale on either dimension. Basically the problem is pretty simple.
First you determine if existing image has width or height which is greater dimension. Based on that, you determine the scaling factor for the image and then apply that scaling factor to both dimensions.
In code that might look like:
$src = imagecreatefromjpeg($image_path);
list($width, $height) = getimagesize($image_path);
$max_dimension = 200;
if($width >= $height) {
// scale on width
$scaling_factor = $width / $max_dimension;
} else {
// scale on height
$scaling_factor = $height / $max_dimension;
}
$new_width = $width / $scaling_factor;
$new_height = $height / $scaling_factor;
A more generalized solution which would let you specify any rectangle (i.e. different max height and width) within which to scale might look like this.
$max_width = 200;
$max_height = 150;
// determine which dimension must be scaled to fit target size
$width_scaling_factor = $width / $max_width;
$height_scaling_factor = $height / $max_height;
if($width_scaling_factor >= $height_scaling_factor) {
$scaling_factor = $width_scaling_factor;
} else {
$scaling_factor = $height_scaling factor;
}
$new_width = $width / $scaling_factor;
$new_height = $height / $scaling_factor;
I am new to stackoverflow, but want to learn with the help of it.
Right now I am learning Image resizing in PHP, and stuck in a situation where Image size is 2445x1783 (width x height)
I want to make a function in PHP which can resize the image, from ratio-wise, as if the image width exceeds 1600 pixels, then resize it to 1600px width and corresponding RATIOED height.
Is there any formula for calculating the height in my case?
Thanks
Just calculate the ratio and multiply the original height by it:
<?php
$max_width = 1600;
$new_width = $original_width; $new_height = $original_height;
if ($original_width > $max_width) {
$ratio = $max_width / $original_width;
$new_width = $max_width;
$new_height = $original_height * $ratio;
}
?>
I have a file upload form where I upload only jpg, png and gif images. I resize the image if its width is more then 225 and height automatically fix with that width.
if($width > 225) {
$newwidth = 225;
}
else {
$newwidth = $width;
}
$newheight = ($height/$width)*$newwidth;
The above code fixes the width for me in case if image > 225. Now the problem is that the new height is according to the image width. I don't want the height to be more then 150. How can I fix it with out stretching the image?
Try adjusting the width in case of $newheight being greater than 150, calculating the proportion. Add this at the bottom:
if ($newheight > 150) {
$proportion = $newwidth/$newheight;
$newheight = 150;
$newwidth = 150*$proportion;
}
I have 2 variables available about a photo i'm importing, width and height.(I could use imagesx etc, but i'm importing in bulk and it takes way too long)
eg..
$w = 720;
$h = 540;
How can I workout the correct height for the photo if I say the max width is 180px, I know the correct height should be 135px (on a standard landscape photo). But the starting width and height i'm given will change depending on the photo.
Keeping the image in proportion what calculation do I need to do to workout the reduced width and height?
Final result will be in pixels so can not have any decimal places.
(I do not actually need to re-size the photo, just calculate the size.)
Thanks in advance :)
720x = 540 * 180
It is a simple cross-multiplying rule. Then round the numbers to get rid of the decimal portion.
Here's one I use:
function scaleDimensions($orig_width, $orig_height, $max_width, $max_height) {
if ($orig_width < $max_width && $orig_height < $max_height) {
return array($orig_width, $orig_height);
}
$ratiow = $max_width / $orig_width;
$ratioh = $max_height / $orig_height;
$ratio = min($ratiow, $ratioh);
$width = intval($ratio * $orig_width);
$height = intval($ratio * $orig_height);
return array($width, $height);
}
Usage:
list($width, $height) = scaleDimensions($w, $h, 180, 135);
And this is the javascript version of #fire's answer
scaleDimensions : function(orig_width, orig_height, max_width, max_height) {
if (orig_width < max_width && orig_height < max_height) {
return [orig_width, orig_height];
}
var ratiow = max_width / orig_width;
var ratioh = max_height / orig_height;
var ratio = Math.min(ratiow, ratioh);
var width = (ratio * orig_width).toFixed(0);
var height = (ratio * orig_height).toFixed(0);
return [width, height];
}