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.../httpdocs/clone/wp-conte.../plugins/wp-smush.../core/modules
File: class-png2jpg.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* PNG to JPG conversion: Png2jpg class
[2] Fix | Delete
*
[3] Fix | Delete
* @package Smush\Core\Modules
[4] Fix | Delete
*
[5] Fix | Delete
* @version 2.4
[6] Fix | Delete
*
[7] Fix | Delete
* @author Umesh Kumar <umesh@incsub.com>
[8] Fix | Delete
*
[9] Fix | Delete
* @copyright (c) 2016, Incsub (http://incsub.com)
[10] Fix | Delete
*/
[11] Fix | Delete
[12] Fix | Delete
namespace Smush\Core\Modules;
[13] Fix | Delete
[14] Fix | Delete
use Exception;
[15] Fix | Delete
use Imagick;
[16] Fix | Delete
use Smush\Core\Helper;
[17] Fix | Delete
use WP_Smush;
[18] Fix | Delete
[19] Fix | Delete
if ( ! defined( 'WPINC' ) ) {
[20] Fix | Delete
die;
[21] Fix | Delete
}
[22] Fix | Delete
[23] Fix | Delete
/**
[24] Fix | Delete
* Class Png2jpg
[25] Fix | Delete
*/
[26] Fix | Delete
class Png2jpg extends Abstract_Module {
[27] Fix | Delete
[28] Fix | Delete
/**
[29] Fix | Delete
* Module slug.
[30] Fix | Delete
*
[31] Fix | Delete
* @var string
[32] Fix | Delete
*/
[33] Fix | Delete
protected $slug = 'png_to_jpg';
[34] Fix | Delete
[35] Fix | Delete
/**
[36] Fix | Delete
* Whether module is pro or not.
[37] Fix | Delete
*
[38] Fix | Delete
* @var string
[39] Fix | Delete
*/
[40] Fix | Delete
protected $is_pro = true;
[41] Fix | Delete
[42] Fix | Delete
/**
[43] Fix | Delete
* Init method.
[44] Fix | Delete
*
[45] Fix | Delete
* @since 3.0
[46] Fix | Delete
*/
[47] Fix | Delete
public function init() {
[48] Fix | Delete
}
[49] Fix | Delete
[50] Fix | Delete
/**
[51] Fix | Delete
* Check if Imagick is available or not
[52] Fix | Delete
*
[53] Fix | Delete
* @return bool True/False Whether Imagick is available or not
[54] Fix | Delete
*/
[55] Fix | Delete
private function supports_imagick() {
[56] Fix | Delete
if ( ! class_exists( '\Imagick' ) ) {
[57] Fix | Delete
return false;
[58] Fix | Delete
}
[59] Fix | Delete
[60] Fix | Delete
return true;
[61] Fix | Delete
}
[62] Fix | Delete
[63] Fix | Delete
/**
[64] Fix | Delete
* Check if GD is loaded
[65] Fix | Delete
*
[66] Fix | Delete
* @return bool True/False Whether GD is available or not
[67] Fix | Delete
*/
[68] Fix | Delete
private function supports_gd() {
[69] Fix | Delete
if ( ! function_exists( 'gd_info' ) ) {
[70] Fix | Delete
return false;
[71] Fix | Delete
}
[72] Fix | Delete
[73] Fix | Delete
return true;
[74] Fix | Delete
}
[75] Fix | Delete
[76] Fix | Delete
/**
[77] Fix | Delete
* Save can be converted status before resizing the image,
[78] Fix | Delete
* because the the image might be lost the undefined-transparent behavior after resizing.
[79] Fix | Delete
*
[80] Fix | Delete
* @see WP_Image_Editor_Imagick::thumbnail_image()
[81] Fix | Delete
* WP Resize function will convert Imagick::ALPHACHANNEL_UNDEFINED -> Imagick::ALPHACHANNEL_OPAQUE.
[82] Fix | Delete
*
[83] Fix | Delete
* @param array $sizes Array of sizes containing max width and height for all the uploaded images.
[84] Fix | Delete
* @param string $file_path Image file path.
[85] Fix | Delete
* @param int $id Image id.
[86] Fix | Delete
*
[87] Fix | Delete
* @return array the Original $sizes.
[88] Fix | Delete
*
[89] Fix | Delete
* @since 3.9.6
[90] Fix | Delete
*/
[91] Fix | Delete
public function cache_can_be_converted_status( $sizes, $file_path, $id ) {
[92] Fix | Delete
// Call can_be_converted and cache the status.
[93] Fix | Delete
$this->can_be_converted( $id, 'full', '', $file_path );
[94] Fix | Delete
// Always return $sizes.
[95] Fix | Delete
return $sizes;
[96] Fix | Delete
}
[97] Fix | Delete
[98] Fix | Delete
/**
[99] Fix | Delete
* Checks if the Given PNG file is transparent or not
[100] Fix | Delete
*
[101] Fix | Delete
* @param string $id Attachment ID.
[102] Fix | Delete
* @param string $file File path for the attachment.
[103] Fix | Delete
*
[104] Fix | Delete
* @return bool|int
[105] Fix | Delete
*/
[106] Fix | Delete
private function is_transparent( $id = '', $file = '' ) {
[107] Fix | Delete
// No attachment id/ file path, return.
[108] Fix | Delete
if ( empty( $id ) && empty( $file ) ) {
[109] Fix | Delete
return false;
[110] Fix | Delete
}
[111] Fix | Delete
[112] Fix | Delete
if ( empty( $file ) ) {
[113] Fix | Delete
// This downloads the file from S3 when S3 is enabled.
[114] Fix | Delete
$file = Helper::get_attached_file( $id );
[115] Fix | Delete
}
[116] Fix | Delete
[117] Fix | Delete
// Check if File exists.
[118] Fix | Delete
if ( empty( $file ) || ! file_exists( $file ) ) {
[119] Fix | Delete
Helper::logger()->png2jpg()->info( sprintf( 'File [%s(%d)] is empty or does not exist.', Helper::clean_file_path( $file ), $id ) );
[120] Fix | Delete
return false;
[121] Fix | Delete
}
[122] Fix | Delete
[123] Fix | Delete
$transparent = '';
[124] Fix | Delete
[125] Fix | Delete
// Try to get transparency using Imagick.
[126] Fix | Delete
if ( $this->supports_imagick() ) {
[127] Fix | Delete
try {
[128] Fix | Delete
$im = new Imagick( $file );
[129] Fix | Delete
[130] Fix | Delete
return $im->getImageAlphaChannel();
[131] Fix | Delete
} catch ( Exception $e ) {
[132] Fix | Delete
Helper::logger()->png2jpg()->error( 'Imagick: Error in checking PNG transparency ' . $e->getMessage() );
[133] Fix | Delete
}
[134] Fix | Delete
} else {
[135] Fix | Delete
// Simple check.
[136] Fix | Delete
// Src: http://camendesign.com/code/uth1_is-png-32bit.
[137] Fix | Delete
if ( ord( file_get_contents( $file, false, null, 25, 1 ) ) & 4 ) {
[138] Fix | Delete
Helper::logger()->png2jpg()->info( sprintf( 'File [%s(%d)] is an PNG 32-bit.', Helper::clean_file_path( $file ), $id ) );
[139] Fix | Delete
return true;
[140] Fix | Delete
}
[141] Fix | Delete
// Src: http://www.jonefox.com/blog/2011/04/15/how-to-detect-transparency-in-png-images/.
[142] Fix | Delete
$contents = file_get_contents( $file );
[143] Fix | Delete
if ( stripos( $contents, 'PLTE' ) !== false && stripos( $contents, 'tRNS' ) !== false ) {
[144] Fix | Delete
Helper::logger()->png2jpg()->info( sprintf( 'File [%s(%d)] is an PNG 8-bit.', Helper::clean_file_path( $file ), $id ) );
[145] Fix | Delete
return true;
[146] Fix | Delete
}
[147] Fix | Delete
[148] Fix | Delete
// If both the conditions failed, that means not transparent.
[149] Fix | Delete
return false;
[150] Fix | Delete
[151] Fix | Delete
}
[152] Fix | Delete
[153] Fix | Delete
// If Imagick is installed, and the code exited due to some error.
[154] Fix | Delete
// Src: StackOverflow.
[155] Fix | Delete
if ( empty( $transparent ) && $this->supports_gd() ) {
[156] Fix | Delete
// Check for transparency using GD.
[157] Fix | Delete
$i = imagecreatefrompng( $file );
[158] Fix | Delete
$palette = ( imagecolortransparent( $i ) < 0 );
[159] Fix | Delete
if ( $palette ) {
[160] Fix | Delete
return true;
[161] Fix | Delete
}
[162] Fix | Delete
}
[163] Fix | Delete
[164] Fix | Delete
return false;
[165] Fix | Delete
}
[166] Fix | Delete
[167] Fix | Delete
/**
[168] Fix | Delete
* Check if given attachment id can be converted to JPEG or not
[169] Fix | Delete
*
[170] Fix | Delete
* @param string $id Atachment ID.
[171] Fix | Delete
* @param string $size Image size.
[172] Fix | Delete
* @param string $mime Mime type.
[173] Fix | Delete
* @param string $file File.
[174] Fix | Delete
*
[175] Fix | Delete
* @since 3.9.6 We removed the private method should_convert
[176] Fix | Delete
* and we also handled the case which we need to delete the download file inside S3.
[177] Fix | Delete
*
[178] Fix | Delete
* Note, we also used this for checking resmush, so we only download the attached file (s3)
[179] Fix | Delete
* if it's necessary (self::is_transparent()). Check the comment on self::__construct() for detail.
[180] Fix | Delete
*
[181] Fix | Delete
* @return bool True/False Can be converted or not
[182] Fix | Delete
*/
[183] Fix | Delete
public function can_be_converted( $id = '', $size = 'full', $mime = '', $file = '' ) {
[184] Fix | Delete
// If PNG2JPG not enabled, or is not smushable, return.
[185] Fix | Delete
if ( ! $this->is_active() || ! Helper::is_smushable( $id ) ) {
[186] Fix | Delete
return false;
[187] Fix | Delete
}
[188] Fix | Delete
// Check it from the cache for full size.
[189] Fix | Delete
if ( 'full' === $size && null !== Helper::cache_get( $id, 'png2jpg_can_be_converted' ) ) {
[190] Fix | Delete
return Helper::cache_get( $id, 'png2jpg_can_be_converted' );
[191] Fix | Delete
}
[192] Fix | Delete
[193] Fix | Delete
// False if not a PNG.
[194] Fix | Delete
$mime = empty( $mime ) ? get_post_mime_type( $id ) : $mime;
[195] Fix | Delete
if ( 'image/png' !== $mime && 'image/x-png' !== $mime ) {
[196] Fix | Delete
return false;
[197] Fix | Delete
}
[198] Fix | Delete
[199] Fix | Delete
// Return if Imagick and GD is not available.
[200] Fix | Delete
if ( ! $this->supports_imagick() && ! $this->supports_gd() ) {
[201] Fix | Delete
Helper::logger()->png2jpg()->warning( 'The site does not support Imagick or GD.' );
[202] Fix | Delete
return false;
[203] Fix | Delete
}
[204] Fix | Delete
[205] Fix | Delete
// If already tried the conversion.
[206] Fix | Delete
if ( get_post_meta( $id, 'wp-smush-pngjpg_savings', true ) ) {
[207] Fix | Delete
Helper::logger()->png2jpg()->info( sprintf( 'File [%s(%d)] already tried the conversion.', Helper::clean_file_path( $file ), $id ) );
[208] Fix | Delete
return false;
[209] Fix | Delete
}
[210] Fix | Delete
[211] Fix | Delete
// Check if registered size is supposed to be converted or not.
[212] Fix | Delete
if ( 'full' !== $size && WP_Smush::get_instance()->core()->mod->smush->skip_image_size( $size ) ) {
[213] Fix | Delete
return false;
[214] Fix | Delete
}
[215] Fix | Delete
[216] Fix | Delete
// Make sure $file is not empty.
[217] Fix | Delete
if ( empty( $file ) ) {
[218] Fix | Delete
$file = Helper::get_attached_file( $id );// S3+.
[219] Fix | Delete
}
[220] Fix | Delete
[221] Fix | Delete
/**
[222] Fix | Delete
* Filter whether to convert the PNG to JPG or not
[223] Fix | Delete
*
[224] Fix | Delete
* @since 2.4
[225] Fix | Delete
*
[226] Fix | Delete
* @param bool $should_convert Current choice for image conversion
[227] Fix | Delete
*
[228] Fix | Delete
* @param int $id Attachment id
[229] Fix | Delete
*
[230] Fix | Delete
* @param string $file File path for the image
[231] Fix | Delete
*
[232] Fix | Delete
* @param string $size Image size being converted
[233] Fix | Delete
*/
[234] Fix | Delete
$should_convert = apply_filters( 'wp_smush_convert_to_jpg', ! $this->is_transparent( $id, $file ), $id, $file, $size );
[235] Fix | Delete
[236] Fix | Delete
if ( 'full' === $size ) {
[237] Fix | Delete
/**
[238] Fix | Delete
* We used this method inside Backup::create_backup(), Smush function, and filter wp_smush_resize_sizes,
[239] Fix | Delete
* so cache the result to avoid to check it again.
[240] Fix | Delete
*/
[241] Fix | Delete
Helper::cache_set( $id, $should_convert, 'png2jpg_can_be_converted' );
[242] Fix | Delete
}
[243] Fix | Delete
[244] Fix | Delete
return $should_convert;
[245] Fix | Delete
}
[246] Fix | Delete
[247] Fix | Delete
/**
[248] Fix | Delete
* Check whether to resmush image or not.
[249] Fix | Delete
*
[250] Fix | Delete
* @since 3.9.6
[251] Fix | Delete
*
[252] Fix | Delete
* @usedby Smush\App\Ajax::scan_images()
[253] Fix | Delete
*
[254] Fix | Delete
* @param bool $should_resmush Current status.
[255] Fix | Delete
* @param int $attachment_id Attachment ID.
[256] Fix | Delete
* @return bool|string png2jpg|TRUE|FALSE
[257] Fix | Delete
*/
[258] Fix | Delete
public function should_resmush( $should_resmush, $attachment_id ) {
[259] Fix | Delete
if ( ! $should_resmush && $this->can_be_converted( $attachment_id ) ) {
[260] Fix | Delete
$should_resmush = 'png2jpg';
[261] Fix | Delete
}
[262] Fix | Delete
[263] Fix | Delete
return $should_resmush;
[264] Fix | Delete
}
[265] Fix | Delete
[266] Fix | Delete
/**
[267] Fix | Delete
* Update the image URL, MIME Type, Attached File, file path in Meta, URL in post content
[268] Fix | Delete
*
[269] Fix | Delete
* @param string $id Attachment ID.
[270] Fix | Delete
* @param string $o_file Original File Path that has to be replaced.
[271] Fix | Delete
* @param string $n_file New File Path which replaces the old file.
[272] Fix | Delete
* @param string $meta Attachment Meta.
[273] Fix | Delete
* @param string $size_k Image Size.
[274] Fix | Delete
* @param string $o_type Operation Type "conversion", "restore".
[275] Fix | Delete
*
[276] Fix | Delete
* @return array Attachment Meta with updated file path.
[277] Fix | Delete
*/
[278] Fix | Delete
public function update_image_path( $id, $o_file, $n_file, $meta, $size_k, $o_type = 'conversion' ) {
[279] Fix | Delete
// Upload Directory.
[280] Fix | Delete
$upload_dir = wp_upload_dir();
[281] Fix | Delete
[282] Fix | Delete
// Upload Path.
[283] Fix | Delete
$upload_path = trailingslashit( $upload_dir['basedir'] );
[284] Fix | Delete
[285] Fix | Delete
$dir_name = pathinfo( $o_file, PATHINFO_DIRNAME );
[286] Fix | Delete
[287] Fix | Delete
// Full Path to new file.
[288] Fix | Delete
$n_file_path = path_join( $dir_name, $n_file );
[289] Fix | Delete
[290] Fix | Delete
// Current URL for image.
[291] Fix | Delete
$o_url = wp_get_attachment_url( $id );
[292] Fix | Delete
[293] Fix | Delete
// Update URL for image size.
[294] Fix | Delete
if ( 'full' !== $size_k ) {
[295] Fix | Delete
$base_url = dirname( $o_url );
[296] Fix | Delete
$o_url = $base_url . '/' . basename( $o_file );
[297] Fix | Delete
}
[298] Fix | Delete
[299] Fix | Delete
// Update File path, Attached File, GUID.
[300] Fix | Delete
$meta = empty( $meta ) ? wp_get_attachment_metadata( $id ) : $meta;
[301] Fix | Delete
$mime = Helper::get_mime_type( $n_file_path );
[302] Fix | Delete
[303] Fix | Delete
/**
[304] Fix | Delete
* If there's no fileinfo extension installed, the mime type will be returned as false.
[305] Fix | Delete
* As a fallback, we set it manually.
[306] Fix | Delete
*
[307] Fix | Delete
* @since 3.8.3
[308] Fix | Delete
*/
[309] Fix | Delete
if ( false === $mime ) {
[310] Fix | Delete
$mime = 'conversion' === $o_type ? 'image/jpeg' : 'image/png';
[311] Fix | Delete
}
[312] Fix | Delete
[313] Fix | Delete
$del_file = true;
[314] Fix | Delete
// Update File Path, Attached file, Mime Type for Image.
[315] Fix | Delete
if ( 'full' === $size_k ) {
[316] Fix | Delete
if ( ! empty( $meta ) ) {
[317] Fix | Delete
$new_file = str_replace( $upload_path, '', $n_file_path );
[318] Fix | Delete
$meta['file'] = $new_file;
[319] Fix | Delete
[320] Fix | Delete
// Update Attached File.
[321] Fix | Delete
if ( ! update_attached_file( $id, $meta['file'] ) ) {
[322] Fix | Delete
$del_file = false;
[323] Fix | Delete
}
[324] Fix | Delete
}
[325] Fix | Delete
[326] Fix | Delete
// Update Mime type.
[327] Fix | Delete
if ( ! wp_update_post(
[328] Fix | Delete
array(
[329] Fix | Delete
'ID' => $id,
[330] Fix | Delete
'post_mime_type' => $mime,
[331] Fix | Delete
)
[332] Fix | Delete
) ) {
[333] Fix | Delete
$del_file = false;
[334] Fix | Delete
}
[335] Fix | Delete
} else {
[336] Fix | Delete
$meta['sizes'][ $size_k ]['file'] = basename( $n_file );
[337] Fix | Delete
$meta['sizes'][ $size_k ]['mime-type'] = $mime;
[338] Fix | Delete
}
[339] Fix | Delete
[340] Fix | Delete
// To be called after the attached file key is updated for the image.
[341] Fix | Delete
if ( ! $this->update_image_url( $id, $size_k, $n_file, $o_url ) ) {
[342] Fix | Delete
$del_file = false;
[343] Fix | Delete
}
[344] Fix | Delete
[345] Fix | Delete
/**
[346] Fix | Delete
* Delete the Original files if backup not enabled
[347] Fix | Delete
* We only delete the file if we don't have any issues while updating the DB.
[348] Fix | Delete
* SMUSH-1088?focusedCommentId=92914.
[349] Fix | Delete
*/
[350] Fix | Delete
if ( $del_file && 'conversion' === $o_type ) {
[351] Fix | Delete
// We might need to backup the full size file, will delete it later if we don't need to use it for backup.
[352] Fix | Delete
if ( 'full' !== $size_k ) {
[353] Fix | Delete
/**
[354] Fix | Delete
* We only need to keep the original file as a backup file.
[355] Fix | Delete
* and try to delete this file on cloud too, e.g S3.
[356] Fix | Delete
*/
[357] Fix | Delete
Helper::delete_permanently( $o_file, $id );
[358] Fix | Delete
}
[359] Fix | Delete
}
[360] Fix | Delete
[361] Fix | Delete
return $meta;
[362] Fix | Delete
}
[363] Fix | Delete
[364] Fix | Delete
/**
[365] Fix | Delete
* Replace the file if there are savings, and return savings
[366] Fix | Delete
*
[367] Fix | Delete
* @param string $file Original File Path.
[368] Fix | Delete
* @param array $result Array structure.
[369] Fix | Delete
* @param string $n_file Updated File path.
[370] Fix | Delete
*
[371] Fix | Delete
* @return array
[372] Fix | Delete
*/
[373] Fix | Delete
private function replace_file( $file = '', $result = array(), $n_file = '' ) {
[374] Fix | Delete
if ( empty( $file ) || empty( $n_file ) ) {
[375] Fix | Delete
return $result;
[376] Fix | Delete
}
[377] Fix | Delete
[378] Fix | Delete
// Get the file size of original image.
[379] Fix | Delete
$o_file_size = filesize( $file );
[380] Fix | Delete
[381] Fix | Delete
$n_file = path_join( dirname( $file ), $n_file );
[382] Fix | Delete
[383] Fix | Delete
$n_file_size = filesize( $n_file );
[384] Fix | Delete
[385] Fix | Delete
// If there aren't any savings return.
[386] Fix | Delete
if ( $n_file_size >= $o_file_size ) {
[387] Fix | Delete
// Delete the JPG image and return.
[388] Fix | Delete
unlink( $n_file );
[389] Fix | Delete
Helper::logger()->png2jpg()->notice( sprintf( 'The new file [%s](%s) is larger than the original file [%s](%s).', Helper::clean_file_path( $n_file ), size_format( $n_file_size ), Helper::clean_file_path( $file ), size_format( $o_file_size ) ) );
[390] Fix | Delete
[391] Fix | Delete
return $result;
[392] Fix | Delete
}
[393] Fix | Delete
[394] Fix | Delete
// Get the savings.
[395] Fix | Delete
$savings = $o_file_size - $n_file_size;
[396] Fix | Delete
[397] Fix | Delete
// Store Stats.
[398] Fix | Delete
$savings = array(
[399] Fix | Delete
'bytes' => $savings,
[400] Fix | Delete
'size_before' => $o_file_size,
[401] Fix | Delete
'size_after' => $n_file_size,
[402] Fix | Delete
);
[403] Fix | Delete
[404] Fix | Delete
$result['savings'] = $savings;
[405] Fix | Delete
[406] Fix | Delete
return $result;
[407] Fix | Delete
}
[408] Fix | Delete
[409] Fix | Delete
/**
[410] Fix | Delete
* Perform the conversion process, using WordPress Image Editor API
[411] Fix | Delete
*
[412] Fix | Delete
* @param string $id Attachment ID.
[413] Fix | Delete
* @param string $file Attachment File path.
[414] Fix | Delete
* @param string $meta Attachment meta.
[415] Fix | Delete
* @param string $size Image size, default empty for full image.
[416] Fix | Delete
*
[417] Fix | Delete
* @return array $result array(
[418] Fix | Delete
* 'meta' => array Update Attachment metadata
[419] Fix | Delete
* 'savings' => Reduction of Image size in bytes
[420] Fix | Delete
* )
[421] Fix | Delete
*/
[422] Fix | Delete
private function convert_to_jpg( $id = '', $file = '', $meta = '', $size = 'full' ) {
[423] Fix | Delete
$result = array(
[424] Fix | Delete
'meta' => $meta,
[425] Fix | Delete
'savings' => '',
[426] Fix | Delete
);
[427] Fix | Delete
[428] Fix | Delete
// Flag: Whether the image was converted or not.
[429] Fix | Delete
if ( 'full' === $size ) {
[430] Fix | Delete
$result['converted'] = false;
[431] Fix | Delete
}
[432] Fix | Delete
[433] Fix | Delete
// If any of the values is not set.
[434] Fix | Delete
if ( empty( $id ) || empty( $file ) || empty( $meta ) || ! file_exists( $file ) ) {
[435] Fix | Delete
Helper::logger()->png2jpg()->info( sprintf( 'Meta file [%s(%d)] is empty or file not found.', Helper::clean_file_path( $file ), $id ) );
[436] Fix | Delete
return $result;
[437] Fix | Delete
}
[438] Fix | Delete
[439] Fix | Delete
$editor = wp_get_image_editor( $file );
[440] Fix | Delete
[441] Fix | Delete
if ( is_wp_error( $editor ) ) {
[442] Fix | Delete
// Use custom method maybe.
[443] Fix | Delete
Helper::logger()->png2jpg()->error( sprintf( 'Image Editor cannot load file [%s(%d)]: %s.', Helper::clean_file_path( $file ), $id, $editor->get_error_message() ) );
[444] Fix | Delete
return $result;
[445] Fix | Delete
}
[446] Fix | Delete
[447] Fix | Delete
$n_file = pathinfo( $file );
[448] Fix | Delete
[449] Fix | Delete
if ( ! empty( $n_file['filename'] ) && $n_file['dirname'] ) {
[450] Fix | Delete
// Get a unique File name.
[451] Fix | Delete
$file_detail = Helper::cache_get( $id, 'convert_to_jpg' );
[452] Fix | Delete
if ( $file_detail ) {
[453] Fix | Delete
list( $old_main_filename, $new_main_filename ) = $file_detail;
[454] Fix | Delete
/**
[455] Fix | Delete
* Thumbnail name.
[456] Fix | Delete
* E.g.
[457] Fix | Delete
* test-150x150.jpg
[458] Fix | Delete
* test-1-150x150.jpg
[459] Fix | Delete
*/
[460] Fix | Delete
if ( $old_main_filename !== $new_main_filename ) {
[461] Fix | Delete
$n_file['filename'] = str_replace( $old_main_filename, $new_main_filename, $n_file['filename'] );
[462] Fix | Delete
}
[463] Fix | Delete
$n_file['filename'] .= '.jpg';
[464] Fix | Delete
} else {
[465] Fix | Delete
$org_filename = $n_file['filename'];
[466] Fix | Delete
/**
[467] Fix | Delete
* Get unique file name for the main file.
[468] Fix | Delete
* E.g.
[469] Fix | Delete
* test.png => test.jpg
[470] Fix | Delete
* test.png => test-1.jpg
[471] Fix | Delete
*/
[472] Fix | Delete
$n_file['filename'] = wp_unique_filename( $n_file['dirname'], $org_filename . '.jpg' );
[473] Fix | Delete
Helper::cache_set( $id, array( $org_filename, pathinfo( $n_file['filename'], PATHINFO_FILENAME ) ), 'convert_to_jpg' );
[474] Fix | Delete
}
[475] Fix | Delete
$n_file = path_join( $n_file['dirname'], $n_file['filename'] );
[476] Fix | Delete
} else {
[477] Fix | Delete
Helper::logger()->png2jpg()->error( sprintf( 'Cannot retrieve the path info of file [%s(%d)].', Helper::clean_file_path( $file ), $id ) );
[478] Fix | Delete
return $result;
[479] Fix | Delete
}
[480] Fix | Delete
[481] Fix | Delete
// Save PNG as JPG.
[482] Fix | Delete
$new_image_info = $editor->save( $n_file, 'image/jpeg' );
[483] Fix | Delete
[484] Fix | Delete
// If image editor was unable to save the image, return.
[485] Fix | Delete
if ( is_wp_error( $new_image_info ) ) {
[486] Fix | Delete
return $result;
[487] Fix | Delete
}
[488] Fix | Delete
[489] Fix | Delete
$n_file = ! empty( $new_image_info ) ? $new_image_info['file'] : '';
[490] Fix | Delete
[491] Fix | Delete
// Replace file, and get savings.
[492] Fix | Delete
$result = $this->replace_file( $file, $result, $n_file );
[493] Fix | Delete
[494] Fix | Delete
if ( ! empty( $result['savings'] ) ) {
[495] Fix | Delete
if ( 'full' === $size ) {
[496] Fix | Delete
$result['converted'] = true;
[497] Fix | Delete
}
[498] Fix | Delete
// Update the File Details. and get updated meta.
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function