pixst/pixst-yii2

PHP实现的Pix Street API

v1.0.0 2015-11-06 20:21 UTC

This package is not auto-updated.

Last update: 2024-10-03 02:15:57 UTC


README

这是Yii 2.0的Pix Street API扩展。Pix Street是一个具有高级功能的云图像托管服务。Pix Street

安装

通过composer安装库是首选方式。

运行

composer.phar require pixst/pixst

或添加

"pixst/pixst-yii2": "~1.0.0",

到composer.json的require部分。

配置

要使用此扩展,请在应用程序配置中添加以下代码

return [
    //....
    'components' => [
        'pixst' => [
            'class' => 'pixst\ClientJson',
            'apiKey' => 'abcdef0123456789',
        ],
    ],
];

使用方法

创建相册并存储图片

$pix = Yii::$app->pixst;

// Create new album with ID "testAlbum" and name "Test album"
$pix->albumCreate('testAlbum', 'Test album');

// Create public image with ID "testImage-1" from remote URL "https://cdn.pix.st/a.jpg"
// and put it in album "Test album"
$pix->imageCreate(
    'testImage-1',
    ['url' => 'https://cdn.pix.st/a.jpg'],
    true,
    false,
    'testAlbum'
);

// Create public image with ID "testImage-2" from local file, rotate clockwise by 90°,
// resize to 640 x 480 and place the result in album "Test album" with short URL
$pix->imageCreate(
    'testImage-2',
    ['base64' => base64_encode(file_get_contents('local-image-file.jpg'))],
    true,
    true,
    'testAlbum',
    'test-image-2.jpg',
    ['test', 'image'],
    [
        'clockwise' => true,
        'angle' => 90
    ],
    [
        width: 640,
        height: 480,
        keepRatio: true
    ],
    null,
    98
);

$allResults = $pix->run(true);

foreach ($allResults as $action) {
    if ($action->isSuccessful) {
        print_r($action->result);
    } else {
        print_r($action->error);
    }
}

查找图片

$pix = Yii::$app->pixst;

// Find all public JPEG images in album "testAlbum" with width not less than 640 px and
// file size greater than 100 kB
$query = $pix->imageSearch(
    'testAlbum',
    'jpg',
    [
        'gte' => 640
    ],
    null,
    [
        'gt' => 102400
    ],
    true
);

$pix->run();

if ($query->isSuccessful) {
    $result = $query->result;

    echo 'Total: ' . $result['total'] . PHP_EOL;

    foreach ($result['images'] as $image) {
        echo 'Image ID: ' . $image['id'] . PHP_EOL;
    }
}

下载图片并从服务器删除

$pix = Yii::$app->pixst;

try {
    // Download image
    $binary = $pix->imageGet('testImage');
    file_put_contents('test-image.jpg', $binary);

    // Remove image from server
    $pix->imageRemove('testImage');
    $pix->run();
} catch (\Exception) {
    echo $e->getMessage();
}