Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the local path of a file in wordpress

Tags:

php

wordpress

Since in wordpress, the files/images uploaded are stored in 3 different sizes thus taking up the memory. I have a code that re sizes the image given the URL of that image. The code to resize is:

$img = wp_get_image_editor( $image_url );
if ( ! is_wp_error( $img ) ) {
    $img->resize( 200, 200, false );
    $filename = $img->generate_filename(
        'final', 
        ABSPATH.'wp-content/uploads',
        NULL 
    );
    $img->save($filename);
}

So I want to use this code to resize the image from the local path of the user so that I don't use up too much of my memory. Can anyone tell me how to get the local path and url of the file uploaded by url?

like image 732
wingskush Avatar asked Jan 06 '14 07:01

wingskush


1 Answers

How about this for getting the local path of an image from its URL? :

function ContentUrlToLocalPath($url){
    preg_match('/.*(\/wp\-content\/uploads\/\d+\/\d+\/.*)/', $url, $mat);
    if(count($mat) > 0) return ABSPATH . $mat[1];
    return '';
}

It assumes the file is located in the uploads folder.

like image 137
NoOne Avatar answered Oct 02 '22 03:10

NoOne