Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP resize image proportionally with max width or weight [closed]

Tags:

There is any php script that resize image proportionally with max widht or height??

Ex: I upload image and this original size is w:500 h:1000. But, I want to resize this thats max height is width and height is 500... That the script resize the image for w: 250 h: 500

like image 363
Igor Martins Avatar asked May 18 '13 18:05

Igor Martins


People also ask

How do you scale a photo proportionally?

If the Shift key is held down whilst moving the handle, then the proportions of the object will be preserved. For example, if I hold Shift and drag the bottom edge upwards to reduce the size by half, then the right edge will automatically move to the left to reduce the width of the object by the same amount.

How do I scale an image in PHP?

The imagescale() function is an inbuilt function in PHP which is used to scale an image using the given new width and height. Parameters: This function accepts four parameters as mentioned above and described below: $image: It is returned by one of the image creation functions, such as imagecreatetruecolor().

How do you resize an object proportionally?

Manually setting the object to a specific proportion:Right-click the object. On the shortcut menu, click Format<object type>. In the dialog box, click the Size tab. Under Scale, enter the percentage of the original height or width you want the object resized to.


1 Answers

All you need is the aspect ratio. Something along these lines:

$fn = $_FILES['image']['tmp_name']; $size = getimagesize($fn); $ratio = $size[0]/$size[1]; // width/height if( $ratio > 1) {     $width = 500;     $height = 500/$ratio; } else {     $width = 500*$ratio;     $height = 500; } $src = imagecreatefromstring(file_get_contents($fn)); $dst = imagecreatetruecolor($width,$height); imagecopyresampled($dst,$src,0,0,0,0,$width,$height,$size[0],$size[1]); imagedestroy($src); imagepng($dst,$target_filename_here); // adjust format as needed imagedestroy($dst); 

You'll need to add some error checking, but this should get you started.

like image 55
Niet the Dark Absol Avatar answered Sep 28 '22 11:09

Niet the Dark Absol