| 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_04_11/wp-content/plugins/ |
Upload File : |
<?php
/*
* @wordpress-plugin
* Plugin Name: _ANDYP - Media - Thumbnail Folders
* Plugin URI: http://londonparkour.com
* Description: <strong>Filter</strong> | Adds a prefix folder to all media thumbnails.
* Version: 1.0.0
* Author: Andy Pearson
* Author URI: https://londonparkour.com
*/
// https://wordpress.stackexchange.com/questions/125784/each-custom-image-size-in-custom-upload-directory
/**
* 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";
/**
* If GD Library being used. (default)
*/
class WP_Image_Editor_Custom_GD extends WP_Image_Editor_GD {
/**
* Add the folder prefix before saving to the database and generating
*/
function multi_resize($sizes) {
$sizes = parent::multi_resize($sizes);
foreach($sizes as $slug => $data)
// $sizes[$slug]['file'] = $data['width']."x".$data['height']."/".$data['file']; // creates a /320x300/ folder.
$sizes[$slug]['file'] = $slug."/".$data['file']; // creates a /large/ folder.
return $sizes;
}
/**
* Changes the suffix (300x300) to a directory prefix.
*/
public function generate_filename($prefix = NULL, $dest_path = NULL, $extension = NULL) {
// If empty, generate a prefix with the parent method get_suffix().
if(!$prefix)
$prefix = $this->get_suffix();
// Determine extension and directory based on file path.
$info = pathinfo($this->file);
$dir = $info['dirname'];
$ext = $info['extension'];
// find the slug name from the width.
$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;
}
}
// Determine image name.
$name = wp_basename($this->file, ".$ext");
// Allow extension to be changed via method argument.
$new_ext = strtolower($extension ? $extension : $ext);
// Default to $_dest_path if method argument is not set or invalid.
if(!is_null($dest_path) && $_dest_path = realpath($dest_path))
$dir = $_dest_path;
// Return our new prefixed filename.
return trailingslashit($dir)."{$prefix}/{$name}.{$new_ext}";
}
}
/**
* If Imagemagick is being used.
*/
class WP_Image_Editor_Custom_IM extends WP_Image_Editor_Imagick {
/**
* Add the folder prefix before saving to the database and generating
*/
function multi_resize($sizes) {
$sizes = parent::multi_resize($sizes);
foreach($sizes as $slug => $data)
// $sizes[$slug]['file'] = $data['width']."x".$data['height']."/".$data['file']; // creates a /320x300/ folder.
$sizes[$slug]['file'] = $slug."/".$data['file']; // creates a /large/ folder.
return $sizes;
}
/**
* Changes the suffix (300x300) to a directory prefix.
*/
public function generate_filename($prefix = NULL, $dest_path = NULL, $extension = NULL) {
// If empty, generate a prefix with the parent method get_suffix().
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;
}
}
// Determine extension and directory based on file path.
$info = pathinfo($this->file);
$dir = $info['dirname'];
$ext = $info['extension'];
// Determine image name.
$name = wp_basename($this->file, ".$ext");
// Allow extension to be changed via method argument.
$new_ext = strtolower($extension ? $extension : $ext);
// Default to $_dest_path if method argument is not set or invalid.
if(!is_null($dest_path) && $_dest_path = realpath($dest_path))
$dir = $_dest_path;
// Return our new prefixed filename.
return trailingslashit($dir)."{$prefix}/{$name}.{$new_ext}";
}
}
/**
* We need to rewrite the post attachment metadata so that
* we include the new /slug/ directory. This is so we are able to
* delete / edit the thumbnail correctly.
*
* see function wp_get_attachment_metadata() in wp-include/post.php
*
*/
add_filter('wp_get_attachment_metadata', 'include_slug_in_attachment_size', 10, 2);
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;
}
############################
#
# Image hooks
#
############################
// disable generated image sizes
function shapeSpace_disable_image_sizes($sizes) {
//unset($sizes['thumbnail']); // disable thumbnail size
//unset($sizes['medium']); // disable medium size
//unset($sizes['large']); // disable large size
unset($sizes['medium_large']); // disable medium-large size
unset($sizes['1536x1536']); // disable 2x medium-large size
unset($sizes['2048x2048']); // disable 2x large size
return $sizes;
}
add_action('intermediate_image_sizes_advanced', 'shapeSpace_disable_image_sizes');
// disable scaled image size
add_filter('big_image_size_threshold', '__return_false');
// Force adding missing image alt & title for WordPress.
// 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;
// }
// add_filter( 'wp_get_attachment_image_attributes','da_add_img_title', 10, 2 );
//delete post images
add_action( 'before_delete_post', 'delete_all_attached_media', 10 );
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 );
}
}
}
//attachment link
add_action( 'template_redirect', 'wpsites_attachment_redirect' );
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_filter( 'wp_handle_upload', 'da_create_webp' );
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 = 'Zv-' . $random_number;
$i = 1;
while(file_exists($dir . '/' . $name . '.webp')) {
$i++;
$name = $name . '-' . $i;
}
//exec('convert ' . $file . ' -auto-orient -resize 1920x1280\> -background white -flatten -alpha off ' . $file);
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;
}
//SHARPEN THUMBNAILS
add_filter( 'image_make_intermediate_size', 'da_image_make_intermediate_size_filter' );
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 <= 250:
$unsharp = '0x0.5+1+0';
break;
case $orig_w <= 600:
$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;
}