Skip to content

Commit 10ae2d4

Browse files
authored
Merge pull request #5968 from kenjis/fix-docs-images.rst
docs: improve images.rst
2 parents 94fd4cc + a21aac8 commit 10ae2d4

6 files changed

Lines changed: 67 additions & 68 deletions

File tree

user_guide_src/source/libraries/images.rst

Lines changed: 59 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ Service function:
3333

3434
The available Handlers are as follows:
3535

36-
- gd The GD/GD2 image library
37-
- imagick The ImageMagick library.
36+
- ``gd`` The GD/GD2 image library
37+
- ``imagick`` The ImageMagick library.
3838

3939
If using the ImageMagick library, you must set the path to the library on your
4040
server in **app/Config/Images.php**.
@@ -43,8 +43,9 @@ server in **app/Config/Images.php**.
4343
loaded on the server. As long as your script can access the library
4444
and can run ``exec()`` on the server, it should work.
4545

46+
*******************
4647
Processing an Image
47-
===================
48+
*******************
4849

4950
Regardless of the type of processing you would like to perform
5051
(resizing, cropping, rotation, or watermarking), the general process is
@@ -57,7 +58,7 @@ For example, to create an image thumbnail you'll do this:
5758
The above code tells the library to look for an image
5859
called *mypic.jpg* located in the source_image folder, then create a
5960
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,
6162
it will attempt to find the best portion of the image to crop based on the
6263
desired aspect ratio, and then crop and resize the result.
6364

@@ -97,19 +98,20 @@ You will need to include the image resource or you will end up with an exact cop
9798

9899
.. literalinclude:: images/006.php
99100

101+
******************
100102
Processing Methods
101-
==================
103+
******************
102104

103105
There are seven available processing methods:
104106

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()``
113115

114116
These methods return the class instance so they can be chained together, as shown above.
115117
If they fail they will throw a ``CodeIgniter\Images\ImageException`` that contains
@@ -119,41 +121,41 @@ error upon failure, like this:
119121
.. literalinclude:: images/007.php
120122

121123
Cropping Images
122-
---------------
124+
===============
123125

124126
Images can be cropped so that only a portion of the original image remains. This is often used when creating
125127
thumbnail images that should match a certain size/aspect ratio. This is handled with the ``crop()`` method::
126128

127129
crop(int $width = null, int $height = null, int $x = null, int $y = null, bool $maintainRatio = false, string $masterDim = 'auto')
128130

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'``.
135137

136138
To take a 50x50 pixel square out of the center of an image, you would need to first calculate the appropriate x and y
137139
offset values:
138140

139141
.. literalinclude:: images/008.php
140142

141143
Converting Images
142-
-----------------
144+
=================
143145

144146
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::
145147

146148
convert(int $imageType)
147149

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):
149151

150152
.. literalinclude:: images/009.php
151153

152154
.. note:: ImageMagick already saves files in the type
153-
indicated by their extension, ignoring **$imageType**
155+
indicated by their extension, ignoring ``$imageType``.
154156

155157
Fitting Images
156-
--------------
158+
==============
157159

158160
The ``fit()`` method aims to help simplify cropping a portion of an image in a "smart" way, by doing the following steps:
159161

@@ -165,16 +167,16 @@ The ``fit()`` method aims to help simplify cropping a portion of an image in a "
165167

166168
fit(int $width, int $height = null, string $position = 'center')
167169

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'``.
171173

172174
This provides a much simpler way to crop that will always maintain the aspect ratio:
173175

174176
.. literalinclude:: images/010.php
175177

176178
Flattening Images
177-
-----------------
179+
=================
178180

179181
The ``flatten()`` method aims to add a background color behind transparent images (PNG) and convert RGBA pixels to RGB pixels
180182

@@ -184,57 +186,57 @@ The ``flatten()`` method aims to add a background color behind transparent image
184186

185187
flatten(int $red = 255, int $green = 255, int $blue = 255)
186188

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.
190192

191193
.. literalinclude:: images/011.php
192194

193195
Flipping Images
194-
---------------
196+
===============
195197

196198
Images can be flipped along either their horizontal or vertical axis::
197199

198200
flip(string $dir)
199201

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'``.
201203

202204
.. literalinclude:: images/012.php
203205

204206
Resizing Images
205-
---------------
207+
===============
206208

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::
208210

209211
resize(int $width, int $height, bool $maintainRatio = false, string $masterDim = 'auto')
210212

211-
- **$width** is the desired width of the new image in pixels
212-
- **$height** is the desired height of the new image in pixels
213-
- **$maintainRatio** determines whether the image is stretched to fit the new dimensions, or the original aspect ratio is maintained.
214-
- **$masterDim** specifies which axis should have its dimension honored when maintaining ratio. Either 'width', 'height'.
213+
- ``$width`` is the desired width of the new image in pixels
214+
- ``$height`` is the desired height of the new image in pixels
215+
- ``$maintainRatio`` determines whether the image is stretched to fit the new dimensions, or the original aspect ratio is maintained.
216+
- ``$masterDim`` specifies which axis should have its dimension honored when maintaining ratio. Either ``'width'``, ``'height'``.
215217

216218
When resizing images you can choose whether to maintain the ratio of the original image, or stretch/squash the new
217-
image to fit the desired dimensions. If $maintainRatio is true, the dimension specified by $masterDim will stay the same,
219+
image to fit the desired dimensions. If ``$maintainRatio`` is true, the dimension specified by ``$masterDim`` will stay the same,
218220
while the other dimension will be altered to match the original image's aspect ratio.
219221

220222
.. literalinclude:: images/013.php
221223

222224
Rotating Images
223-
---------------
225+
===============
224226

225-
The rotate() method allows you to rotate an image in 90 degree increments::
227+
The ``rotate()`` method allows you to rotate an image in 90 degree increments::
226228

227229
rotate(float $angle)
228230

229-
- **$angle** is the number of degrees to rotate. One of '90', '180', '270'.
231+
- ``$angle`` is the number of degrees to rotate. One of ``90``, ``180``, ``270``.
230232

231-
.. note:: While the $angle parameter accepts a float, it will convert it to an integer during the process.
233+
.. note:: While the ``$angle`` parameter accepts a float, it will convert it to an integer during the process.
232234
If the value is any other than the three values listed above, it will throw a CodeIgniter\Images\ImageException.
233235

234236
Adding a Text Watermark
235-
-----------------------
237+
=======================
236238

237-
You can overlay a text watermark onto the image very simply with the text() method. This is useful for placing copyright
239+
You can overlay a text watermark onto the image very simply with the ``text()`` method. This is useful for placing copyright
238240
notices, photographer names, or simply marking the images as a preview so they won't be used in other people's final
239241
products.
240242

@@ -249,17 +251,17 @@ that allow you to specify how the text should be displayed:
249251

250252
The possible options that are recognized are as follows:
251253

252-
- color Text Color (hex number), i.e., #ff0000
253-
- opacity A number between 0 and 1 that represents the opacity of the text.
254-
- withShadow Boolean value whether to display a shadow or not.
255-
- shadowColor Color of the shadow (hex number)
256-
- shadowOffset How many pixels to offset the shadow. Applies to both the vertical and horizontal values.
257-
- hAlign Horizontal alignment: left, center, right
258-
- vAlign Vertical alignment: top, middle, bottom
259-
- hOffset Additional offset on the x axis, in pixels
260-
- vOffset Additional offset on the y axis, in pixels
261-
- fontPath The full server path to the TTF font you wish to use. System font will be used if none is given.
262-
- fontSize The font size to use. When using the GD handler with the system font, valid values are between 1-5.
254+
- ``color`` Text Color (hex number), i.e., #ff0000
255+
- ``opacity`` A number between 0 and 1 that represents the opacity of the text.
256+
- ``withShadow`` Boolean value whether to display a shadow or not.
257+
- ``shadowColor`` Color of the shadow (hex number)
258+
- ``shadowOffset`` How many pixels to offset the shadow. Applies to both the vertical and horizontal values.
259+
- ``hAlign`` Horizontal alignment: left, center, right
260+
- ``vAlign`` Vertical alignment: top, middle, bottom
261+
- ``hOffset`` Additional offset on the x axis, in pixels
262+
- ``vOffset`` Additional offset on the y axis, in pixels
263+
- ``fontPath`` The full server path to the TTF font you wish to use. System font will be used if none is given.
264+
- ``fontSize`` The font size to use. When using the GD handler with the system font, valid values are between 1-5.
263265

264266
.. note:: The ImageMagick driver does not recognize full server path for fontPath. Instead, simply provide the
265267
name of one of the installed system fonts that you wish to use, i.e., Calibri.
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
$image = \Config\Services::image()
4-
->withFile('/path/to/image/mypic.jpg')
3+
$image->withFile('/path/to/image/mypic.jpg')
54
->fit(100, 100, 'center')
65
->save('/path/to/image/mypic_thumb.jpg');

user_guide_src/source/libraries/images/004.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22

3-
$image = \Config\Services::image()
4-
->withFile('/path/to/image/mypic.jpg')
3+
$image->withFile('/path/to/image/mypic.jpg')
54
->reorient()
65
->rotate(90)
76
->crop(100, 100, 0, 0)
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
$image = \Config\Services::image()
4-
->withFile('/path/to/image/mypic.jpg')
3+
$image->withFile('/path/to/image/mypic.jpg')
54
// processing methods
65
->save('/path/to/image/my_low_quality_pic.jpg', 10);
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
$image = \Config\Services::image()
4-
->withFile('/path/to/image/mypic.jpg')
3+
$image->withFile('/path/to/image/mypic.jpg')
54
->withResource()
65
->save('/path/to/image/my_low_quality_pic.jpg', 10);
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<?php
22

3+
$image = \Config\Services::image();
4+
35
try {
4-
$image = \Config\Services::image()
5-
->withFile('/path/to/image/mypic.jpg')
6+
$image->withFile('/path/to/image/mypic.jpg')
67
->fit(100, 100, 'center')
78
->save('/path/to/image/mypic_thumb.jpg');
8-
} catch (CodeIgniter\Images\ImageException $e) {
9+
} catch (CodeIgniter\Images\Exceptions\ImageException $e) {
910
echo $e->getMessage();
1011
}

0 commit comments

Comments
 (0)