I know how to resize an image keeping its ratio based on these formulas :
Width Formula :
Orig H / Orig W * New W = New H
Height Forula :
Orig W / Orig H * New H = New W
But i have set a maximum width before the image is resized and a maximum height value too.
So how do i work it with two maximum values ??
Say the max height was 600 and the max height was 100 ??
The answer is probably staring me in the face..
This is what i have just now using just the height.
$image_size = new Image(DIR_IMAGE.'data/signatures/'.$file);
$w_size = $image_size->info['width'];
$h_size = $image_size->info['height'];
$w_new_size = round($w_size/$h_size*100);
$h_new_size = 100;
I guess what i'm trying to say is i don't want the image to go above the set width or set height but it has to keep its ratio.
Some show me the light ??
Let php.net show you the light:
http://www.php.net/manual/en/function.imagecopyresampled.php
Example #2 is doing what you ask for, isn't it?
Related
I am reading an 8.5 x 11" PDF and creating a jpg thumbnail.
I want to crop all but approx the bottom 10% of the image. (basically only want the footer in the final)
$pdf_file = $file;
$save_to = 'bottom.jpg';
$img = new imagick();
$img->setResolution(300,300);
$img->readImage("{$pdf_file}[0]");
$img->scaleImage(800,0);
$img->setImageFormat('jpg');
$img = $img->flattenImages();
$img->cropImage(0,0,0,350);
$img->writeImages($save_to, false);
echo '<img src="bottom.jpg">';
The output of the above code produces a jpg showing the footer, however the image is 800px W X 685px H with white space on top of the footer.
I just want the footer at 800px W X approx 200px H.
I'm not sure why you're passing in zero 3 times to the crop function. The parameters are meant to be:
width - The width of the crop
height - The height of the crop
x - The X coordinate of the cropped region's top left corner
y - The Y coordinate of the cropped region's top left corner
So this should do what you want:
$img->cropImage(
$image->getImageWidth(),
350,
0,
$image->getImageHeight() - 350
);
Basically I want to turn this:
Into this:
Tried diff = actual_row_width - max_row_width / number_of_items;, then substract diff from each rectangle but it doesn't seem to keep the ratio :(
The diff is a weighted sum in regard to the width of each of the items. You can't just substract the same amount from every item, but you should divide it according to the relative width of each. Try, for each rectangle:
diff = (actual_row_width - max_row_width) * actual_rect_width / actual_row_width
You could also reason in percentages and calculate each new rectangle width with the following formula:
new_rect_width = (actual_rect_width / actual_row_width) * max_row_width
with (actual_rect_width / actual_row_width) being the percentage of the width of the row each rectangle occupies.
Given
W = width
H = height
You want
NH = New height
Based on this:
R = Ratio = NW / W
Then
NH = New height = H * R = H * ( NW / W )
Hey all i am trying to figure out how to resize an image that has a higher height than width. The normal width and height of the area where the image needs to be displayed is:
width = 1000px
height = 700px
What type of math would i need in order to resize that to the proper width/height above without screwing it?
The test image size is:
width = 1451
height = 2200
I was thinking of doing this:
(700/org.height)
But that does not come up with the correct number of 462.
In photoshop, changing the height to 700 yields a width value of 462.
so you want to scale your image so that it fits as large as possible within a 1000x700 square without stretching it? You could do something like this:
$availH = 700;
$availW = 1000;
$imageH = 2200;
$imageW = 1451;
// you know you want to limit the height to 700
$newH = $availH;
// figure out what the ratio was that you adjusted (will equal aprox 0.3181)
$ratio = $availH / $imageH;
// now that you have the $ratio you can apply that to the width (will equal 462)
$newW = round(imageW * $ratio);
Now you have $newW and $newH which are the new sizes for your image properly scaled. You could of course condense this down but I've written it out so each step is more clear.
Formula to keep aspect ratio when resizing is
original height / original width x new width = new height
Source: http://andrew.hedges.name/experiments/aspect_ratio/
But you want to switch it around to the following I think:
original width / original height x new height = new width
I just want to know if it is possible to get different images' sizes, and create fixed thumbnail dimension measurements for these pictures without losing their accurate aspect ratios.
So far, I have made these:
Resize different images
Maintain their aspect ratios
NOT supplying the same size (for example: 100px- height and 100px- width)
Here's the code that I am working with:
<?php
require("dbinfo.php");
$allPhotosQuery = mysql_query (" SELECT * FROM `placesImages` ");
while ($allPhotosArray = mysql_fetch_assoc ($allPhotosQuery))
{
$filename= $allPhotosArray['fileName'];
$placeId = $allPhotosArray['placeId'];
$imagePath = "placesImages/" . $placeId . "/" . $filename;
$imageSize = getimagesize($imagePath);
$imageWidth = $imageSize[0];
$imageHeight = $imageSize[1];
$newSize = ($imageWidth + $imageHeight)/($imageWidth*($imageHeight/45));
$newHeight = $imageHeight * $newSize;
$newWidth = $imageWidth * $newSize;
echo "<img src='".$imagePath."' width='".$newWidth."' height='".$newHeight."' />";
}
?>
Short of cropping, the simplest way to maintain aspect ratio while making a thumbnail is to do something similar to what you have, but set one fixed:
For example, if you want all your tumbs to be 100px wide:
$imageWidth = $imageSize[0];
$imageHeight = $imageSize[1];
$ratio=ImageWidth/$imageHeight;
$newHeight=(int)$ratio*100;
$newWidth=100;
The caveat with this is that you might end up with some funny sizes if the image has a funny ratio - as in it will happily go ahead and just do it. It might be a good idea to put some sort of check on the ratio in your code - if it it too low or too high, do something else, otherwise use this standard process.
feed this function your original image width and heights followed by the maximum constraints of your thumbnail limits and it will spit out an array with x/y of what you should set your thumbnail at to maintain aspect ratio. (anything smaller than the thumbnail will be enlarged)
function imageResizeDimensions($source_width,$source_height,$thumb_width,$thumb_height)
{
$source_ratio = $source_width / $source_height;
$thumb_ratio = $thumb_width / $thumb_height;
if($thumb_ratio > $source_ratio)
{
return array('x'=>$thumb_height * $source_ratio,'y'=>$thumb_height);
}
elseif($thumb_ratio < $source_ratio)
{
return array('x'=>$thumb_width,'y'=>$thumb_width/$source_ratio);
}
else
{
return array('x'=>$thumb_width,'y'=>$thumb_width);
}
}
Let’s begin with two constants, thumb_width and thumb_height, which are the desired width and height of your thumbnail images. They can be equal, but don’t have to be.
If you have an image that’s wider than it is tall (landscape) we can set the width to the desired width of the thumbnail, thumb_width, and adjust the height to maintain the aspect ratio.
new_width = thumb_width
new_height = thumb_height * old_height / old_width
See imagecreatetruecolor.
Then you can move the image to center it vertically within the limits of the thumbnail, producing a letterbox effect. See imagecopyresampled.
new_y = (thumb_height - new_height) / 2
For images that are taller than they are wide (portrait) the procedure is the same, but the math is a little different.
new_height = thumb_height
new_width = thumb_width * old_width / old_height
Then you can center it horizontally within the limits of the thumbnail.
new_x = (thumb_width - new_width) / 2
For more information on the basics of creating thumbnail images see Resizing images in PHP with GD and Imagick
I want the new height and width to which the image needs to be resized. There are two conditions
Width needs to be around 180px(170-180) but < 180px (uploaded image is always > 180)
Height should be max 180px (uploaded image may or may not be > 180)
If you are writing program for Linux I would recommend using ImageMagick. It is more memory efficient and probably faster than any PHP based method. Almost all servers have it installed. Following code will do the trick.
function resizeTo($source, $dest, $w=180, $h=180) {
system("convert $source -resize {$w}x{$h} $dest");
}
It will mind the aspect ratio.
Edit:
Sorry for the confusion. I think the following should do what you are looking for. It is not tested and might need a little bit debugging, if you run into trouble I can try and post again.
//accepts and returns point object (having ->x and ->y)
function resizeTo($current, $max) {
if($current->x <= $max->x && $current->y <= $max->y) //you will not need this but
return $current; // still its good to have
if( ($current->y / $max->y) > ($current->x / $max->x) ) { //y axis needs more trimming
$r=$current->y / $max->y;
$current->y = $max->y;
$current->x = $current->x / $r;
} else {
$r=$current->x / $max->x;
$current->x = $max->x;
$current->y = $current->y / $r;
}
return $current;
}
You just need a few steps:
1. scale = imageWidth / 180;
2. scale = (imageHeight/scale>180) ? imageHeight/180 : scale;
The first one will set the scale factor you need to make the width 180 (based on your comment that it is ALWAYS larger then 180)
The second one will check if the height will be larger then 180 with that scale. If it is, then the scale will be height/180. If its not, you already have the max height.
Then you also need steps to get the actual width and height:
width = imageWidth/scale;
height = imageHeight/scale;
Considering you want to make the imageWidth between 170 and 180 I guess cropping the image is also a possibility. If that is the case you need an additional check
if (width<170) {
width = 170;
height = imageHeigh / (imageWidth/170);
//resize image to width and height
//crop image to height = 180
}