PHP 按最大宽度或重量按比例调整图像大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16627476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:27:49  来源:igfitidea点击:

PHP resize image proportionally with max width or weight

php

提问by Igor Martins

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

有任何 php 脚本可以按最大宽度或高度按比例调整图像大小?

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

例如:我上传了图片,这个原始尺寸是 w:500 h:1000。但是,我想调整这个最大高度是宽度和高度是 500 ......脚本调整图像大小为 w: 250 h: 500

回答by Niet the Dark Absol

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.

您需要添加一些错误检查,但这应该可以帮助您入门。

回答by Ben Carey

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

使用 Colin Verot 编写的上传类。它有各种调整大小、编辑、水印等选项......太棒了!!

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