23

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

intuitivepixel
  • 23,302
  • 3
  • 57
  • 51
Igor Martins
  • 2,015
  • 7
  • 36
  • 57

2 Answers2

46

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.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • 1
    I believed you need "scale" instead of the ratio. You have to calculate the minimum scale to fit your limit, got a piece of Javascript on my hand: var scale = Math.min($window.width() / item.width, $window.height() / item.height); var itemWidth = scale * item.width; var itemHeight = scale * item.height; – Thomas Decaux Jun 11 '14 at 12:47
  • I believe your condition should be `if( $ratio < 1) {`. Am I right? – Shafizadeh Dec 14 '17 at 11:45
  • `$ratio > 1` means that the width is greater than the height, and therefore should be the one that is clamped to 500. The height is then divided by it (resulting in a smaller number). – Niet the Dark Absol Dec 14 '17 at 13:01
13

Use the Upload Class written by Colin Verot. It has all sorts of options for resizing, editing, watermarking etc... It's awesome!!

The class is maintained and used by websites all over the internet so you can rely on it to be reliable!

See here

Even though this is called the Upload Class, you can apply the same methods to files already on your server

How to Use

Follow the installation instructions on the site, quite simply, download the class and place it in your site.

Your script will then look something like this:

// Include the upload class
include('class.upload.php');

// Initiate the upload object based on the uploaded file field
$handle = new upload($_FILES['image_field']);

// Only proceed if the file has been uploaded
if($handle->uploaded) {
    // Set the new filename of the uploaded image
    $handle->file_new_name_body   = 'image_resized';
    // Make sure the image is resized
    $handle->image_resize         = true;
    // Set the width of the image
    $handle->image_x              = 100;
    // Ensure the height of the image is calculated based on ratio
    $handle->image_ratio_y        = true;
    // Process the image resize and save the uploaded file to the directory
    $handle->process('/home/user/files/');
    // Proceed if image processing completed sucessfully
    if($handle->processed) {
        // Your image has been resized and saved
        echo 'image resized';
        // Reset the properties of the upload object
        $handle->clean();
    }else{
        // Write the error to the screen
        echo 'error : ' . $handle->error;
    }
}
Ben Carey
  • 16,540
  • 19
  • 87
  • 169