Last active
November 11, 2019 13:33
-
-
Save mohamedsalehamin/4b44a618897a61d68e17184857c04248 to your computer and use it in GitHub Desktop.
fix convert Arabic images names to webp through webp-express
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// wp-content/plugins/webp-express/lib/classes/Convert.php | |
public static function convertFile($source, $config = null, $convertOptions = null, $converter = null) | |
{ | |
try { | |
// fix arabic file names | |
$oldPath = pathinfo($source); | |
$basename = $oldPath['basename']; | |
copy($source, $oldPath['dirname'] . '/imageCopy.' . $oldPath['extension']); | |
$newPath = $oldPath['dirname'] . '/imageCopy.' . $oldPath['extension']; | |
// Check source | |
// --------------- | |
$checking = 'source path'; | |
$source = SanityCheck::absPathExistsAndIsFile($source); | |
//$filename = SanityCheck::absPathExistsAndIsFileInDocRoot($source); | |
// PS: No need to check mime type as the WebPConvert library does that (it only accepts image/jpeg and image/png) | |
// Check that source is within a valid image root | |
$activeRootIds = Paths::getImageRootIds(); // Currently, root ids cannot be selected, so all root ids are active. | |
$rootId = Paths::findImageRootOfPath($source, $activeRootIds); | |
if ($rootId === false) { | |
throw new \Exception('Path of source is not within a valid image root'); | |
} | |
// Check config | |
// -------------- | |
$checking = 'configuration file'; | |
if (is_null($config)) { | |
$config = Config::loadConfigAndFix(); // ps: if this fails to load, default config is returned. | |
} | |
if (!is_array($config)) { | |
throw new SanityException('file is corrupt'); | |
} | |
// Check convert options | |
// ------------------------------- | |
$checking = 'configuration file (options)'; | |
if (is_null($convertOptions)) { | |
$wodOptions = Config::generateWodOptionsFromConfigObj($config); | |
if (!isset($wodOptions['webp-convert']['convert'])) { | |
throw new SanityException('conversion options are missing'); | |
} | |
$convertOptions = $wodOptions['webp-convert']['convert']; | |
} | |
if (!is_array($convertOptions)) { | |
throw new SanityException('conversion options are missing'); | |
} | |
// Check destination | |
// ------------------------------- | |
$checking = 'destination'; | |
$destination = self::getDestination($newPath, $config); | |
$destination = SanityCheck::absPath($destination); | |
// Check log dir | |
// ------------------------------- | |
$checking = 'conversion log dir'; | |
$logDir = SanityCheck::absPath(Paths::getWebPExpressContentDirAbs() . '/log'); | |
} catch (\Exception $e) { | |
return [ | |
'success' => false, | |
'msg' => 'Check failed for ' . $checking . ': ' . $e->getMessage(), | |
'log' => '', | |
]; | |
} | |
// Done with sanitizing, lets get to work! | |
// --------------------------------------- | |
//return false; | |
$result = ConvertHelperIndependent::convert($newPath, $destination, $convertOptions, $logDir, $converter); | |
//error_log('looki:' . $source . $converter); | |
// If we are using stack converter, check if Ewww discovered invalid api key | |
//if (is_null($converter)) { | |
if (isset(Ewww::$nonFunctionalApiKeysDiscoveredDuringConversion)) { | |
// We got an invalid or exceeded api key (at least one). | |
//error_log('look:' . print_r(Ewww::$nonFunctionalApiKeysDiscoveredDuringConversion, true)); | |
EwwwTools::markApiKeysAsNonFunctional( | |
Ewww::$nonFunctionalApiKeysDiscoveredDuringConversion, | |
Paths::getConfigDirAbs() | |
); | |
} | |
//} | |
if ($result['success'] === true) { | |
$result['filesize-original'] = @filesize($source); | |
$result['filesize-webp'] = @filesize($destination); | |
$result['destination-path'] = $destination; | |
$rootOfDestination = Paths::destinationRoot($rootId, $config['destination-folder'], $config['destination-structure']); | |
$relPathFromImageRootToSource = PathHelper::getRelDir( | |
realpath(Paths::getAbsDirById($rootId)), | |
realpath($source) | |
); | |
$relPathFromImageRootToDest = ConvertHelperIndependent::appendOrSetExtension( | |
$relPathFromImageRootToSource, | |
$config['destination-folder'], | |
$config['destination-extension'], | |
($rootId == 'uploads') | |
); | |
$result['destination-url'] = $rootOfDestination['url'] . '/' . $relPathFromImageRootToDest; | |
} | |
$newName = end(explode('/',$relPathFromImageRootToDest)); | |
// fix arabic file names | |
$destination = pathinfo($destination); | |
$old_file = $destination['dirname'] . '/imageCopy.' . $oldPath['extension'] . '.webp'; | |
$new_file = $destination['dirname'] . '/' . $newName; | |
rename($old_file, $new_file); | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment