AI Verified

Name

Convertir imagenes ha formato Webp

Language

PHP

Rating

Voted: 0 by 0 user(s)

Codevault

Blushopi

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: 6.4

Our AI bot has checked this snippet is compatable up to wordpress version: 6.1

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

History

Last modified:

15/01/2024

Important Note

This snippet has the following status:

AI Verified

This snippet has been tested by our AI bot, see any comments below.

AI Bot Comments:

Potential vulnerability found : Arbitrary File Deletion
Found on line : 20
Code : unlink( $file_path )
Vulnerable line : 27
Code : $file_path = $upload['file']);

Potential vulnerability found : Arbitrary File Deletion
Found on line : 20
Code : unlink( $file_path )
Vulnerable line : 27
Code : $file_path = $upload['file']);

Found 2 vulnerabilities

Convertir imagenes ha formato Webp

 
                    
1/**
2* Convert Uploaded Images to WebP Format
3*
4* This snippet converts uploaded images (JPEG, PNG, GIF) to WebP format
5* automatically in WordPress. Ideal for use in a theme's functions.php file,
6* or with plugins like Code Snippets or WPCodeBox.
7*
8* @package WordPress_Custom_Functions
9* @author Mark Harris
10* @link www.christchurchwebsolutions.co.uk
11*
12* Usage Instructions:
13* - Add this snippet to your theme's functions.php file, or add it as a new
14* snippet in Code Snippets or WPCodeBox.
15* - The snippet hooks into WordPress's image upload process and converts
16* uploaded images to the WebP format.
17*
18* Optional Configuration:
19* - By default, the original image file is deleted after conversion to WebP.
20* If you prefer to keep the original image file, simply comment out or remove
21* the line '@unlink( $file_path );' in the wpturbo_handle_upload_convert_to_webp function.
22* This will preserve the original uploaded image file alongside the WebP version.
23*/
24add_filter( 'wp_handle_upload', 'wpturbo_handle_upload_convert_to_webp' );
25function wpturbo_handle_upload_convert_to_webp( $upload ) {
26if ( $upload['type'] == 'image/jpeg' || $upload['type'] == 'image/png' || $upload['type'] == 'image/gif' ) {
27$file_path = $upload['file'];
28// Check if ImageMagick or GD is available
29if ( extension_loaded( 'imagick' ) || extension_loaded( 'gd' ) ) {
30$image_editor = wp_get_image_editor( $file_path );
31if ( ! is_wp_error( $image_editor ) ) {
32$file_info = pathinfo( $file_path );
33$dirname = $file_info['dirname'];
34$filename = $file_info['filename'];
35// Create a new file path for the WebP image
36$new_file_path = $dirname . '/' . $filename . '.webp';
37// Attempt to save the image in WebP format
38$saved_image = $image_editor->save( $new_file_path, 'image/webp' );
39if ( ! is_wp_error( $saved_image ) && file_exists( $saved_image['path'] ) ) {
40// Success: replace the uploaded image with the WebP image
41$upload['file'] = $saved_image['path'];
42$upload['url'] = str_replace( basename( $upload['url'] ), basename( $saved_image['path'] ), $upload['url'] );
43$upload['type'] = 'image/webp';
44// Optionally remove the original image
45@unlink( $file_path );
46}
47}
48}
49}
50return $upload;
51}

0

Related Snippets

Please see some snippets below related to this snippet..

General

Unverified

0

Dropdown Button Links

Added: 3 months ago

Last Updated: 3 months ago

General

AI Verified

1

Smooth scroll for GeneratePress anchor links

Added: 2 years ago

Last Updated: 1 year ago

If you're having problems with 'smooth scroll' while using GeneratePress this PHP snippet will sort it out! It will make it smooth instead of just 'jumping' to the anchor link destination

General

AI Verified

0

Migrate BasePress -> BetterDocs: 2. Analytics

Added: 11 months ago

Last Updated: 11 months ago

<p>Use these to migrate data from BasePress to BetterDocs, or clone them and customize to migrate to some other structure.</p> <p>Import all 4 snippets, then run them in order. This will handle docs,...

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

General

AI Verified

0

Convertir imagenes ha formato Webp

Added: 8 months ago

Last Updated: 8 months ago

General

AI Verified

0

Fluen Forms Login

Added: 8 months ago

Last Updated: 8 months ago

General

AI Verified

0

Cerrar sesion - cliente

Added: 8 months ago

Last Updated: 8 months ago