CodeIgniter’s Image Manipulation class lets you perform the following actions: Show
The following image libraries are supported: GD/GD2, and ImageMagick. Initializing the ClassLike most other classes in CodeIgniter, the image class is initialized in your controller by calling the Services class: <?php $image = \Config\Services::image(); You can pass the alias for the image library you wish to use into the Service function: <?php $image = Config\Services::image('imagick'); The available Handlers are as follows:
If using the ImageMagick library, you must set the path to the library on your server in app/Config/Images.php. Note The ImageMagick handler requires the imagick extension. Processing an ImageRegardless of the type of processing you would like to perform (resizing, cropping, rotation, or watermarking), the general process is identical. You will set some preferences corresponding to the action you intend to perform, then call one of the available processing functions. For example, to create an image thumbnail you’ll do this: <?php $image->withFile('/path/to/image/mypic.jpg') ->fit(100, 100, 'center') ->save('/path/to/image/mypic_thumb.jpg'); The above code tells the library to look for an image called mypic.jpg located in the source_image folder, then create a new image from it that is 100 x 100pixels using the GD2 image_library, and save it to a new file (the thumb). Since it is using the An image can be processed through as many of the available methods as needed before saving. The original image is left untouched, and a new image is used and passed through each method, applying the results on top of the previous results: <?php $image->withFile('/path/to/image/mypic.jpg') ->reorient() ->rotate(90) ->crop(100, 100, 0, 0) ->save('/path/to/image/mypic_thumb.jpg'); This example would take the same image and first fix any mobile phone orientation issues, rotate the image by 90 degrees, and then crop the result into a 100x100 pixel image, starting at the top left corner. The result would be saved as the thumbnail. Note In order for the image class to be allowed to do any processing, the folder containing the image files must have write permissions. Note Image processing can require a considerable amount of server memory for some operations. If you are experiencing out of memory errors while processing images you may need to limit their maximum size, and/or adjust PHP memory limits. Image Quality
<?php $image->withFile('/path/to/image/mypic.jpg') // processing methods ->save('/path/to/image/my_low_quality_pic.jpg', 10); If you are only interested in changing the image quality without doing any processing. You will need to include the image resource or you will end up with an exact copy: <?php $image->withFile('/path/to/image/mypic.jpg') ->withResource() ->save('/path/to/image/my_low_quality_pic.jpg', 10); Processing MethodsThere are seven available processing methods:
These methods return the class instance so they can be chained together, as
shown above. If they fail they will throw a <?php $image = \Config\Services::image(); try { $image->withFile('/path/to/image/mypic.jpg') ->fit(100, 100, 'center') ->save('/path/to/image/mypic_thumb.jpg'); } catch (CodeIgniter\Images\Exceptions\ImageException $e) { echo $e->getMessage(); } Cropping ImagesImages
can be cropped so that only a portion of the original image remains. This is often used when creating thumbnail images that should match a certain size/aspect ratio. This is handled with the crop(int $width = null, int $height = null, int $x = null, int $y = null, bool $maintainRatio = false, string $masterDim = 'auto')
To take a 50x50 pixel square out of the center of an image, you would need to first calculate the appropriate x and y offset values: <?php $info = \Config\Services::image('imagick') ->withFile('/path/to/image/mypic.jpg') ->getFile() ->getProperties(true); $xOffset = ($info['width'] / 2) - 25; $yOffset = ($info['height'] / 2) - 25; \Config\Services::image('imagick') ->withFile('/path/to/image/mypic.jpg') ->crop(50, 50, $xOffset, $yOffset) ->save('/path/to/new/image.jpg'); Converting ImagesThe
Note ImageMagick already saves files in the type indicated by their extension, ignoring Fitting ImagesThe
fit(int $width, int $height = null, string $position = 'center')
This provides a much simpler way to crop that will always maintain the aspect ratio: <?php \Config\Services::image('imagick') ->withFile('/path/to/image/mypic.jpg') ->fit(100, 150, 'left') ->save('/path/to/new/image.jpg'); Flattening ImagesThe
flatten(int $red = 255, int $green = 255, int $blue = 255)
<?php \Config\Services::image('imagick') ->withFile('/path/to/image/mypic.png') ->flatten() ->save('/path/to/new/image.jpg'); \Config\Services::image('imagick') ->withFile('/path/to/image/mypic.png') ->flatten(25, 25, 112) ->save('/path/to/new/image.jpg'); Flipping ImagesImages can be flipped along either their horizontal or vertical axis:
<?php \Config\Services::image('imagick') ->withFile('/path/to/image/mypic.jpg') ->flip('horizontal') ->save('/path/to/new/image.jpg'); Resizing ImagesImages can be resized to fit any dimension you require with the resize(int $width, int $height, bool $maintainRatio = false, string $masterDim = 'auto')
When resizing images you can choose whether to maintain the ratio of the original image, or stretch/squash the new image to fit the desired dimensions. If <?php \Config\Services::image('imagick') ->withFile('/path/to/image/mypic.jpg') ->resize(200, 100, true, 'height') ->save('/path/to/new/image.jpg'); Rotating ImagesThe
Note While the Adding a Text WatermarkYou can overlay a text watermark onto the image very simply with the text(string $text, array $options = []) The first parameter is the string of text that you wish to display. The second parameter is an array of options that allow you to specify how the text should be displayed: <?php \Config\Services::image('imagick') ->withFile('/path/to/image/mypic.jpg') ->text('Copyright 2017 My Photo Co', [ 'color' => '#fff', 'opacity' => 0.5, 'withShadow' => true, 'hAlign' => 'center', 'vAlign' => 'bottom', 'fontSize' => 20, ]) ->save('/path/to/new/image.jpg'); The possible options that are recognized are as follows:
Note The ImageMagick driver does not recognize full server path for fontPath. Instead, simply provide the name of one of the installed system fonts that you wish to use, i.e., Calibri. |