You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: user_guide_src/source/libraries/images.rst
+59-57Lines changed: 59 additions & 57 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -33,8 +33,8 @@ Service function:
33
33
34
34
The available Handlers are as follows:
35
35
36
-
- gd The GD/GD2 image library
37
-
- imagick The ImageMagick library.
36
+
- ``gd`` The GD/GD2 image library
37
+
- ``imagick`` The ImageMagick library.
38
38
39
39
If using the ImageMagick library, you must set the path to the library on your
40
40
server in **app/Config/Images.php**.
@@ -43,8 +43,9 @@ server in **app/Config/Images.php**.
43
43
loaded on the server. As long as your script can access the library
44
44
and can run ``exec()`` on the server, it should work.
45
45
46
+
*******************
46
47
Processing an Image
47
-
===================
48
+
*******************
48
49
49
50
Regardless of the type of processing you would like to perform
50
51
(resizing, cropping, rotation, or watermarking), the general process is
@@ -57,7 +58,7 @@ For example, to create an image thumbnail you'll do this:
57
58
The above code tells the library to look for an image
58
59
called *mypic.jpg* located in the source_image folder, then create a
59
60
new image from it that is 100 x 100pixels using the GD2 image_library,
60
-
and save it to a new file (the thumb). Since it is using the fit() method,
61
+
and save it to a new file (the thumb). Since it is using the ``fit()`` method,
61
62
it will attempt to find the best portion of the image to crop based on the
62
63
desired aspect ratio, and then crop and resize the result.
63
64
@@ -97,19 +98,20 @@ You will need to include the image resource or you will end up with an exact cop
97
98
98
99
.. literalinclude:: images/006.php
99
100
101
+
******************
100
102
Processing Methods
101
-
==================
103
+
******************
102
104
103
105
There are seven available processing methods:
104
106
105
-
- $image->crop()
106
-
- $image->convert()
107
-
- $image->fit()
108
-
- $image->flatten()
109
-
- $image->flip()
110
-
- $image->resize()
111
-
- $image->rotate()
112
-
- $image->text()
107
+
- ``$image->crop()``
108
+
- ``$image->convert()``
109
+
- ``$image->fit()``
110
+
- ``$image->flatten()``
111
+
- ``$image->flip()``
112
+
- ``$image->resize()``
113
+
- ``$image->rotate()``
114
+
- ``$image->text()``
113
115
114
116
These methods return the class instance so they can be chained together, as shown above.
115
117
If they fail they will throw a ``CodeIgniter\Images\ImageException`` that contains
@@ -119,41 +121,41 @@ error upon failure, like this:
119
121
.. literalinclude:: images/007.php
120
122
121
123
Cropping Images
122
-
---------------
124
+
===============
123
125
124
126
Images can be cropped so that only a portion of the original image remains. This is often used when creating
125
127
thumbnail images that should match a certain size/aspect ratio. This is handled with the ``crop()`` method::
126
128
127
129
crop(int $width = null, int $height = null, int $x = null, int $y = null, bool $maintainRatio = false, string $masterDim = 'auto')
128
130
129
-
- **$width** is the desired width of the resulting image, in pixels.
130
-
- **$height** is the desired height of the resulting image, in pixels.
131
-
- **$x** is the number of pixels from the left side of the image to start cropping.
132
-
- **$y** is the number of pixels from the top of the image to start cropping.
133
-
- **$maintainRatio** will, if true, adjust the final dimensions as needed to maintain the image's original aspect ratio.
134
-
- **$masterDim** specifies which dimension should be left untouched when $maintainRatio is true. Values can be: 'width', 'height', or 'auto'.
131
+
- ``$width`` is the desired width of the resulting image, in pixels.
132
+
- ``$height`` is the desired height of the resulting image, in pixels.
133
+
- ``$x`` is the number of pixels from the left side of the image to start cropping.
134
+
- ``$y`` is the number of pixels from the top of the image to start cropping.
135
+
- ``$maintainRatio`` will, if true, adjust the final dimensions as needed to maintain the image's original aspect ratio.
136
+
- ``$masterDim`` specifies which dimension should be left untouched when ``$maintainRatio`` is true. Values can be: ``'width'``, ``'height'``, or ``'auto'``.
135
137
136
138
To take a 50x50 pixel square out of the center of an image, you would need to first calculate the appropriate x and y
137
139
offset values:
138
140
139
141
.. literalinclude:: images/008.php
140
142
141
143
Converting Images
142
-
-----------------
144
+
=================
143
145
144
146
The ``convert()`` method changes the library's internal indicator for the desired file format. This doesn't touch the actual image resource, but indicates to ``save()`` what format to use::
145
147
146
148
convert(int $imageType)
147
149
148
-
- **$imageType** is one of PHP's image type constants (see for example https://www.php.net/manual/en/function.image-type-to-mime-type.php):
150
+
- ``$imageType`` is one of PHP's image type constants (see for example https://www.php.net/manual/en/function.image-type-to-mime-type.php):
149
151
150
152
.. literalinclude:: images/009.php
151
153
152
154
.. note:: ImageMagick already saves files in the type
153
-
indicated by their extension, ignoring **$imageType**
155
+
indicated by their extension, ignoring ``$imageType``.
154
156
155
157
Fitting Images
156
-
--------------
158
+
==============
157
159
158
160
The ``fit()`` method aims to help simplify cropping a portion of an image in a "smart" way, by doing the following steps:
159
161
@@ -165,16 +167,16 @@ The ``fit()`` method aims to help simplify cropping a portion of an image in a "
165
167
166
168
fit(int $width, int $height = null, string $position = 'center')
167
169
168
-
- **$width** is the desired final width of the image.
169
-
- **$height** is the desired final height of the image.
170
-
- **$position** determines the portion of the image to crop out. Allowed positions: 'top-left', 'top', 'top-right', 'left', 'center', 'right', 'bottom-left', 'bottom', 'bottom-right'.
170
+
- ``$width`` is the desired final width of the image.
171
+
- ``$height`` is the desired final height of the image.
172
+
- ``$position`` determines the portion of the image to crop out. Allowed positions: ``'top-left'``, ``'top'``, ``'top-right'``, ``'left'``, ``'center'``, ``'right'``, ``'bottom-left'``, ``'bottom'``, ``'bottom-right'``.
171
173
172
174
This provides a much simpler way to crop that will always maintain the aspect ratio:
173
175
174
176
.. literalinclude:: images/010.php
175
177
176
178
Flattening Images
177
-
-----------------
179
+
=================
178
180
179
181
The ``flatten()`` method aims to add a background color behind transparent images (PNG) and convert RGBA pixels to RGB pixels
180
182
@@ -184,57 +186,57 @@ The ``flatten()`` method aims to add a background color behind transparent image
184
186
185
187
flatten(int $red = 255, int $green = 255, int $blue = 255)
186
188
187
-
- **$red** is the red value of the background.
188
-
- **$green** is the green value of the background.
189
-
- **$blue** is the blue value of the background.
189
+
- ``$red`` is the red value of the background.
190
+
- ``$green`` is the green value of the background.
191
+
- ``$blue`` is the blue value of the background.
190
192
191
193
.. literalinclude:: images/011.php
192
194
193
195
Flipping Images
194
-
---------------
196
+
===============
195
197
196
198
Images can be flipped along either their horizontal or vertical axis::
197
199
198
200
flip(string $dir)
199
201
200
-
- **$dir** specifies the axis to flip along. Can be either 'vertical' or 'horizontal'.
202
+
- ``$dir`` specifies the axis to flip along. Can be either ``'vertical'`` or ``'horizontal'``.
201
203
202
204
.. literalinclude:: images/012.php
203
205
204
206
Resizing Images
205
-
---------------
207
+
===============
206
208
207
-
Images can be resized to fit any dimension you require with the resize() method::
209
+
Images can be resized to fit any dimension you require with the ``resize()`` method::
0 commit comments