resize image while uploading in PHP - php

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;
}

Related

how to resize an image while maintaining width and height ratio

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

Scale down an image to a fixed width and Height with same aspect resolution

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;

Calculating equivalent height if width is resized of image in PHP

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;
}
?>

Get a image height according to width using php

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)

find proportional resize dim which should be less than or equal to the needed thumb dim

I php need to resize an image so that both width and height after resize should be less than or equal to the expected resized values.
consider the source image is 680x520
The needed thumbmail size should be <= 300x200
I want to find the resized proportional value which should be less than or equal to 300x200.
it can be like 299x199 or 300x200 but not greater than 300 in width and 200 in height.
So both should be less than or equal to the needed size.
What could be the formula?
//height - original image height;
//width - original image width;
if ($height > $width) {
$thumb_height = 200;
$thumb_width = round(($width * $thumb_height) / $height);
} else {
$thumb_width = 300;
$thumb_height = round(($height * $thumb_width) / $width);
}
EDIT:
//height - original image height;
//width - original image width;
$thumb_height = 0;
$thumb_width = 0;
if ($height > $width) {
$thumb_height = 200;
} else {
if ($height > ($width * 200)/300)
$thumb_height = 200;
else
$thumb_width = 300;
}
if ($thumb_height > 0) {
$thumb_width = round(($width * $thumb_height) / $height);
} else {
$thumb_height = round(($height * $thumb_width) / $width);
}
I didn't find any example for the reversed that you gave me. If you have one, please tell me and I will check to see if the above script needs any modifications.
Maybe offtopic, but for image manipulation realy easy to use:
Wide Image

Categories