Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../public_h.../wp-inclu...
File: class-wp-image-editor-imagick.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* WordPress Imagick Image Editor
[2] Fix | Delete
*
[3] Fix | Delete
* @package WordPress
[4] Fix | Delete
* @subpackage Image_Editor
[5] Fix | Delete
*/
[6] Fix | Delete
[7] Fix | Delete
/**
[8] Fix | Delete
* WordPress Image Editor Class for Image Manipulation through Imagick PHP Module
[9] Fix | Delete
*
[10] Fix | Delete
* @since 3.5.0
[11] Fix | Delete
*
[12] Fix | Delete
* @see WP_Image_Editor
[13] Fix | Delete
*/
[14] Fix | Delete
class WP_Image_Editor_Imagick extends WP_Image_Editor {
[15] Fix | Delete
/**
[16] Fix | Delete
* Imagick object.
[17] Fix | Delete
*
[18] Fix | Delete
* @var Imagick
[19] Fix | Delete
*/
[20] Fix | Delete
protected $image;
[21] Fix | Delete
[22] Fix | Delete
public function __destruct() {
[23] Fix | Delete
if ( $this->image instanceof Imagick ) {
[24] Fix | Delete
// We don't need the original in memory anymore.
[25] Fix | Delete
$this->image->clear();
[26] Fix | Delete
$this->image->destroy();
[27] Fix | Delete
}
[28] Fix | Delete
}
[29] Fix | Delete
[30] Fix | Delete
/**
[31] Fix | Delete
* Checks to see if current environment supports Imagick.
[32] Fix | Delete
*
[33] Fix | Delete
* We require Imagick 2.2.0 or greater, based on whether the queryFormats()
[34] Fix | Delete
* method can be called statically.
[35] Fix | Delete
*
[36] Fix | Delete
* @since 3.5.0
[37] Fix | Delete
*
[38] Fix | Delete
* @param array $args
[39] Fix | Delete
* @return bool
[40] Fix | Delete
*/
[41] Fix | Delete
public static function test( $args = array() ) {
[42] Fix | Delete
[43] Fix | Delete
// First, test Imagick's extension and classes.
[44] Fix | Delete
if ( ! extension_loaded( 'imagick' ) || ! class_exists( 'Imagick', false ) || ! class_exists( 'ImagickPixel', false ) ) {
[45] Fix | Delete
return false;
[46] Fix | Delete
}
[47] Fix | Delete
[48] Fix | Delete
if ( version_compare( phpversion( 'imagick' ), '2.2.0', '<' ) ) {
[49] Fix | Delete
return false;
[50] Fix | Delete
}
[51] Fix | Delete
[52] Fix | Delete
$required_methods = array(
[53] Fix | Delete
'clear',
[54] Fix | Delete
'destroy',
[55] Fix | Delete
'valid',
[56] Fix | Delete
'getimage',
[57] Fix | Delete
'writeimage',
[58] Fix | Delete
'getimageblob',
[59] Fix | Delete
'getimagegeometry',
[60] Fix | Delete
'getimageformat',
[61] Fix | Delete
'setimageformat',
[62] Fix | Delete
'setimagecompression',
[63] Fix | Delete
'setimagecompressionquality',
[64] Fix | Delete
'setimagepage',
[65] Fix | Delete
'setoption',
[66] Fix | Delete
'scaleimage',
[67] Fix | Delete
'cropimage',
[68] Fix | Delete
'rotateimage',
[69] Fix | Delete
'flipimage',
[70] Fix | Delete
'flopimage',
[71] Fix | Delete
'readimage',
[72] Fix | Delete
'readimageblob',
[73] Fix | Delete
);
[74] Fix | Delete
[75] Fix | Delete
// Now, test for deep requirements within Imagick.
[76] Fix | Delete
if ( ! defined( 'imagick::COMPRESSION_JPEG' ) ) {
[77] Fix | Delete
return false;
[78] Fix | Delete
}
[79] Fix | Delete
[80] Fix | Delete
$class_methods = array_map( 'strtolower', get_class_methods( 'Imagick' ) );
[81] Fix | Delete
if ( array_diff( $required_methods, $class_methods ) ) {
[82] Fix | Delete
return false;
[83] Fix | Delete
}
[84] Fix | Delete
[85] Fix | Delete
return true;
[86] Fix | Delete
}
[87] Fix | Delete
[88] Fix | Delete
/**
[89] Fix | Delete
* Checks to see if editor supports the mime-type specified.
[90] Fix | Delete
*
[91] Fix | Delete
* @since 3.5.0
[92] Fix | Delete
*
[93] Fix | Delete
* @param string $mime_type
[94] Fix | Delete
* @return bool
[95] Fix | Delete
*/
[96] Fix | Delete
public static function supports_mime_type( $mime_type ) {
[97] Fix | Delete
$imagick_extension = strtoupper( self::get_extension( $mime_type ) );
[98] Fix | Delete
[99] Fix | Delete
if ( ! $imagick_extension ) {
[100] Fix | Delete
return false;
[101] Fix | Delete
}
[102] Fix | Delete
[103] Fix | Delete
/*
[104] Fix | Delete
* setIteratorIndex is optional unless mime is an animated format.
[105] Fix | Delete
* Here, we just say no if you are missing it and aren't loading a jpeg.
[106] Fix | Delete
*/
[107] Fix | Delete
if ( ! method_exists( 'Imagick', 'setIteratorIndex' ) && 'image/jpeg' !== $mime_type ) {
[108] Fix | Delete
return false;
[109] Fix | Delete
}
[110] Fix | Delete
[111] Fix | Delete
try {
[112] Fix | Delete
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
[113] Fix | Delete
return ( (bool) @Imagick::queryFormats( $imagick_extension ) );
[114] Fix | Delete
} catch ( Exception $e ) {
[115] Fix | Delete
return false;
[116] Fix | Delete
}
[117] Fix | Delete
}
[118] Fix | Delete
[119] Fix | Delete
/**
[120] Fix | Delete
* Loads image from $this->file into new Imagick Object.
[121] Fix | Delete
*
[122] Fix | Delete
* @since 3.5.0
[123] Fix | Delete
*
[124] Fix | Delete
* @return true|WP_Error True if loaded; WP_Error on failure.
[125] Fix | Delete
*/
[126] Fix | Delete
public function load() {
[127] Fix | Delete
if ( $this->image instanceof Imagick ) {
[128] Fix | Delete
return true;
[129] Fix | Delete
}
[130] Fix | Delete
[131] Fix | Delete
if ( ! is_file( $this->file ) && ! wp_is_stream( $this->file ) ) {
[132] Fix | Delete
return new WP_Error( 'error_loading_image', __( 'File does not exist?' ), $this->file );
[133] Fix | Delete
}
[134] Fix | Delete
[135] Fix | Delete
/*
[136] Fix | Delete
* Even though Imagick uses less PHP memory than GD, set higher limit
[137] Fix | Delete
* for users that have low PHP.ini limits.
[138] Fix | Delete
*/
[139] Fix | Delete
wp_raise_memory_limit( 'image' );
[140] Fix | Delete
[141] Fix | Delete
try {
[142] Fix | Delete
$this->image = new Imagick();
[143] Fix | Delete
$file_extension = strtolower( pathinfo( $this->file, PATHINFO_EXTENSION ) );
[144] Fix | Delete
[145] Fix | Delete
if ( 'pdf' === $file_extension ) {
[146] Fix | Delete
$pdf_loaded = $this->pdf_load_source();
[147] Fix | Delete
[148] Fix | Delete
if ( is_wp_error( $pdf_loaded ) ) {
[149] Fix | Delete
return $pdf_loaded;
[150] Fix | Delete
}
[151] Fix | Delete
} else {
[152] Fix | Delete
if ( wp_is_stream( $this->file ) ) {
[153] Fix | Delete
// Due to reports of issues with streams with `Imagick::readImageFile()`, uses `Imagick::readImageBlob()` instead.
[154] Fix | Delete
$this->image->readImageBlob( file_get_contents( $this->file ), $this->file );
[155] Fix | Delete
} else {
[156] Fix | Delete
$this->image->readImage( $this->file );
[157] Fix | Delete
}
[158] Fix | Delete
}
[159] Fix | Delete
[160] Fix | Delete
if ( ! $this->image->valid() ) {
[161] Fix | Delete
return new WP_Error( 'invalid_image', __( 'File is not an image.' ), $this->file );
[162] Fix | Delete
}
[163] Fix | Delete
[164] Fix | Delete
// Select the first frame to handle animated images properly.
[165] Fix | Delete
if ( is_callable( array( $this->image, 'setIteratorIndex' ) ) ) {
[166] Fix | Delete
$this->image->setIteratorIndex( 0 );
[167] Fix | Delete
}
[168] Fix | Delete
[169] Fix | Delete
if ( 'pdf' === $file_extension ) {
[170] Fix | Delete
$this->remove_pdf_alpha_channel();
[171] Fix | Delete
}
[172] Fix | Delete
[173] Fix | Delete
$this->mime_type = $this->get_mime_type( $this->image->getImageFormat() );
[174] Fix | Delete
} catch ( Exception $e ) {
[175] Fix | Delete
return new WP_Error( 'invalid_image', $e->getMessage(), $this->file );
[176] Fix | Delete
}
[177] Fix | Delete
[178] Fix | Delete
$updated_size = $this->update_size();
[179] Fix | Delete
[180] Fix | Delete
if ( is_wp_error( $updated_size ) ) {
[181] Fix | Delete
return $updated_size;
[182] Fix | Delete
}
[183] Fix | Delete
[184] Fix | Delete
return $this->set_quality();
[185] Fix | Delete
}
[186] Fix | Delete
[187] Fix | Delete
/**
[188] Fix | Delete
* Sets Image Compression quality on a 1-100% scale.
[189] Fix | Delete
*
[190] Fix | Delete
* @since 3.5.0
[191] Fix | Delete
*
[192] Fix | Delete
* @param int $quality Compression Quality. Range: [1,100]
[193] Fix | Delete
* @return true|WP_Error True if set successfully; WP_Error on failure.
[194] Fix | Delete
*/
[195] Fix | Delete
public function set_quality( $quality = null ) {
[196] Fix | Delete
$quality_result = parent::set_quality( $quality );
[197] Fix | Delete
if ( is_wp_error( $quality_result ) ) {
[198] Fix | Delete
return $quality_result;
[199] Fix | Delete
} else {
[200] Fix | Delete
$quality = $this->get_quality();
[201] Fix | Delete
}
[202] Fix | Delete
[203] Fix | Delete
try {
[204] Fix | Delete
switch ( $this->mime_type ) {
[205] Fix | Delete
case 'image/jpeg':
[206] Fix | Delete
$this->image->setImageCompressionQuality( $quality );
[207] Fix | Delete
$this->image->setImageCompression( imagick::COMPRESSION_JPEG );
[208] Fix | Delete
break;
[209] Fix | Delete
case 'image/webp':
[210] Fix | Delete
$webp_info = wp_get_webp_info( $this->file );
[211] Fix | Delete
[212] Fix | Delete
if ( 'lossless' === $webp_info['type'] ) {
[213] Fix | Delete
// Use WebP lossless settings.
[214] Fix | Delete
$this->image->setImageCompressionQuality( 100 );
[215] Fix | Delete
$this->image->setOption( 'webp:lossless', 'true' );
[216] Fix | Delete
} else {
[217] Fix | Delete
$this->image->setImageCompressionQuality( $quality );
[218] Fix | Delete
}
[219] Fix | Delete
break;
[220] Fix | Delete
case 'image/avif':
[221] Fix | Delete
default:
[222] Fix | Delete
$this->image->setImageCompressionQuality( $quality );
[223] Fix | Delete
}
[224] Fix | Delete
} catch ( Exception $e ) {
[225] Fix | Delete
return new WP_Error( 'image_quality_error', $e->getMessage() );
[226] Fix | Delete
}
[227] Fix | Delete
return true;
[228] Fix | Delete
}
[229] Fix | Delete
[230] Fix | Delete
[231] Fix | Delete
/**
[232] Fix | Delete
* Sets or updates current image size.
[233] Fix | Delete
*
[234] Fix | Delete
* @since 3.5.0
[235] Fix | Delete
*
[236] Fix | Delete
* @param int $width
[237] Fix | Delete
* @param int $height
[238] Fix | Delete
* @return true|WP_Error
[239] Fix | Delete
*/
[240] Fix | Delete
protected function update_size( $width = null, $height = null ) {
[241] Fix | Delete
$size = null;
[242] Fix | Delete
if ( ! $width || ! $height ) {
[243] Fix | Delete
try {
[244] Fix | Delete
$size = $this->image->getImageGeometry();
[245] Fix | Delete
} catch ( Exception $e ) {
[246] Fix | Delete
return new WP_Error( 'invalid_image', __( 'Could not read image size.' ), $this->file );
[247] Fix | Delete
}
[248] Fix | Delete
}
[249] Fix | Delete
[250] Fix | Delete
if ( ! $width ) {
[251] Fix | Delete
$width = $size['width'];
[252] Fix | Delete
}
[253] Fix | Delete
[254] Fix | Delete
if ( ! $height ) {
[255] Fix | Delete
$height = $size['height'];
[256] Fix | Delete
}
[257] Fix | Delete
[258] Fix | Delete
/*
[259] Fix | Delete
* If we still don't have the image size, fall back to `wp_getimagesize`. This ensures AVIF images
[260] Fix | Delete
* are properly sized without affecting previous `getImageGeometry` behavior.
[261] Fix | Delete
*/
[262] Fix | Delete
if ( ( ! $width || ! $height ) && 'image/avif' === $this->mime_type ) {
[263] Fix | Delete
$size = wp_getimagesize( $this->file );
[264] Fix | Delete
$width = $size[0];
[265] Fix | Delete
$height = $size[1];
[266] Fix | Delete
}
[267] Fix | Delete
[268] Fix | Delete
return parent::update_size( $width, $height );
[269] Fix | Delete
}
[270] Fix | Delete
[271] Fix | Delete
/**
[272] Fix | Delete
* Sets Imagick time limit.
[273] Fix | Delete
*
[274] Fix | Delete
* Depending on configuration, Imagick processing may take time.
[275] Fix | Delete
*
[276] Fix | Delete
* Multiple problems exist if PHP times out before ImageMagick completed:
[277] Fix | Delete
* 1. Temporary files aren't cleaned by ImageMagick garbage collection.
[278] Fix | Delete
* 2. No clear error is provided.
[279] Fix | Delete
* 3. The cause of such timeout can be hard to pinpoint.
[280] Fix | Delete
*
[281] Fix | Delete
* This function, which is expected to be run before heavy image routines, resolves
[282] Fix | Delete
* point 1 above by aligning Imagick's timeout with PHP's timeout, assuming it is set.
[283] Fix | Delete
*
[284] Fix | Delete
* However seems it introduces more problems than it fixes,
[285] Fix | Delete
* see https://core.trac.wordpress.org/ticket/58202.
[286] Fix | Delete
*
[287] Fix | Delete
* Note:
[288] Fix | Delete
* - Imagick resource exhaustion does not issue catchable exceptions (yet).
[289] Fix | Delete
* See https://github.com/Imagick/imagick/issues/333.
[290] Fix | Delete
* - The resource limit is not saved/restored. It applies to subsequent
[291] Fix | Delete
* image operations within the time of the HTTP request.
[292] Fix | Delete
*
[293] Fix | Delete
* @since 6.2.0
[294] Fix | Delete
* @since 6.3.0 This method was deprecated.
[295] Fix | Delete
*
[296] Fix | Delete
* @return int|null The new limit on success, null on failure.
[297] Fix | Delete
*/
[298] Fix | Delete
public static function set_imagick_time_limit() {
[299] Fix | Delete
_deprecated_function( __METHOD__, '6.3.0' );
[300] Fix | Delete
[301] Fix | Delete
if ( ! defined( 'Imagick::RESOURCETYPE_TIME' ) ) {
[302] Fix | Delete
return null;
[303] Fix | Delete
}
[304] Fix | Delete
[305] Fix | Delete
// Returns PHP_FLOAT_MAX if unset.
[306] Fix | Delete
$imagick_timeout = Imagick::getResourceLimit( Imagick::RESOURCETYPE_TIME );
[307] Fix | Delete
[308] Fix | Delete
// Convert to an integer, keeping in mind that: 0 === (int) PHP_FLOAT_MAX.
[309] Fix | Delete
$imagick_timeout = $imagick_timeout > PHP_INT_MAX ? PHP_INT_MAX : (int) $imagick_timeout;
[310] Fix | Delete
[311] Fix | Delete
$php_timeout = (int) ini_get( 'max_execution_time' );
[312] Fix | Delete
[313] Fix | Delete
if ( $php_timeout > 1 && $php_timeout < $imagick_timeout ) {
[314] Fix | Delete
$limit = (float) 0.8 * $php_timeout;
[315] Fix | Delete
Imagick::setResourceLimit( Imagick::RESOURCETYPE_TIME, $limit );
[316] Fix | Delete
[317] Fix | Delete
return $limit;
[318] Fix | Delete
}
[319] Fix | Delete
}
[320] Fix | Delete
[321] Fix | Delete
/**
[322] Fix | Delete
* Resizes current image.
[323] Fix | Delete
*
[324] Fix | Delete
* At minimum, either a height or width must be provided.
[325] Fix | Delete
* If one of the two is set to null, the resize will
[326] Fix | Delete
* maintain aspect ratio according to the provided dimension.
[327] Fix | Delete
*
[328] Fix | Delete
* @since 3.5.0
[329] Fix | Delete
*
[330] Fix | Delete
* @param int|null $max_w Image width.
[331] Fix | Delete
* @param int|null $max_h Image height.
[332] Fix | Delete
* @param bool|array $crop {
[333] Fix | Delete
* Optional. Image cropping behavior. If false, the image will be scaled (default).
[334] Fix | Delete
* If true, image will be cropped to the specified dimensions using center positions.
[335] Fix | Delete
* If an array, the image will be cropped using the array to specify the crop location:
[336] Fix | Delete
*
[337] Fix | Delete
* @type string $0 The x crop position. Accepts 'left' 'center', or 'right'.
[338] Fix | Delete
* @type string $1 The y crop position. Accepts 'top', 'center', or 'bottom'.
[339] Fix | Delete
* }
[340] Fix | Delete
* @return true|WP_Error
[341] Fix | Delete
*/
[342] Fix | Delete
public function resize( $max_w, $max_h, $crop = false ) {
[343] Fix | Delete
if ( ( $this->size['width'] === $max_w ) && ( $this->size['height'] === $max_h ) ) {
[344] Fix | Delete
return true;
[345] Fix | Delete
}
[346] Fix | Delete
[347] Fix | Delete
$dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop );
[348] Fix | Delete
if ( ! $dims ) {
[349] Fix | Delete
return new WP_Error( 'error_getting_dimensions', __( 'Could not calculate resized image dimensions' ) );
[350] Fix | Delete
}
[351] Fix | Delete
[352] Fix | Delete
list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims;
[353] Fix | Delete
[354] Fix | Delete
if ( $crop ) {
[355] Fix | Delete
return $this->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h );
[356] Fix | Delete
}
[357] Fix | Delete
[358] Fix | Delete
// Execute the resize.
[359] Fix | Delete
$thumb_result = $this->thumbnail_image( $dst_w, $dst_h );
[360] Fix | Delete
if ( is_wp_error( $thumb_result ) ) {
[361] Fix | Delete
return $thumb_result;
[362] Fix | Delete
}
[363] Fix | Delete
[364] Fix | Delete
return $this->update_size( $dst_w, $dst_h );
[365] Fix | Delete
}
[366] Fix | Delete
[367] Fix | Delete
/**
[368] Fix | Delete
* Efficiently resize the current image
[369] Fix | Delete
*
[370] Fix | Delete
* This is a WordPress specific implementation of Imagick::thumbnailImage(),
[371] Fix | Delete
* which resizes an image to given dimensions and removes any associated profiles.
[372] Fix | Delete
*
[373] Fix | Delete
* @since 4.5.0
[374] Fix | Delete
*
[375] Fix | Delete
* @param int $dst_w The destination width.
[376] Fix | Delete
* @param int $dst_h The destination height.
[377] Fix | Delete
* @param string $filter_name Optional. The Imagick filter to use when resizing. Default 'FILTER_TRIANGLE'.
[378] Fix | Delete
* @param bool $strip_meta Optional. Strip all profiles, excluding color profiles, from the image. Default true.
[379] Fix | Delete
* @return void|WP_Error
[380] Fix | Delete
*/
[381] Fix | Delete
protected function thumbnail_image( $dst_w, $dst_h, $filter_name = 'FILTER_TRIANGLE', $strip_meta = true ) {
[382] Fix | Delete
$allowed_filters = array(
[383] Fix | Delete
'FILTER_POINT',
[384] Fix | Delete
'FILTER_BOX',
[385] Fix | Delete
'FILTER_TRIANGLE',
[386] Fix | Delete
'FILTER_HERMITE',
[387] Fix | Delete
'FILTER_HANNING',
[388] Fix | Delete
'FILTER_HAMMING',
[389] Fix | Delete
'FILTER_BLACKMAN',
[390] Fix | Delete
'FILTER_GAUSSIAN',
[391] Fix | Delete
'FILTER_QUADRATIC',
[392] Fix | Delete
'FILTER_CUBIC',
[393] Fix | Delete
'FILTER_CATROM',
[394] Fix | Delete
'FILTER_MITCHELL',
[395] Fix | Delete
'FILTER_LANCZOS',
[396] Fix | Delete
'FILTER_BESSEL',
[397] Fix | Delete
'FILTER_SINC',
[398] Fix | Delete
);
[399] Fix | Delete
[400] Fix | Delete
/**
[401] Fix | Delete
* Set the filter value if '$filter_name' name is in the allowed list and the related
[402] Fix | Delete
* Imagick constant is defined or fall back to the default filter.
[403] Fix | Delete
*/
[404] Fix | Delete
if ( in_array( $filter_name, $allowed_filters, true ) && defined( 'Imagick::' . $filter_name ) ) {
[405] Fix | Delete
$filter = constant( 'Imagick::' . $filter_name );
[406] Fix | Delete
} else {
[407] Fix | Delete
$filter = defined( 'Imagick::FILTER_TRIANGLE' ) ? Imagick::FILTER_TRIANGLE : false;
[408] Fix | Delete
}
[409] Fix | Delete
[410] Fix | Delete
/**
[411] Fix | Delete
* Filters whether to strip metadata from images when they're resized.
[412] Fix | Delete
*
[413] Fix | Delete
* This filter only applies when resizing using the Imagick editor since GD
[414] Fix | Delete
* always strips profiles by default.
[415] Fix | Delete
*
[416] Fix | Delete
* @since 4.5.0
[417] Fix | Delete
*
[418] Fix | Delete
* @param bool $strip_meta Whether to strip image metadata during resizing. Default true.
[419] Fix | Delete
*/
[420] Fix | Delete
if ( apply_filters( 'image_strip_meta', $strip_meta ) ) {
[421] Fix | Delete
$this->strip_meta(); // Fail silently if not supported.
[422] Fix | Delete
}
[423] Fix | Delete
[424] Fix | Delete
try {
[425] Fix | Delete
/*
[426] Fix | Delete
* To be more efficient, resample large images to 5x the destination size before resizing
[427] Fix | Delete
* whenever the output size is less that 1/3 of the original image size (1/3^2 ~= .111),
[428] Fix | Delete
* unless we would be resampling to a scale smaller than 128x128.
[429] Fix | Delete
*/
[430] Fix | Delete
if ( is_callable( array( $this->image, 'sampleImage' ) ) ) {
[431] Fix | Delete
$resize_ratio = ( $dst_w / $this->size['width'] ) * ( $dst_h / $this->size['height'] );
[432] Fix | Delete
$sample_factor = 5;
[433] Fix | Delete
[434] Fix | Delete
if ( $resize_ratio < .111 && ( $dst_w * $sample_factor > 128 && $dst_h * $sample_factor > 128 ) ) {
[435] Fix | Delete
$this->image->sampleImage( $dst_w * $sample_factor, $dst_h * $sample_factor );
[436] Fix | Delete
}
[437] Fix | Delete
}
[438] Fix | Delete
[439] Fix | Delete
/*
[440] Fix | Delete
* Use resizeImage() when it's available and a valid filter value is set.
[441] Fix | Delete
* Otherwise, fall back to the scaleImage() method for resizing, which
[442] Fix | Delete
* results in better image quality over resizeImage() with default filter
[443] Fix | Delete
* settings and retains backward compatibility with pre 4.5 functionality.
[444] Fix | Delete
*/
[445] Fix | Delete
if ( is_callable( array( $this->image, 'resizeImage' ) ) && $filter ) {
[446] Fix | Delete
$this->image->setOption( 'filter:support', '2.0' );
[447] Fix | Delete
$this->image->resizeImage( $dst_w, $dst_h, $filter, 1 );
[448] Fix | Delete
} else {
[449] Fix | Delete
$this->image->scaleImage( $dst_w, $dst_h );
[450] Fix | Delete
}
[451] Fix | Delete
[452] Fix | Delete
// Set appropriate quality settings after resizing.
[453] Fix | Delete
if ( 'image/jpeg' === $this->mime_type ) {
[454] Fix | Delete
if ( is_callable( array( $this->image, 'unsharpMaskImage' ) ) ) {
[455] Fix | Delete
$this->image->unsharpMaskImage( 0.25, 0.25, 8, 0.065 );
[456] Fix | Delete
}
[457] Fix | Delete
[458] Fix | Delete
$this->image->setOption( 'jpeg:fancy-upsampling', 'off' );
[459] Fix | Delete
}
[460] Fix | Delete
[461] Fix | Delete
if ( 'image/png' === $this->mime_type ) {
[462] Fix | Delete
$this->image->setOption( 'png:compression-filter', '5' );
[463] Fix | Delete
$this->image->setOption( 'png:compression-level', '9' );
[464] Fix | Delete
$this->image->setOption( 'png:compression-strategy', '1' );
[465] Fix | Delete
$this->image->setOption( 'png:exclude-chunk', 'all' );
[466] Fix | Delete
}
[467] Fix | Delete
[468] Fix | Delete
/*
[469] Fix | Delete
* If alpha channel is not defined, set it opaque.
[470] Fix | Delete
*
[471] Fix | Delete
* Note that Imagick::getImageAlphaChannel() is only available if Imagick
[472] Fix | Delete
* has been compiled against ImageMagick version 6.4.0 or newer.
[473] Fix | Delete
*/
[474] Fix | Delete
if ( is_callable( array( $this->image, 'getImageAlphaChannel' ) )
[475] Fix | Delete
&& is_callable( array( $this->image, 'setImageAlphaChannel' ) )
[476] Fix | Delete
&& defined( 'Imagick::ALPHACHANNEL_UNDEFINED' )
[477] Fix | Delete
&& defined( 'Imagick::ALPHACHANNEL_OPAQUE' )
[478] Fix | Delete
) {
[479] Fix | Delete
if ( $this->image->getImageAlphaChannel() === Imagick::ALPHACHANNEL_UNDEFINED ) {
[480] Fix | Delete
$this->image->setImageAlphaChannel( Imagick::ALPHACHANNEL_OPAQUE );
[481] Fix | Delete
}
[482] Fix | Delete
}
[483] Fix | Delete
[484] Fix | Delete
// Limit the bit depth of resized images to 8 bits per channel.
[485] Fix | Delete
if ( is_callable( array( $this->image, 'getImageDepth' ) ) && is_callable( array( $this->image, 'setImageDepth' ) ) ) {
[486] Fix | Delete
if ( 8 < $this->image->getImageDepth() ) {
[487] Fix | Delete
$this->image->setImageDepth( 8 );
[488] Fix | Delete
}
[489] Fix | Delete
}
[490] Fix | Delete
} catch ( Exception $e ) {
[491] Fix | Delete
return new WP_Error( 'image_resize_error', $e->getMessage() );
[492] Fix | Delete
}
[493] Fix | Delete
}
[494] Fix | Delete
[495] Fix | Delete
/**
[496] Fix | Delete
* Create multiple smaller images from a single source.
[497] Fix | Delete
*
[498] Fix | Delete
* Attempts to create all sub-sizes and returns the meta data at the end. This
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function