devmachine / ontheio-bundle
此包已被废弃,不再维护。未建议替代包。
onthe.io 图像云 API 集成。
1.2.0
2018-01-05 10:34 UTC
Requires
- php: ^5.5|^7.0
- sensio/buzz-bundle: ^1.0
- symfony/framework-bundle: ^2.8|^3.0|^4.0
- symfony/twig-bridge: ^2.8|^3.0|^4.0
Requires (Dev)
- phpunit/phpunit: ^4.0|^5.0
This package is not auto-updated.
Last update: 2022-02-01 12:51:15 UTC
README
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 主题
目前仅支持通过 URL 上传图像。
相册表单类型假定以下 JavaScript 和 CSS 代码存在
示例 bower.json
{ "dependencies": { "bootbox": "~4.4", "magnific-popup": "~1.0" } }