Access our premium support and let us know your problems, we will help you solve them.

0
No products in the cart.
  • This topic is empty.
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #9927
    blankjohnitbonuc
    Participant

    I have a small question; In PHP I have used curl to get data from an URL:

    $url = "http://www.prelovac.com/vladimir/wp-content/uploads/2008/03/example.jpg";
    

    With that I use curl_getinfo() which gave me an array:

    Array
    (
    [url] => http://www.prelovac.com/vladimir/wp-content/uploads/2008/03/example.jpg
    [content_type] => image/jpeg
    [http_code] => 200
    [header_size] => 496
    [request_size] => 300
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 2.735
    [namelookup_time] => 0.063
    [connect_time] => 0.063
    [pretransfer_time] => 0.063
    [size_upload] => 0
    [size_download] => 34739
    [speed_download] => 12701
    [speed_upload] => 0
    [download_content_length] => 34739
    [upload_content_length] => -1
    [starttransfer_time] => 1.282
    [redirect_time] => 0
    )
    

    How can I get the name of the image in the link [url] => http://www.prelovac.com/vladimir/wp-content/uploads/2008/03/example.jpg such as

    [image_name] : example
    [image_ex] : jpg
    

    Thanks for any suggestion!

    #9935
    blankmarkel-mairs
    Participant
    #9932
    blankcheck123
    Participant
    $url_arr = explode ('/', $arr['url']);
    $ct = count($url_arr);
    $name = $url_arr[$ct-1];
    $name_div = explode('.', $name);
    $ct_dot = count($name_div);
    $img_type = $name_div[$ct_dot -1];
    
    echo $name . "  " . $img_type;
    
    #9931
    blanksunny
    Participant
    $URL = urldecode('http://www.greenbiz.com/sites/default/files/imagecache/wide_large/Woman_HORIZ.jpg?sunny=20$mal+1');
    $image_name = (stristr($URL,'?',true))?stristr($URL,'?',true):$URL;
    $pos = strrpos($image_name,'/');
    $image_name = substr($image_name,$pos+1);
    $extension = stristr($image_name,'.');
    if($extension == '.jpg' || $extension == '.png' || $extension == '.gif' || $extension == '.jpeg'){`enter code here`
    print $image_name;
    }
    
    #9928
    blankkhandad-niazi
    Participant
    $imagePath = 'http://www.prelovac.com/vladimir/wp-content/uploads/2008/03/example.jpg';
    
    $imageName = get_basename($imagePath);
    
    function get_basename($filename)
    {
        return preg_replace('/^.+[\\\\\\/]/', '', $imagePath);
    }
    
    #9933
    blankravi-mane
    Participant

    consider the following is the image path
    $image_url=’http://development/rwc/wp-content/themes/Irvine/images/attorney1.png‘;
    from this url to get image name with extention use following function basename();
    look following code

    code:

    $image_url='http://development/rwc/wp-content/themes/Irvine/images/attorney1.png';
    echo basename($image_url);
    

    output:
    attorney1.png

    #9934
    blanksinha
    Participant

    Sometime url has additional parameters appended to it. In such scenario we can remove the parameter section first and then we can use the PHP’s inbuilt pathinfo() function to get the image name from the url.

    $url = 'http://images.fitnessmagazine.mdpcdn.com/sites/story/shutterstock_65560759.jpg?itok=b8HiA95H';
    

    Check if the image url has parameters appended.

    if (strpos($url, '?') !== false) {
        $t = explode('?',$url);
        $url = $t[0];            
    }      
    

    The resulting url variable now contain

    http://images.fitnessmagazine.mdpcdn.com/sites/story/shutterstock_65560759.jpg

    Use the pathinfo() to retrieve the desired details.

    $pathinfo = pathinfo($url);
    echo $pathinfo['filename'].'.'.$pathinfo['extension'];
    

    This will give shutterstock_65560759.jpg as output.

    #9929
    blankyaroslawww
    Participant

    You can use regex /(?:.+\/)(.+\.(png|jpg|jepg))[?#]?.*$/

    Example:

    $examples = [
        'http://images.fitnessmagazine.mdpcdn.com/sites/story/with_query.jpg?itok=b8HiA95H',
        'http://images.fitnessmagazine.mdpcdn.com/sites/story/with_hash.jpg#hghgh',
        'http://images.fitnessmagazine.mdpcdn.com/sites/story/with_query.jpg?',
        'http://images.fitnessmagazine.mdpcdn.com/sites/story/with_hash.jpg#',
        'http://images.fitnessmagazine.mdpcdn.com/sites/story/with_multydots.65560759.jpg',
        'http://images.fitnessmagazine.mdpcdn.com/sites/story/image.png',
        'http://images.fitnessmagazine.mdpcdn.com/sites/story/without_ext',   
        ];
    
    foreach($examples as $example) {
        preg_match('/(?:.+\/)(.+\.(png|jpg|jepg))[?#]?.*$/', $example, $matches);
        
        if(isset($matches[1]) && $matches[1]) {
         echo "Url: {$example}, image name: {$matches[1]} \n";   
        } else {
            echo "Url: {$example} is not image url \n";   
        }
    }
    
    
    

    Printed:

    Url: http://images.fitnessmagazine.mdpcdn.com/sites/story/with_query.jpg?itok=b8HiA95H, image name: with_query.jpg 
    Url: http://images.fitnessmagazine.mdpcdn.com/sites/story/with_hash.jpg#hghgh, image name: with_hash.jpg 
    Url: http://images.fitnessmagazine.mdpcdn.com/sites/story/with_query.jpg?, image name: with_query.jpg 
    Url: http://images.fitnessmagazine.mdpcdn.com/sites/story/with_hash.jpg#, image name: with_hash.jpg 
    Url: http://images.fitnessmagazine.mdpcdn.com/sites/story/with_multydots.65560759.jpg, image name: with_multydots.65560759.jpg 
    Url: http://images.fitnessmagazine.mdpcdn.com/sites/story/image.png, image name: image.png 
    Url: http://images.fitnessmagazine.mdpcdn.com/sites/story/without_ext is not image url 
    
    #9930
    blankhett
    Participant

    If you wanna to get file name from url ignoring uri params, you can try this:

    $url = 'http://host/filename.ext?params';
    $parsedUrl = parse_url($url);
    $pathInfo = pathinfo($parsedUrl['path']);
    print_r($pathInfo);
    Array
    (
        [dirname] => /
        [basename] => filename.ext
        [extension] => ext
        [filename] => filename
    )
    
    
Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.