Skip to content

Commit 1e61396

Browse files
authored
Merge pull request #2113 from MGatner/image-convert
Add Image->convert()
2 parents e1acfb9 + 4986a64 commit 1e61396

4 files changed

Lines changed: 56 additions & 1 deletion

File tree

system/Images/Handlers/BaseHandler.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,22 @@ public function crop(int $width = null, int $height = null, int $x = null, int $
286286

287287
//--------------------------------------------------------------------
288288

289+
/**
290+
* Changes the stored image type to indicate the new file format to use when saving.
291+
* Does not touch the actual resource.
292+
*
293+
* @param integer|null $imageType A PHP imageType constant, e.g. https://www.php.net/manual/en/function.image-type-to-mime-type.php
294+
*
295+
* @return $this
296+
*/
297+
public function convert(int $imageType)
298+
{
299+
$this->image->imageType = $imageType;
300+
return $this;
301+
}
302+
303+
//--------------------------------------------------------------------
304+
289305
/**
290306
* Rotates the image on the current canvas.
291307
*

system/Images/ImageHandlerInterface.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ public function crop(int $width = null, int $height = null, int $x = null, int $
7474

7575
//--------------------------------------------------------------------
7676

77+
/**
78+
* Changes the stored image type to indicate the new file format to use when saving.
79+
* Does not touch the actual resource.
80+
*
81+
* @param integer|null $imageType A PHP imagetype constant, e.g. https://www.php.net/manual/en/function.image-type-to-mime-type.php
82+
*
83+
* @return $this
84+
*/
85+
public function convert(int $imageType);
86+
87+
//--------------------------------------------------------------------
88+
7789
/**
7890
* Rotates the image on the current canvas.
7991
*

tests/system/Images/GDHandlerTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,4 +332,13 @@ public function testImageSave()
332332
}
333333
}
334334

335+
public function testImageConvert()
336+
{
337+
$this->handler->withFile($this->origin . 'ci-logo.jpeg');
338+
$this->handler->getResource(); // make sure resource is loaded
339+
$this->handler->convert(IMAGETYPE_PNG);
340+
$this->handler->save($this->start . 'work/ci-logo.png');
341+
$this->assertEquals(exif_imagetype($this->start . 'work/ci-logo.png'), IMAGETYPE_PNG);
342+
}
343+
335344
}

user_guide_src/source/libraries/images.rst

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,10 @@ starting at the top left corner. The result would be saved as the thumbnail.
9292
Processing Methods
9393
==================
9494

95-
There are six available processing methods:
95+
There are seven available processing methods:
9696

9797
- $image->crop()
98+
- $image->convert()
9899
- $image->fit()
99100
- $image->flatten()
100101
- $image->flip()
@@ -155,6 +156,23 @@ offset values::
155156
->crop(50, 50, $xOffset, $yOffset)
156157
->save('path/to/new/image.jpg');
157158

159+
Converting Images
160+
-----------------
161+
162+
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::
163+
164+
convert(int $imageType)
165+
166+
- **$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)::
167+
168+
Services::image()
169+
->withFile('/path/to/image/mypic.jpg')
170+
->convert(IMAGETYPE_PNG)
171+
->save('path/to/new/image.png');
172+
173+
.. note:: ImageMagick already saves files in the type
174+
indicated by their extension, ignoring **$imageType**
175+
158176
Fitting Images
159177
--------------
160178

0 commit comments

Comments
 (0)