Created
October 24, 2019 07:54
-
-
Save w-jerome/5835b8a39d47961882cd4371846ec489 to your computer and use it in GitHub Desktop.
WordPress - Create blur image
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
<?php | |
class WPMediaBlur { | |
public function __construct() { | |
$this->add_media_sizes(); | |
add_action( 'updated_post_meta', array( $this, 'post_updated' ), 10, 4 ); | |
add_action( 'added_post_meta', array( $this, 'post_updated' ), 10, 4 ); | |
} | |
public function add_media_sizes() { | |
add_image_size( 'blur', 350 ); | |
} | |
public function post_updated($meta_id, $post_id, $meta_key, $meta_value) { | |
if ('_wp_attachment_metadata' === $meta_key && in_array( $meta_value['sizes']['blur']['mime-type'], array( 'image/jpeg', 'image/png' ) ) ) { | |
$path_image = get_attached_file($post_id); | |
$path_image = ( ! empty( $path_image ) ) ? $path_image : ''; | |
$path_image_blur = dirname($path_image) . DIRECTORY_SEPARATOR . $meta_value['sizes']['blur']['file']; | |
if ( file_exists( $path_image_blur ) ) { | |
$gd_fn_imagecreatefrom = ( 'image/jpeg' === $meta_value['sizes']['blur']['mime-type'] ) ? 'imagecreatefromjpeg' : 'imagecreatefrompng'; | |
$gd_fn_image = ( 'image/jpeg' === $meta_value['sizes']['blur']['mime-type'] ) ? 'imagejpeg' : 'imagepng'; | |
$image = $gd_fn_imagecreatefrom($path_image_blur); | |
for ( $x = 1; $x <= 20; $x++ ) { | |
@imagefilter($image, IMG_FILTER_GAUSSIAN_BLUR); | |
} | |
$gd_fn_image($image, $path_image_blur); | |
@imagedestroy($image); | |
} | |
} | |
} | |
} | |
new WPMediaBlur(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment