Resizing images using php

前端 未结 7 1873
慢半拍i
慢半拍i 2021-02-10 16:17

I\'m using a neat little php script to resize my images to fit in a 300x300 pixel square, while keeping the aspect ratio. I got the script from here.

Here is the whole s

相关标签:
7条回答
  • 2021-02-10 16:59

    It's working correctly - it's resizing to a height of 300 and keeping the aspect ratio (which, in this case, makes the width 400). You need to see which side is the biggest side (or, more accurately, the side furthest from the aspect ratio you require) first, and then only call one function (resizeToWidth() or resizeToHeight()).

    If I had to use that class, I think this will work:

    $image = new SimpleImage();
    $size = getImageSize($_FILES['uploaded_image']['tmp_name']);
    if ($size[0] > 300) {
        $image->load($_FILES['uploaded_image']['tmp_name']);
        $image->resizeToWidth(300);
        $image->save('./images/photo'.$id.'.jpg');
    } else {
        move_uploaded_file($_FILES['uploaded_image']['tmp_name'], './images/photo'.$id.'.jpg');
    }
    
    $size = getImageSize('./images/photo'.$id.'.jpg');
    if ($size[1] > 300) {
        $image->load('./images/photo'.$id.'.jpg');
        $image->resizeToHeight(300);
        $image->save('./images/photo'.$id.'.jpg');
    }
    
    header("Location: people.php");
    exit;
    
    0 讨论(0)
提交回复
热议问题