| 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/tour.mitrofanov.ru/public/wp-content/plugins/tour-addons/includes/ |
Upload File : |
<?php
/**
* Add custom image editor (which extends the GD / IMagick editor)
*/
add_filter("wp_image_editors", "my_wp_image_editors");
function my_wp_image_editors($editors) {
array_unshift($editors, "WP_Image_Editor_Custom_GD");
array_unshift($editors, "WP_Image_Editor_Custom_IM");
return $editors;
}
// 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-gd.php";
require_once ABSPATH.WPINC."/class-wp-image-editor-imagick.php";
class WP_Image_Editor_Custom_GD extends WP_Image_Editor_GD {
function multi_resize($sizes) {
$sizes = parent::multi_resize($sizes);
foreach($sizes as $slug => $data)
$sizes[$slug]['file'] = $slug."/".$data['file'];
return $sizes;
}
public function generate_filename($prefix = NULL, $dest_path = NULL, $extension = NULL) {
if(!$prefix)
$prefix = $this->get_suffix();
$info = pathinfo($this->file);
$dir = $info['dirname'];
$ext = $info['extension'];
$sizes = wp_get_registered_image_subsizes();
$dimen = explode('x', $prefix);
foreach($sizes as $name => $size)
{
if ($dimen[0] == $size['width'] || $dimen[1] == $size['height']){
$prefix = $name;
}
}
$name = wp_basename($this->file, ".$ext");
$new_ext = strtolower($extension ? $extension : $ext);
if(!is_null($dest_path) && $_dest_path = realpath($dest_path))
$dir = $_dest_path;
return trailingslashit($dir)."{$prefix}/{$name}.{$new_ext}";
}
}
class WP_Image_Editor_Custom_IM extends WP_Image_Editor_Imagick {
function multi_resize($sizes) {
$sizes = parent::multi_resize($sizes);
foreach($sizes as $slug => $data)
$sizes[$slug]['file'] = $slug."/".$data['file'];
return $sizes;
}
public function generate_filename($prefix = NULL, $dest_path = NULL, $extension = NULL) {
if(!$prefix)
$prefix = $this->get_suffix();
$sizes = wp_get_registered_image_subsizes();
$dimen = explode('x', $prefix);
foreach($sizes as $name => $size)
{
if ($dimen[0] == $size['width'] || $dimen[1] == $size['height']){
$prefix = $name;
}
}
$info = pathinfo($this->file);
$dir = $info['dirname'];
$ext = $info['extension'];
$name = wp_basename($this->file, ".$ext");
$new_ext = strtolower($extension ? $extension : $ext);
if(!is_null($dest_path) && $_dest_path = realpath($dest_path))
$dir = $_dest_path;
return trailingslashit($dir)."{$prefix}/{$name}.{$new_ext}";
}
}
#######################
# IMAGE HOOKS
#######################
function include_slug_in_attachment_size($data, $attachment_id)
{
// Add the size slug to the front of the filename.
foreach($data['sizes'] as $name => $size)
{
$file = $data['sizes'][$name]['file'];
$data['sizes'][$name]['file'] = $name .'/'.$file;
}
return $data;
}
add_filter('wp_get_attachment_metadata', 'include_slug_in_attachment_size', 10, 2);
function shapeSpace_disable_image_sizes($sizes) {
unset($sizes['medium_large']);
unset($sizes['1536x1536']);
unset($sizes['2048x2048']);
return $sizes;
}
add_action('intermediate_image_sizes_advanced', 'shapeSpace_disable_image_sizes');
add_filter('big_image_size_threshold', '__return_false');
function delete_all_attached_media( $post_id ) {
if( get_post_type($post_id) == "post" ) {
if(has_post_thumbnail( $post_id ))
{
$attachment_id = get_post_thumbnail_id( $post_id );
wp_delete_attachment($attachment_id, true);
}
$attachments = get_attached_media( '', $post_id );
foreach ($attachments as $attachment) {
wp_delete_attachment( $attachment->ID, true );
}
}
}
add_action( 'before_delete_post', 'delete_all_attached_media', 10 );
function wpsites_attachment_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;
}
add_action( 'template_redirect', 'wpsites_attachment_redirect' );
function da_create_webp($image_data) {
if( empty($image_data['file']) || empty($image_data['type']) ) {
return $image_data;
}
$valid_types = array('image/png','image/jpeg','image/jpg');
if(!in_array($image_data['type'], $valid_types)) {
return $image_data;
}
$file = $image_data['file'];
$info = pathinfo($file);
$dir = $info['dirname'];
$ext = $info['extension'];
$random_number = md5(rand(10000,99999));
$name = 'Ep-' . $random_number;
$i = 1;
while(file_exists($dir . '/' . $name . '.webp')) {
$i++;
$name = $name . '-' . $i;
}
exec ('mogrify -auto-orient -filter Hamming -define filter:support=2 -thumbnail 1920x1280\> -unsharp 0.25x0.08+8.3+0.045 -dither None -posterize 136 -quality 100 -define jpeg:fancy-upsampling=off -define png:compression-filter=5 -define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude-chunk=all -interlace none -colorspace sRGB -strip -background white -alpha remove -flatten -alpha off '. $file );
if ( $ext == 'png' ) {
exec('cwebp -quiet -q 85 -lossless '.$file.' -o '.$dir . '/' . $name.'.webp ');
} else {
exec('cwebp -quiet -q 85 -m 6 -af -f 50 -sharp_yuv -mt '.$file.' -o '.$dir . '/' . $name.'.webp ');
}
unlink($file);
$upload_dir = wp_upload_dir();
$image_data['file'] = $dir . '/' . $name . '.webp';
$image_data['url'] = $upload_dir['baseurl'] . '/' . $name . '.webp';
$image_data['type'] = 'image/webp';
return $image_data;
}
add_filter( 'wp_handle_upload', 'da_create_webp' );
//SHARPEN THUMBNAILS
function da_image_make_intermediate_size_filter( $filename ){
$size = @getimagesize($filename);
if(!$size)
return new WP_Error('invalid_image', __('Could not read image size'), $filename);
list($orig_w, $orig_h, $orig_type) = $size;
switch($orig_w) {
case $orig_w <= 320:
$unsharp = '0x0.5+1+0';
break;
case $orig_w <= 800:
$unsharp = '0x0.75+0.75+0.008';
break;
case $orig_w <= 9999:
$unsharp = '1.5x1+0.7+0.02';
break;
}
exec ('mogrify -filter Hamming -define filter:support=2 -unsharp ' . $unsharp . ' -quality 85 -interlace none -colorspace sRGB '. $filename );
return $filename;
}
add_filter( 'image_make_intermediate_size', 'da_image_make_intermediate_size_filter' );