devmachine/ontheio-bundle

此包已被废弃,不再维护。未建议替代包。

onthe.io 图像云 API 集成。

安装: 544

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 0

开放问题: 0

类型:symfony-bundle

1.2.0 2018-01-05 10:34 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:51:15 UTC


README

Build Status Scrutinizer Code Quality SensioLabsInsight

onthe.io 图像云 API 集成。

2017 更新

i.onthe.io 服务已不再对公众开放。考虑集成其他图像云提供商。

安装

使用 Composer 安装此包。将以下内容添加到您的 composer.json 中

{
    "require": {
        "devmachine/ontheio-bundle": "~1.0"
    }
}

在内核中注册包

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = [
        // ...

        new Devmachine\Bundle\OntheioBundle\DevmachineOntheioBundle(),
        new Sensio\Bundle\BuzzBundle\SensioBuzzBundle(),
    ];
}

更新配置

devmachine_ontheio:
    image:
        key: "your-key"
        secret: "your-secret"

示例用法

class MyController extends Controller
{
    /**
     * Upload remote URL to the cloud.
     */
    public function uploadUrlAction(Request $request)
    {
        // Get URL from request.
        $url = $request->query->get('url');

        // Upload image using URL.
        $result = $this->get('devmachine_ontheio.client.image')->uploadByUrl($url);

        // Key from image API - you can save this in DB.
        $key = $result->getKey();

        // Width of uploaded image.
        $width = $result->getWidth();

        // Height of uploaded image.
        $height = $result->getHeight();

        // Check if same URL was uploaded before.
        $new = $result->isNew();

        // You can render hosted URLs with image helper.
        return $this->render('PathToTemplate.html.twig', [
            // Hosted URL of original image.
            'url' => $this->get('devmachine_ontheio.helper.image')->url($key),

            // Resize image into 200x150.
            'thumbnail_url' => $this->get('devmachine_ontheio.helper.image')->resizeUrl($key, 200, 150),

            // Crop image into 150x150 starting from (50, 50).
            'avatar_url' => $this->get('devmachine_ontheio.helper.image')->cropUrl($key, 150, 150, 50, 50),
        ]);
    }

    /**
     * Upload image from file i.e. convert local file to hosted image.
     */
    public function uploadFileAction()
    {
        // Assuming $filepath is set.
        $result = $this->get('devmachine_ontheio.client.image')->uploadByFile($filepath);
    }
}

Twig 辅助函数

{# PathToTemplate.html.twig #}

Original:  <img src="{{ key | devmachine_ontheio_image_url }}" alt=""><br>
Thumbnail: <img src="{{ key | devmachine_ontheio_image_url(200, 150) }}" alt=""><br>
Avatar:    <img src="{{ key | devmachine_ontheio_image_url(150, 150, 50, 50) }}" alt="">

注意

API 文档建议您可以旋转和删除图像。尽管已实现了这些功能的集成,但我未能实现描述的功能。您可以检查 ImageClient::rotate()ImageClient::delete() 方法。

表单集成

您可以将图像上传直接集成到您的表单中。

class MyType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('foo', 'text')
            ->add('bar', 'text')
            ->add('images', 'devmachine_ontheio_image_gallery')
        ;
    }
}

Bootstrap 3 主题

Gallery

目前仅支持通过 URL 上传图像。

相册表单类型假定以下 JavaScript 和 CSS 代码存在

示例 bower.json

{
    "dependencies": {
        "bootbox": "~4.4",
        "magnific-popup": "~1.0"
    }
}