# Images
[Phalcon\\Image](http://docs.iphalcon.cn/api/Phalcon_Image.html)is the component that allows you to manipulate image files. Multiple operations can be performed on the same image object.
> This guide is not intended to be a complete documentation of available methods and their arguments. Please visit the[API](http://docs.iphalcon.cn/api/index.html)for a complete reference.
## Adapters
This component makes use of adapters to encapsulate specific image manipulator programs. The following image manipulator programs are supported:
| Class | Description |
| --- | --- |
| [Phalcon\\Image\\Adapter\\Gd](http://docs.iphalcon.cn/api/Phalcon_Image_Adapter_Gd.html) | Requires the[GD PHP extension](http://php.net/manual/en/book.image.php). |
| [Phalcon\\Image\\Adapter\\Imagick](http://docs.iphalcon.cn/api/Phalcon_Image_Adapter_Imagick.html) | Requires the[ImageMagick PHP extension](http://php.net/manual/en/book.imagick.php). |
### Implementing your own adapters
The[Phalcon\\Image\\AdapterInterface](http://docs.iphalcon.cn/api/Phalcon_Image_AdapterInterface.html)interface must be implemented in order to create your own image adapters or extend the existing ones.
## Saving and rendering images
Before we begin with the various features of the image component, it’s worth understanding how to save and render these images.
~~~
<?php
$image = new \Phalcon\Image\Adapter\Gd("image.jpg");
// ...
// Overwrite the original image
$image->save();
~~~
~~~
<?php
$image = new \Phalcon\Image\Adapter\Gd("image.jpg");
// ...
// Save to 'new-image.jpg'
$image->save("new-image.jpg");
~~~
You can also change the format of the image:
~~~
<?php
$image = new \Phalcon\Image\Adapter\Gd("image.jpg");
// ...
// Save as a PNG file
$image->save("image.png");
~~~
When saving as a JPEG, you can also specify the quality as the second parameter:
~~~
<?php
$image = new \Phalcon\Image\Adapter\Gd("image.jpg");
// ...
// Save as a JPEG with 80% quality
$image->save("image.jpg", 80);
~~~
## Resizing images
There are several modes of resizing:
* `\Phalcon\Image::WIDTH`
* `\Phalcon\Image::HEIGHT`
* `\Phalcon\Image::NONE`
* `\Phalcon\Image::TENSILE`
* `\Phalcon\Image::AUTO`
* `\Phalcon\Image::INVERSE`
* `\Phalcon\Image::PRECISE`
### `\Phalcon\Image::WIDTH`
The height will automatically be generated to keep the proportions the same; if you specify a height, it will be ignored.
~~~
<?php
$image = new \Phalcon\Image\Adapter\Gd("image.jpg");
$image->resize(
300,
null,
\Phalcon\Image::WIDTH
);
$image->save("resized-image.jpg");
~~~
### `\Phalcon\Image::HEIGHT`
The width will automatically be generated to keep the proportions the same; if you specify a width, it will be ignored.
~~~
<?php
$image = new \Phalcon\Image\Adapter\Gd("image.jpg");
$image->resize(
null,
300,
\Phalcon\Image::HEIGHT
);
$image->save("resized-image.jpg");
~~~
### `\Phalcon\Image::NONE`
The`NONE`constant ignores the original image’s ratio. Neither width and height are required. If a dimension is not specified, the original dimension will be used. If the new proportions differ from the original proportions, the image may be distorted and stretched.
~~~
<?php
$image = new \Phalcon\Image\Adapter\Gd("image.jpg");
$image->resize(
400,
200,
\Phalcon\Image::NONE
);
$image->save("resized-image.jpg");
~~~
### `\Phalcon\Image::TENSILE`
Similar to the`NONE`constant, the`TENSILE`constant ignores the original image’s ratio. Both width and height are required. If the new proportions differ from the original proportions, the image may be distorted and stretched.
~~~
<?php
$image = new \Phalcon\Image\Adapter\Gd("image.jpg");
$image->resize(
400,
200,
\Phalcon\Image::NONE
);
$image->save("resized-image.jpg");
~~~
## Cropping images
For example, to get a 100px by 100px square from the centre of the image:
~~~
<?php
$image = new \Phalcon\Image\Adapter\Gd("image.jpg");
$width = 100;
$height = 100;
$offsetX = (($image->getWidth() - $width) / 2);
$offsetY = (($image->getHeight() - $height) / 2);
$image->crop($width, $height, $offsetX, $offsetY);
$image->save("cropped-image.jpg");
~~~
## Rotating images
~~~
<?php
$image = new \Phalcon\Image\Adapter\Gd("image.jpg");
// Rotate an image by 90 degrees clockwise
$image->rotate(90);
$image->save("rotated-image.jpg");
~~~
## Flipping images
You can flip an image horizontally (using the`\Phalcon\Image::HORIZONTAL`constant) and vertically (using the`\Phalcon\Image::VERTICAL`constant):
~~~
<?php
$image = new \Phalcon\Image\Adapter\Gd("image.jpg");
// Flip an image horizontally
$image->flip(
\Phalcon\Image::HORIZONTAL
);
$image->save("flipped-image.jpg");
~~~
## Sharpening images
The`sharpen()`method takes a single parameter - an integer between 0 (no effect) and 100 (very sharp):
~~~
<?php
$image = new \Phalcon\Image\Adapter\Gd("image.jpg");
$image->sharpen(50);
$image->save("sharpened-image.jpg");
~~~
## Adding watermarks to images
~~~
<?php
$image = new \Phalcon\Image\Adapter\Gd("image.jpg");
$watermark = new \Phalcon\Image\Adapter\Gd("me.jpg");
// Put the watermark in the top left corner
$offsetX = 10;
$offsetY = 10;
$opacity = 70;
$image->watermark(
$watermark,
$offsetX,
$offsetY,
$opacity
);
$image->save("watermarked-image.jpg");
~~~
Of course, you can also manipulate the watermarked image before applying it to the main image:
~~~
<?php
$image = new \Phalcon\Image\Adapter\Gd("image.jpg");
$watermark = new \Phalcon\Image\Adapter\Gd("me.jpg");
$watermark->resize(100, 100);
$watermark->rotate(90);
$watermark->sharpen(5);
// Put the watermark in the bottom right corner with a 10px margin
$offsetX = ($image->getWidth() - $watermark->getWidth() - 10);
$offsetY = ($image->getHeight() - $watermark->getHeight() - 10);
$opacity = 70;
$image->watermark(
$watermark,
$offsetX,
$offsetY,
$opacity
);
$image->save("watermarked-image.jpg");
~~~
## Blurring images
The`blur()`method takes a single parameter - an integer between 0 (no effect) and 100 (very blurry):
~~~
<?php
$image = new \Phalcon\Image\Adapter\Gd("image.jpg");
$image->blur(50);
$image->save("blurred-image.jpg");
~~~
## Pixelating images
The`pixelate()`method takes a single parameter - the higher the integer, the more pixelated the image becomes:
~~~
<?php
$image = new \Phalcon\Image\Adapter\Gd("image.jpg");
$image->pixelate(10);
$image->save("pixelated-image.jpg");
~~~
- 简介
- 安装
- 安装(installlation)
- XAMPP下的安装
- WAMP下安装
- Nginx安装说明
- Apache安装说明
- Cherokee 安装说明
- 使用 PHP 内置 web 服务器
- Phalcon 开发工具
- Linux 系统下使用 Phalcon 开发工具
- Mac OS X 系统下使用 Phalcon 开发工具
- Windows 系统下使用 Phalcon 开发工具
- 教程
- 教程 1:让我们通过例子来学习
- 教程 2:INVO简介
- 教程 3: 保护INVO
- 教程4: 使用CRUD
- 教程5: 定制INVO
- 教程 6: Vökuró
- 教程 7:创建简单的 REST API
- 组件
- 依赖注入与服务定位器
- MVC架构
- 使用控制器
- 使用模型
- 模型关系
- 事件与事件管理器
- Behaviors
- 模型元数据
- 事务管理
- 验证数据完整性
- Workingwith Models
- Phalcon查询语言
- 缓存对象关系映射
- 对象文档映射 ODM
- 使用视图
- 视图助手
- 资源文件管理
- Volt 模版引擎
- MVC 应用
- 路由
- 调度控制器
- Micro Applications
- 使用命名空间
- 事件管理器
- Request Environmen
- 返回响应
- Cookie 管理
- 生成 URL 和 路径
- 闪存消息
- 使用 Session 存储数据
- 过滤与清理
- 上下文编码
- 验证Validation
- 表单_Forms
- 读取配置
- 分页 Pagination
- 使用缓存提高性能
- 安全
- 加密与解密 Encryption/Decryption
- 访问控制列表
- 多语言支持
- 类加载器 Class Autoloader
- 日志记录_Logging
- 注释解析器 Annotations Parser
- 命令行应用 Command Line Applications
- Images
- 队列 Queueing
- 数据库抽象层
- 国际化
- 数据库迁移
- 调试应用程序
- 单元测试
- 进阶技巧与延伸阅读
- 提高性能:下一步该做什么?
- Dependency Injection Explained
- Understanding How Phalcon Applications Work
- Api
- Abstract class Phalcon\Acl