stormwild/image-resizer

使用 GD 重新调整图像大小的 PHP 类

1.0.0 2015-01-07 12:00 UTC

This package is not auto-updated.

Last update: 2024-09-28 17:12:31 UTC


README

使用 GD 重新调整图像大小的 PHP 类

历史记录

虽然我在最近的项目中使用了这个类的版本,但这实际上只是我创建一个简单的 PHP 类库,通过 Composer 和 Packagist 分享的一个练习。

设置

此软件包通过 Packagist 提供,供应商和包标识符与该存储库相同。

如果使用 Composer,请在您的 composer.json 文件中添加

{
    "require": {
        "stormwild/image-resizer": "1.0.0"
    }
}

否则

include '/path/to/ImageResizer.php';

因为此类使用命名空间,在实例化对象时,您可以使用完全限定的命名空间

$resized = \Stormwild\ImageResizer::resizer($path, $width, $height);

或者为其设置别名

use \Stormwild\ImageResizer;

$resized = ImageResizer::resizer($path, $width, $height);

用法

    // possibly in controller action
    // check if image uploaded successfully
    // get path to uploaded image
    
    $path = "path/to/uploaded/file.ext"; // real path to file on server
    $width = 1050;
    $height = 700;    
    
    $resized = ImageResizer::resize($path, $width, $height);
    
    if($resized) {
        // upload to cloud storage
        // get url from cloud
        // save model with reference to image url
    }

该函数调整大小并替换(更新)上传的文件到指定的尺寸。

参考

感谢网络上许多关于使用 GD 进行图像调整大小的代码示例。特别是,我参考了以下内容