Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
/lib/ -> gdlib.php (source)

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403]

gdlib.php - Collection of routines in Moodle related to processing images using GD

Copyright: 1999 onwards Martin Dougiamas {@link http://moodle.com}
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 477 lines (18 kb)
Included or required:0 times
Referenced: 1 time
Includes or requires: 0 files

Defines 7 functions


Functions that are not part of a class:

imagecopybicubic($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)   X-Ref
Copies a rectangular portion of the source image to another rectangle in the destination image

This function calls imagecopyresampled() if it is available and GD version is 2 at least.
Otherwise it reimplements the same behaviour. See the PHP manual page for more info.

param: resource|\GdImage $dst_img the destination GD image resource
param: resource|\GdImage $src_img the source GD image resource
param: int $dst_x vthe X coordinate of the upper left corner in the destination image
param: int $dst_y the Y coordinate of the upper left corner in the destination image
param: int $src_x the X coordinate of the upper left corner in the source image
param: int $src_y the Y coordinate of the upper left corner in the source image
param: int $dst_w the width of the destination rectangle
param: int $dst_h the height of the destination rectangle
param: int $src_w the width of the source rectangle
param: int $src_h the height of the source rectangle
return: bool tru on success, false otherwise

process_new_icon($context, $component, $filearea, $itemid, $originalfile, $preferpng = false)   X-Ref
Stores optimised icon images in icon file area.

Since 2.9 this function will generate an icon in the same format as the original file when possible.
To counter that behaviour, you can use the argument $preferpng to generate a PNG icon.

param: context $context
param: string $component
param: string filearea
param: int $itemid
param: string $originalfile
param: boolean $preferpng When true, it will try to generate a PNG file regardless of the original file.
return: mixed new unique revision number or false if not saved

resize_image($filepath, $width, $height, $forcecanvas = false)   X-Ref
Resize an image from an image path.

This maintains the aspect ratio of the image.
This will not enlarge the image.

param: string $filepath The full path to the original image file.
param: int|null $width The max width of the resized image, or null to only use the height.
param: int|null $height The max height of the resized image, or null to only use the width.
param: bool $forcecanvas Whether the final dimensions should be set to $width and $height.
return: string|bool False if a problem occurs, else the resized image data.

resize_image_from_image($original, $imageinfo, $width, $height, $forcecanvas = false)   X-Ref
Resize an image from an image object.

param: resource|\GdImage $original The image to work on.
param: array $imageinfo Contains [0] => originalwidth, [1] => originalheight.
param: int|null $width The max width of the resized image, or null to only use the height.
param: int|null $height The max height of the resized image, or null to only use the width.
param: bool $forcecanvas Whether the final dimensions should be set to $width and $height.
return: string|bool False if a problem occurs, else the resized image data.

generate_image_thumbnail($filepath, $width, $height)   X-Ref
Generates a thumbnail for the given image

If the GD library has at least version 2 and PNG support is available, the returned data
is the content of a transparent PNG file containing the thumbnail. Otherwise, the function
returns contents of a JPEG file with black background containing the thumbnail.

param: string $filepath the full path to the original image file
param: int $width the width of the requested thumbnail
param: int $height the height of the requested thumbnail
return: string|bool false if a problem occurs, the thumbnail image data otherwise

generate_image_thumbnail_from_string($filedata, $width, $height)   X-Ref
Generates a thumbnail for the given image string.

If the GD library has at least version 2 and PNG support is available, the returned data
is the content of a transparent PNG file containing the thumbnail. Otherwise, the function
returns contents of a JPEG file with black background containing the thumbnail.

param: string $filedata The image content as a string
param: int $width the width of the requested thumbnail
param: int $height the height of the requested thumbnail
return: string|bool false if a problem occurs, the thumbnail image data otherwise

generate_image_thumbnail_from_image($original, $imageinfo, $width, $height)   X-Ref
Generates a thumbnail for the given image string.

If the GD library has at least version 2 and PNG support is available, the returned data
is the content of a transparent PNG file containing the thumbnail. Otherwise, the function
returns contents of a JPEG file with black background containing the thumbnail.

param: resource $original The image to work on.
param: array $imageinfo Contains [0] => originalwidth, [1] => originalheight.
param: int $width The width of the requested thumbnail.
param: int $height The height of the requested thumbnail.
return: string|bool False if a problem occurs, the thumbnail image data otherwise.