图像处理类
CodeIgniter 的图像处理类可以使你完成以下的操作:
- 调整图像大小
- 创建缩略图
- 图像裁剪
- 图像旋转
- 添加图像水印
可以很好的支持三个主流的图像库:GD/GD2, NetPBM, and ImageMagick。
注意: 添加水印操作仅仅在使用GD/GD2时可用。另外,即使支持其他的图像处理库,但是为了计算图像的属性,GD是必需的。然而,将使用你制定的库来进行图像处理操作。
初始化类
像 CodeIgniter 的大多数类一样,图像处理类在你的控制器里使用 $this->load->library 方法来初始化:
$this->load->library('image_lib');
在图像处理库被载入后就已经做好被使用的准备了。你将用来调用所有图像处理方法的图像处理库对象是:$this->image_libO
处理一个图像
不管你想进行何种图像处理操作(调整大小,图像裁剪,图像旋转,添加水印),通常过程都是一样的。你先设置一些你想进行的图像操作的参数,然后调用四个可用方法中的一个。例如,创建一个图像缩略图:
$config['image_library'] = 'gd2';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
以上代码告诉image_resize函数去查找位于source_image目录且名为mypic.jpg的图片,然后运用GD2图像库创建75 X 50像素的缩略图。 Since the maintain_ratio option is enabled, the thumb will be as close to the target width and height as possible while preserving the original aspect ratio. The thumbnail will be called mypic_thumb.jpg
Note: In order for the image class to be allowed to do any processing, the folder containing the image files must have write permissions.
处理函数
有五个处理函数可以调用:
- $this->image_lib->resize()
- $this->image_lib->crop()
- $this->image_lib->rotate()
- $this->image_lib->watermark()
- $this->image_lib->clear()
当调用成功时,这些函数会返回 TRUE, 否则会返回 FALSE. 如果调用失败时,用以下函数可以获取错误信息:
echo $this->image_lib->display_errors();
像下面这样当调用失败时显示错误,是一个好的例子来有条件的使用处理函数:
if ( ! $this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
注意: 你可以随意的给错误使用HTML的格式。像下面这样,添加打开/关闭标记符:
$this->image_lib->display_errors('<p>', '</p>');
参数
你可以用下面的参数来对图像处理进行调配,既而满足你的要求。
注意,不是所有的参数都可以应用到任何得函数中。例如,x/y 轴参数只能被图像裁剪使用。但是,宽度和高度参数对裁剪函数是无效的。 "可用性" 列指明了哪些函数是可以使用对应的参数的。
可用性图列:
- R - 调整图像大小
- C - 图像裁剪
- X - 图像旋转
- W - 添加图像水纹
| 参数 | 默认值 | 选项 | 描述 | 可用性 |
|---|---|---|---|---|
| image_library | GD2 | GD, GD2, ImageMagick, NetPBM | 设置图像库 | R, C, X, W |
| library_path | 无 | 无 | 设置 ImageMagick 或 NetPBM 库在服务器上的路径。要使用其中任何一个,你都需要设置它们的路径 | R, C, X |
| source_image | 无 | 无 | 设置原始图像的名字/路径。 对路径而言,只能是相对或绝对的服务器路径,不能使用URL。 | R, C, S, W |
| dynamic_output | FALSE | TRUE/FALSE (布尔值) | 决定新图像的生成是要写入硬盘还是动态的存在。Determines whether the new image file should be written to disk or generated dynamically. 注意,如果是动态生成的话,图像生成后是不能在页面中定位的。它简单的在你的浏览器中以原图像输出。同时有图像It simply outputs the raw image dynamically to your browser, along with image headers. | R, C, X, W |
| quality | 90% | 1 - 100% | 设置图像的品质。品质越高,图像文件越大 | R, C, X, W |
| new_image | None | None | 设置图像的目标名/路径。 当复制图像时,你会用到这个参数的。这个路径必须是相对或绝对的服务器路径,不能是URL | R, C, X, W |
| width | None | None | 设置你想要得图像宽度。 | R, C |
| height | None | None | 设置你想要得图像高度 | R, C |
| create_thumb | FALSE | TRUE/FALSE (boolean) | 让图像处理函数产生一个预览图像 | R |
| thumb_marker | _thumb | 无 | 指定预览图像的标示。它将在被插入文件扩展名之前。例如,mypic.jpg 将会变成 mypic_thumb.jpg | R |
| maintain_ratio | TRUE | TRUE/FALSE (boolean) | Specifies whether to maintain the original aspect ratio when resizing or use hard values. | R, C |
| master_dim | auto | auto, width, height | Specifies what to use as the master axis when resizing or creating thumbs. For example, let's say you want to resize an image to 100 X 75 pixels. If the source image size does not allow perfect resizing to those dimensions, this setting determines which axis should be used as the hard value. "auto" sets the axis automatically based on whether the image is taller then wider, or vice versa. | R |
| rotation_angle | None | 90, 180, 270, vrt, hor | Specifies the angle of rotation when rotating images. Note that PHP rotates counter-clockwise, so a 90 degree rotation to the right must be specified as 270. | X |
| x_axis | None | None | Sets the X coordinate in pixels for image cropping. For example, a setting of 30 will crop an image 30 pixels from the left. | C |
| y_axis | None | None | Sets the Y coordinate in pixels for image cropping. For example, a setting of 30 will crop an image 30 pixels from the top. | C |
Setting preferences in a config file
If you prefer not to set preferences using the above method, you can instead put them into a config file. Simply create a new file called image_lib.php, add the $config array in that file. Then save the file in: config/image_lib.php and it will be used automatically. You will NOT need to use the $this->image_lib->initialize function if you save your preferences in a config file.
$this->image_lib->resize()
The image resizing function lets you resize the original image, create a copy (with or without resizing), or create a thumbnail image.
For practical purposes there is no difference between creating a copy and creating a thumbnail except a thumb will have the thumbnail marker as part of the name (ie, mypic_thumb.jpg).
All preferences listed in the table above are available for this function except these three: rotation_angle, x_axis, and y_axis.
Creating a Thumbnail
The resizing function will create a thumbnail file (and preserve the original) if you set this preference to TRUE:
$config['create_thumb'] = TRUE;
This single preference determines whether a thumbnail is created or not.
Creating a Copy
The resizing function will create a copy of the image file (and preserve the original) if you set a path and/or a new filename using this preference:
$config['new_image'] = '/path/to/new_image.jpg';
Notes regarding this preference:
- If only the new image name is specified it will be placed in the same folder as the original
- If only the path is specified, the new image will be placed in the destination with the same name as the original.
- If both the path and image name are specified it will placed in its own destination and given the new name.
Resizing the Original Image
If neither of the two preferences listed above (create_thumb, and new_image) are used, the resizing function will instead target the original image for processing.
$this->image_lib->crop()
The cropping function works nearly identically to the resizing function except it requires that you set preferences for the X and Y axis (in pixels) specifying where to crop, like this:
$config['x_axis'] = '100';
$config['y_axis'] = '40';
All preferences listed in the table above are available for this function except these: rotation_angle, width, height, create_thumb, new_image.
Here's an example showing how you might crop an image:
$config['image_library'] = 'imagemagick';
$config['library_path'] = '/usr/X11R6/bin/';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['x_axis'] = '100';
$config['y_axis'] = '60';
$this->image_lib->initialize($config);
if ( ! $this->image_lib->crop())
{
echo $this->image_lib->display_errors();
}
Note: Without a visual interface it is difficult to crop images, so this function is not very useful unless you intend to build such an interface. That's exactly what we did using for the photo gallery module in ExpressionEngine, the CMS we develop. We added a JavaScript UI that lets the cropping area be selected.
$this->image_lib->rotate()
The image rotation function requires that the angle of rotation be set via its preference:
$config['rotation_angle'] = '90';
There are 5 rotation options:
- 90 - rotates counter-clockwise by 90 degrees.
- 180 - rotates counter-clockwise by 180 degrees.
- 270 - rotates counter-clockwise by 270 degrees.
- hor - flips the image horizontally.
- vrt - flips the image vertically.
Here's an example showing how you might rotate an image:
$config['image_library'] = 'netpbm';
$config['library_path'] = '/usr/bin/';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['rotation_angle'] = 'hor';
$this->image_lib->initialize($config);
if ( ! $this->image_lib->rotate())
{
echo $this->image_lib->display_errors();
}
$this->image_lib->clear()
The clear function resets all of the values used when processing an image. You will want to call this if you are processing images in a loop.
$this->image_lib->clear();
图像水印处理
水印处理功能需要 GD/GD2 库的支持.
水印处理的两种类型
你可以使用以下两种图像水印处理方式:
- Text: The watermark message will be generating using text, either with a True Type font that you specify, or using the native text output that the GD library supports. If you use the True Type version your GD installation must be compiled with True Type support (most are, but not all).
- Overlay: 水印信息将以图像方式生成,新生成的水印图像(通常是透明的 PNG 或者 GIF)将覆盖在原图像上.
水印处理一个图像
类似使用其他类型图像函数(resizing, cropping, and rotating), 你也要对水印处理函数进行参数设置来生成你要得结果. 例子如下:
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['wm_text'] = 'Copyright 2006 - John Doe';
$config['wm_type'] = 'text';
$config['wm_font_path'] = './system/fonts/texb.ttf';
$config['wm_font_size'] = '16';
$config['wm_font_color'] = 'ffffff';
$config['wm_vrt_alignment'] = 'bottom';
$config['wm_hor_alignment'] = 'center';
$config['wm_padding'] = '20';
$this->image_lib->initialize($config);
$this->image_lib->watermark();
上面的例子是使用16像素 True Type 字体来生成文本水印"Copyright 2006 - John Doe" 该水印将出现在离图像底部20像素的中底部位置,
注意: 当调用图像类处理图像时,任何目标图片文件必须有 "写入"的权限, 例如:777.
水印处理参数
这个表里列举了两种水印处理 (text or overlay) 的可设置的参数
| 参数 | 默认值 | 选项 | 描述 |
|---|---|---|---|
| wm_type | text | text, overlay | 设置想要使用的水银处理类型. |
| source_image | 无 | 无 | 设置原图像的名字和路径. 路径必须是相对或绝对路径,但不能是URL. |
| dynamic_output | FALSE | TRUE/FALSE (boolean) | Determines whether the new image file should be written to disk or generated dynamically. Note: If you choose the dynamic setting, only one image can be shown at a time, and it can't be positioned on the page. It simply outputs the raw image dynamically to your browser, along with image headers. |
| quality | 90% | 1 - 100% | Sets the quality of the image. The higher the quality the larger the file size. |
| padding | None | A number | The amount of padding, set in pixels, that will be applied to the watermark to set it away from the edge of your images. |
| wm_vrt_alignment | bottom | top, middle, bottom | Sets the vertical alignment for the watermark image. |
| wm_hor_alignment | center | left, center, right | Sets the horizontal alignment for the watermark image. |
| wm_vrt_offset | None | None | You may specify a vertical offset (in pixels) to apply to the watermark position. The offset normally moves the watermark to the right, except if you have your alignment set to "right" then your offset value will move the watermark toward the left of the image. |
| wm_hor_offset | None | None | You may specify a horizontal offset (in pixels) to apply to the watermark position. The offset normally moves the watermark down, except if you have your alignment set to "bottom" then your offset value will move the watermark toward the top of the image. |
Text Preferences
This table shown the preferences that are available for the text type of watermarking.
| Preference | Default Value | Options | Description |
|---|---|---|---|
| wm_text | None | None | The text you would like shown as the watermark. Typically this will be a copyright notice. |
| wm_font_path | None | None | The server path to the True Type Font you would like to use. If you do not use this option, the native GD font will be used. |
| wm_font_size | 16 | None | The size of the text. Note: If you are not using the True Type option above, the number is set using a range of 1 - 5. Otherwise, you can use any valid pixel size for the font you're using. |
| wm_font_color | ffffff | None | The font color, specified in hex. Note, you must use the full 6 character hex value (ie, 993300), rather than the three character abbreviated version (ie fff). |
| wm_shadow_color | None | None | The color of the drop shadow, specified in hex. If you leave this blank a drop shadow will not be used. Note, you must use the full 6 character hex value (ie, 993300), rather than the three character abbreviated version (ie fff). |
| wm_shadow_distance | 3 | None | The distance (in pixels) from the font that the drop shadow should appear. |
Overlay Preferences
This table shown the preferences that are available for the overlay type of watermarking.
| Preference | Default Value | Options | Description |
|---|---|---|---|
| wm_overlay_path | None | None | The server path to the image you wish to use as your watermark. Required only if you are using the overlay method. |
| wm_opacity | 50 | 1 - 100 | Image opacity. You may specify the opacity (i.e. transparency) of your watermark image. This allows the watermark to be faint and not completely obscure the details from the original image behind it. A 50% opacity is typical. |
| wm_x_transp | 4 | A number | If your watermark image is a PNG or GIF image, you may specify a color on the image to be "transparent". This setting (along with the next) will allow you to specify that color. This works by specifying the "X" and "Y" coordinate pixel (measured from the upper left) within the image that corresponds to a pixel representative of the color you want to be transparent. |
| wm_y_transp | 4 | A number | Along with the previous setting, this allows you to specify the coordinate to a pixel representative of the color you want to be transparent. |