Question

I am developing a podcast theme where i have a form that allows the user to upload their podcast cover arts. The Podcast cover art’s dimension should fall under 1400×1400 and 3000×3000 since we are following iTune’s regulations.

Since such big files will impact the page load speed of the site i wanna resize the uploaded image to a smaller size, say 250×250 so that we can use that to display on our web pages.

I have used WP_Image_Editor::resize function to resize the uploaded image and WP_Image_Editor::save function to save it. It actually worked. I now have two different images with two different dimensions. Since a lot of our user’s have already posted their podcast with the bigger images, i simply cannot exclusively show the compressed version. Instead i wanna check if a compressed version exists or not and show the image accordingly.

I have used PHP’s file_exists function to check for the compressed version of the images. But it seems, it always returns false in the case of compressed images. But i can access it through the browser by typing in the full address.

file_exists('path/to/original_image'); = true
file_exists('path/to/compressed_image'); = false

I have used wp_handle_upload to upload the original image but wp_image_editor class to resize and save the compressed one. So there must be something different with these two functions that’s causing this issue.

I have hosted this on wordpress.com and when i try to access the compressed files (which are inside the upload folder) through FTP i can't see them, but are accessible through browser.

Do you guys have any idea, how i can get over this ? Any help would be greatly appreciated.

Thanks in advance.

EDIT - MORE INFO

This is the code i am using actually,

if ( isset( $_FILES ) && isset( $_FILES['podcast_cover'] ) ) {
    
    // Validating the file extension first,
  
    // And then the Image Mime Type

    $image = wp_get_image_editor( $_FILES['podcast_cover']['tmp_name'] );
    $size = $image->get_size();

    // Validating the size ...
    if ( $size['width'] < 1400 ) {
        global $registration_errors;
        $registration_errors->add( 'podcast_cover', 'The image should be atleast 1400x1400 in size.' );
        return $this->redirect_back_to_form();
    } elseif ($size['width'] > 3000) {
        global $registration_errors;
        $registration_errors->add( 'podcast_cover', 'The image should not be over 3000x3000 in size.' );
        return $this->redirect_back_to_form();
    }

    // Uploading the main one,
    $override = ['test_form' => false];
    $uploaded = wp_handle_upload( $_FILES['podcast_cover'], $override );

    // Now compressing and saving the alternative one
    $pathinfo = pathinfo( $uploaded['url'] );
    $image = wp_get_image_editor( $uploaded['url'] );
    $image->resize( 250, 250 );
    $image->save( $pathinfo['dirname'] . '/' . $pathinfo['filename'] . '-300x300.' . $pathinfo['extension'] );

}

These are the images i am referring to actually

Original Image : https://torontopodcaststudio.com/wp-content/uploads/2020/08/my-podcast-cover.jpg

Compressed Image : https://torontopodcaststudio.com/wp-content/uploads/2020/08/my-podcast-cover-300x300.jpg

ftp screenshot

Was it helpful?

Solution

When I used your exact code on a local test site I was unable to save the resized image. This is because you're passing a URL to wp_get_image_editor() instead of a path. You're also passing a URL to pathinfo(), which also requires a path.

$pathinfo = pathinfo( $uploaded['url'] );
$image = wp_get_image_editor( $uploaded['url'] );

The above lines need to be:

$pathinfo = pathinfo( $uploaded['file'] );
$image = wp_get_image_editor( $uploaded['file'] );

This is because the file key of the array returned by wp_handle_upload() contains the path to the file, which is what each function above requires.

I can't explain why you're able to access the 300x300 version in the browser though. That didn't happen to me when I tested. It could be something specific to the WordPress.com hosting environment.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top