wingskush
wingskush

Reputation: 554

How to get the local path of a file in 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?

Upvotes: 7

Views: 15452

Answers (2)

NoOne
NoOne

Reputation: 4091

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.

Upvotes: 8

Dinesh
Dinesh

Reputation: 4110

we could use get_attached_file() for retrieve attached file path based on attachment ID

try:

<?php 
get_attached_file( $attachment_id); 
?>

Upvotes: 6

Related Questions