| Server IP : 185.252.147.100 / Your IP : 216.73.217.33 Web Server : nginx/1.27.3 System : Linux mitrofanov.ru 6.1.0-37-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.140-1 (2025-05-22) x86_64 User : mitr ( 1000) PHP Version : 8.2.29 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /www/html/metal.mitrofanov.ru/wordpress/wp-content/themes/generatepress_child/inc/ |
Upload File : |
<?php
defined('ABSPATH') || exit;
/**
* Add custom image editor (which extends the GD / IMagick editor)
*/
add_filter("wp_image_editors", "da_wp_imagick_super_hooks");
// Include the existing classes first in order to extend them.
require_once ABSPATH . WPINC . "/class-wp-image-editor.php";
require_once ABSPATH . WPINC . "/class-wp-image-editor-imagick.php";
add_filter('wp_handle_upload', 'da_bmp_to_jpeg');
add_filter('wp_generate_attachment_metadata', 'da_change_image_ext_after_generate', 10, 2);
add_filter('intermediate_image_sizes_advanced', 'da_image_sizes_advanced_filter');
// increase the image size threshold to 1920px
add_filter('big_image_size_threshold', 'da_wp_big_image_size_threshold', 10, 1);
add_filter('jpeg_quality', function ($arg) {return 100;});
// Force adding missing image alt & title for WordPress.
add_filter('wp_get_attachment_image_attributes', 'da_add_img_title', 10, 2);
//attachment link
add_action('template_redirect', 'da_wpsites_attachment_link_redirect');
function da_bmp_to_jpeg($image_data)
{
if (empty($image_data[ 'file' ]) || empty($image_data[ 'type' ]) || $image_data[ 'type' ] != 'image/bmp') {
return $image_data;
}
$file = $image_data[ 'file' ];
$info = pathinfo($file);
$dir = $info[ 'dirname' ];
$ext = $info[ 'extension' ];
$name = $info[ 'filename' ];
$ready = exec('mogrify -interlace line -colorspace sRGB -strip -format jpg ' . $file);
if (! file_exists($dir . '/' . $name . '.jpg')) {
return $image_data;
}
unlink($file);
$upload_dir = wp_upload_dir();
$image_data[ 'file' ] = $dir . '/' . $name . '.jpg';
$image_data[ 'url' ] = $upload_dir[ 'baseurl' ] . '/' . $name . '.jpg';
$image_data[ 'type' ] = 'image/jpeg';
return $image_data;
}
function da_change_image_ext_after_generate($metadata, $attachment_id)
{
$mime_type = get_post_mime_type($attachment_id);
$valid_types = [ 'image/png', 'image/jpeg', 'image/gif' ];
if (! in_array($mime_type, $valid_types)) {
return $metadata;
}
$file_avif = da_change_ext($metadata[ 'file' ], 'avif');
$wp_uploads = wp_upload_dir();
$upload_dir = wp_normalize_path($wp_uploads[ 'basedir' ]);
$upload_dir = untrailingslashit($upload_dir);
if ('.' === dirname($file_avif)) {
$dir_name_path = wp_normalize_path('/');
} else {
$dir_name_path = wp_normalize_path($dir_name_url);
}
$path = $upload_dir . $dir_name_path;
$metadata_avif = $metadata;
if (array_key_exists('original_image', $metadata) && ! empty($metadata[ 'original_image' ])) {
$file_avif = da_change_ext($metadata[ 'original_image' ], 'avif');
$ret = da_create_avif($upload_dir . '/' . $metadata[ 'file' ], $mime_type, $path . wp_basename($file_avif));
if ($ret) {
wp_delete_file($upload_dir . '/' . $metadata[ 'file' ]);
unset($metadata_avif[ 'original_image' ]);
wp_delete_file($upload_dir . '/' . $metadata[ 'original_image' ]);
}
} else {
$ret = da_create_avif($upload_dir . '/' . $metadata[ 'file' ], $mime_type, $path . wp_basename($file_avif));
wp_delete_file($upload_dir . '/' . $metadata[ 'file' ]);
}
foreach ((array) $metadata[ 'sizes' ] as $key => $value) {
$file_thumb = $value[ 'file' ];
$file_thumb_avif = da_change_ext($file_thumb, 'avif');
$ret = da_create_avif($path . $file_thumb, $mime_type, $path . $file_thumb_avif);
if ($ret) {
$metadata_avif[ 'sizes' ][ $key ][ 'file' ] = $file_thumb_avif;
$metadata_avif[ 'sizes' ][ $key ][ 'mime-type' ] = 'image/avif';
$avif_size = filesize($path . wp_basename($file_thumb_avif));
$metadata_avif[ 'sizes' ][ $key ][ 'filesize' ] = $avif_size;
wp_delete_file($path . $file_thumb);
}
}
if ($ret) {
$metadata_avif[ 'file' ] = $file_avif;
$avif_size = filesize($path . wp_basename($file_avif));
$metadata_avif[ 'filesize' ] = $avif_size;
$up_post = [
'ID' => $attachment_id,
'guid' => $upload_url . '/' . $file_avif,
'post_mime_type' => 'image/avif',
];
wp_update_post($up_post);
update_post_meta($attachment_id, '_wp_attached_file', $file_avif);
update_post_meta($attachment_id, '_wp_attachment_metadata', $metadata_avif);
}
return $metadata_avif;
}
function da_image_sizes_advanced_filter($sizes)
{
unset($sizes[ 'medium_large' ]);
unset($sizes[ '1536x1536' ]);
unset($sizes[ '2048x2048' ]);
return $sizes;
}
function da_wp_big_image_size_threshold($threshold)
{
return 1920;
}
function da_add_img_title($attr, $attachment = null)
{
$img_title = trim(strip_tags($attachment->post_title));
$attr[ 'title' ] = the_title_attribute('echo=0');
$attr[ 'alt' ] = the_title_attribute('echo=0');
return $attr;
}
function da_wpsites_attachment_link_redirect()
{
global $post;
if (is_attachment() && isset($post->post_parent) && is_numeric($post->post_parent) && ($post->post_parent != 0)):
wp_redirect(get_permalink($post->post_parent), 301);
exit();
wp_reset_postdata();
endif;
}
// HERE WE CAN RENAME
function da_change_ext($before_file_name, $ext)
{
$exts = explode('.', $before_file_name);
//$before_ext = '.' . end( $exts );
$name = reset($exts);
// $name = uniqid('EPA');
$after_file_name = $name . '.' . $ext;
//$after_ext = '.' . $ext;
//$after_file_name = str_replace( $before_ext, $after_ext, $before_file_name );
return $after_file_name;
}
function da_create_avif($filename, $mime_type, $filename_avif)
{
if (! file_exists($filename)) {
return false;
}
$res = false;
switch ($mime_type) {
case 'image/gif':
$res = exec('ffmpeg -i ' . $filename . ' -strict -1 -f yuv4mpegpipe -pix_fmt yuva444p - | avifenc --stdin ' . $filename_avif);
break;
default:
$res = exec('avifenc -j 2 -d 10 -y 422 --min 0 --max 63 -a end-usage=q -a cq-level=18 -a tune=ssim ' . $filename . ' ' . $filename_avif);
break;
}
if (file_exists($filename_avif)) {
$res = true;
}
return $res;
}
function da_wp_imagick_super_hooks($editors)
{
array_unshift($editors, "WP_Image_Editor_Custom_IM");
return $editors;
}
class WP_Image_Editor_Custom_IM extends WP_Image_Editor_Imagick
{
public function set_color_space($colorSpace = null)
{
if ('image/jpeg' === $this->mime_type) {
try {
$this->image->setColorSpace($colorSpace);
} catch (Exception $e) {
return new WP_Error('image_color_space_error', $e->getMessage());
}
}
return true;
}
protected function thumbnail_image($dst_w, $dst_h, $filter_name = 'FILTER_HAMMING', $strip_meta = true)
{
$filter = Imagick::FILTER_HAMMING;
if (apply_filters('image_strip_meta', $strip_meta)) {
$this->strip_meta();
}
try {
if (is_callable([ $this->image, 'sampleImage' ])) {
$resize_ratio = ($dst_w / $this->size[ 'width' ]) * ($dst_h / $this->size[ 'height' ]);
$sample_factor = 5;
if ($resize_ratio < .111 && ($dst_w * $sample_factor > 128 && $dst_h * $sample_factor > 128)) {
$this->image->sampleImage($dst_w * $sample_factor, $dst_h * $sample_factor);
}
}
if (is_callable([ $this->image, 'resizeImage' ]) && $filter) {
$this->image->setOption('filter:support', '2.0');
$this->image->resizeImage($dst_w, $dst_h, $filter, 1);
} else {
$this->image->scaleImage($dst_w, $dst_h);
}
if ($this->image->getImageColorspace() == \Imagick::COLORSPACE_CMYK) {
$this->image->transformimagecolorspace(\Imagick::COLORSPACE_SRGB);
}
if ('image/jpeg' === $this->mime_type) {
if (is_callable([ $this->image, 'unsharpMaskImage' ])) {
switch ($dst_w) {
case $dst_w <= 250:
$this->image->unsharpMaskImage(0, 0.5, 1, 0);
break;
case $dst_w <= 600:
$this->image->unsharpMaskImage(0, 0.75, 0.75, 0.008);
break;
default:
$this->image->unsharpMaskImage(0.5, 1, 0.5, 0.05);
}
}
$this->image->setOption('jpeg:fancy-upsampling', 'off');
}
if ('image/png' === $this->mime_type) {
$this->image->setOption('png:compression-filter', '5');
$this->image->setOption('png:compression-level', '9');
$this->image->setOption('png:compression-strategy', '1');
$this->image->setOption('png:exclude-chunk', 'all');
}
if (is_callable([ $this->image, 'getImageAlphaChannel' ])
&& is_callable([ $this->image, 'setImageAlphaChannel' ])
&& defined('Imagick::ALPHACHANNEL_UNDEFINED')
&& defined('Imagick::ALPHACHANNEL_OPAQUE')
) {
if ($this->image->getImageAlphaChannel() === Imagick::ALPHACHANNEL_UNDEFINED) {
$this->image->setImageAlphaChannel(Imagick::ALPHACHANNEL_OPAQUE);
}
}
if (is_callable([ $this->image, 'getImageDepth' ]) && is_callable([ $this->image, 'setImageDepth' ])) {
if (8 < $this->image->getImageDepth()) {
$this->image->setImageDepth(8);
}
}
} catch (Exception $e) {
return new WP_Error('image_resize_error', $e->getMessage());
}
}
}