On this page
WP_Image_Editor_GD::_save( resource|GdImage $image, string|null $filename = null, string|null $mime_type = null ): array|WP_Error
Parameters
$imageresource|GdImage Required$filenamestring|null Optional-
Default:
null $mime_typestring|null Optional-
Default:
null
Return
array|WP_Error Array on success or WP_Error if the file failed to save.
pathstringPath to the image file.filestringName of the image file.widthintImage width.heightintImage height.mime-typestringThe mime type of the image.filesizeintFile size of the image.
Source
File: wp-includes/class-wp-image-editor-gd.php. View all references
protected function _save( $image, $filename = null, $mime_type = null ) {
list( $filename, $extension, $mime_type ) = $this->get_output_format( $filename, $mime_type );
if ( ! $filename ) {
$filename = $this->generate_filename( null, null, $extension );
}
if ( 'image/gif' === $mime_type ) {
if ( ! $this->make_image( $filename, 'imagegif', array( $image, $filename ) ) ) {
return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
}
} elseif ( 'image/png' === $mime_type ) {
// Convert from full colors to index colors, like original PNG.
if ( function_exists( 'imageistruecolor' ) && ! imageistruecolor( $image ) ) {
imagetruecolortopalette( $image, false, imagecolorstotal( $image ) );
}
if ( ! $this->make_image( $filename, 'imagepng', array( $image, $filename ) ) ) {
return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
}
} elseif ( 'image/jpeg' === $mime_type ) {
if ( ! $this->make_image( $filename, 'imagejpeg', array( $image, $filename, $this->get_quality() ) ) ) {
return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
}
} elseif ( 'image/webp' == $mime_type ) {
if ( ! function_exists( 'imagewebp' ) || ! $this->make_image( $filename, 'imagewebp', array( $image, $filename, $this->get_quality() ) ) ) {
return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
}
} else {
return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
}
// Set correct file permissions.
$stat = stat( dirname( $filename ) );
$perms = $stat['mode'] & 0000666; // Same permissions as parent folder, strip off the executable bits.
chmod( $filename, $perms );
return array(
'path' => $filename,
/**
* Filters the name of the saved image file.
*
* @since 2.6.0
*
* @param string $filename Name of the file.
*/
'file' => wp_basename( apply_filters( 'image_make_intermediate_size', $filename ) ),
'width' => $this->size['width'],
'height' => $this->size['height'],
'mime-type' => $mime_type,
'filesize' => wp_filesize( $filename ),
);
}
Hooks
- apply_filters( 'image_make_intermediate_size',
string $filename ) -
Filters the name of the saved image file.
Related
Uses
| Uses | Description |
|---|---|
| wp_filesize() wp-includes/functions.php | Wrapper for PHP filesize with filters and casting the result as an integer. |
| WP_Image_Editor_GD::make_image() wp-includes/class-wp-image-editor-gd.php | Either calls editor’s save function or handles file as a stream. |
| __() wp-includes/l10n.php | Retrieves the translation of $text. |
| wp_basename() wp-includes/formatting.php | i18n-friendly version of basename(). |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
| WP_Error::__construct() wp-includes/class-wp-error.php | Initializes the error. |
Used By
| Used By | Description |
|---|---|
| WP_Image_Editor_GD::make_subsize() wp-includes/class-wp-image-editor-gd.php | Create an image sub-size and return the image meta data value for it. |
| WP_Image_Editor_GD::save() wp-includes/class-wp-image-editor-gd.php | Saves current in-memory image to file. |
Changelog
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_image_editor_gd/_save