欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

得到远程图像文件图片文件并保存 larvavel get remote image and save Intervention Image: Save image directly from an url with original file name and ext? 有大用


How to get the filename, when taking image from a remote server? And how to save with original size and filename?

// Take remote image
$img = Image::make('http://image.info/demo.jpg');
// how to save in the img/original/demo.jpg
$img->save(????);

I use Intervention, (http://image.intervention.io/api/make) to build CakePHP 3 image Behavior, I want to provide an easy uploading from remote servers, and keep the original image as a source for future manipulation.

EDIT

I ask, is there the Intervention Image method that returns the name of the file, when taken from the remote server. I know php copy(), I can also use the CakePHP File utilities, but it gives me the duplicate request on remote file.




2 Answers  正确答案

response() does not return the original filename like Salines was requesting. This should work

$path = 'https://i.stack.imgur.com/koFpQ.png';
$filename = basename($path);

Image::make($path)->save(public_path('images/' . $filename));

You can easily do it using the make(); and response(); methods. Check my code below.

Just keep in mind that allow_url_fopen must be enabled to fetch a remote image.

This will display my profile picture. If you are not using it with Laravel framework, just replace return with echo.

$img = Image::make('https://i.stack.imgur.com/koFpQ.png');
header('Content-Type: image/png');
return $img->response();

Hope that helps and upvote my reply if useful ;)

If you need further help just ask.

    来自  https://stackoverflow.com/questions/32828606/intervention-image-save-image-directly-from-an-url-with-original-file-name-and




    Storing image file retrieved from external url

    PUBLISHED 1 YEAR AGO BY DESIGNHEROES

    Im retrieving images from an URL and i can't seem to use the build in store() function with the format i get the file in what could i use that works with the store function instead of the usual form posted file?

    Best Answer(As Selected By designheroes)
    jlucia

    Are you storing it on disk? This worked in my tester.

    use Storage;
    
    $url = "<a href="http://www.google.co.in/intl/en_com/images/srpr/logo1w.png" ;"="" target="_blank" rel="noreferrer noopener" style="box-sizing: inherit; margin: 0px; padding: 0px; font-size: 12px; vertical-align: baseline; background: 0px 0px; color: rgb(132, 123, 185); cursor: pointer; text-decoration-line: none; transition: none 86ms ease-out; border: 0px;">http://www.google.co.in/intl/en_com/images/srpr/logo1w.png";
    $contents = file_get_contents($url);
    $name = substr($url, strrpos($url, '/') + 1);
    Storage::put($name, $contents);
    
    jekinney
    jekinney
    1 year ago(212,715 XP)

    No you won't because your request doesn't have a file, it has a string.

    You'll have to send a request to the provided URL to retrieve the image. Hint file_get_contents will help you.

    http://php.net/manual/en/function.file-get-contents.php

    designheroes

    Yea i know i just dont know how to save the "retrieved" file when its not submitted by a form.

    jlucia
    jlucia
    1 year ago(20,850 XP)

    Are you storing it on disk? This worked in my tester.

    use Storage;
    
    $url = "<a href="http://www.google.co.in/intl/en_com/images/srpr/logo1w.png" ;"="" target="_blank" rel="noreferrer noopener" style="box-sizing: inherit; margin: 0px; padding: 0px; font-size: 12px; vertical-align: baseline; background: 0px 0px; color: rgb(132, 123, 185); cursor: pointer; text-decoration-line: none; transition: none 86ms ease-out; border: 0px;">http://www.google.co.in/intl/en_com/images/srpr/logo1w.png";
    $contents = file_get_contents($url);
    $name = substr($url, strrpos($url, '/') + 1);
    Storage::put($name, $contents);
    
    designheroes

    Works perfect, thanks! Didn't know you could use that class :)

    eXist73

    I'm generating Illuminate\Http\UploadedFile from urls, but this helped me get there. Heres my code for anything interested:

    $info = pathinfo($url);
    $contents = file_get_contents($url);
    $file = '/tmp/' . $info['basename'];
    file_put_contents($file, $contents);
    $uploaded_file = new UploadedFile($file, $info['basename']);
    

    Then I can run it through my controller logic the same way as if someone on my site uploaded it.

    kenny11

    congrats

    Please sign in or create an account to participate in this conversation.


    来自  https://laracasts.com/discuss/channels/laravel/storing-image-file-retrieved-from-external-url?page=1



    how to save image from this url via PHP

    I have URLs like this:

    https://strg.global/getImage.php?app=mgm&type=adinda-kennedy&imgId=MGM%20Adinda%20Kennedy%20e6057b8e-aa68-4461-8c9a-1d069fc021a4.jpg&size=1080&cachebreaker=1500536003696`

    When you visit it the image downloads automatically.

    How can I get and save it via PHP?

    I've tried file_get_content(), but it returned info in inappropriate format for saving. I've tried to use Intervention image library and it's method Image::make($url), but it hasn't worked either.

      1 Answer 

      正确答案

      Get the content from the URL, then create an image from the string you get back with (surprisingly) "imagecreatefromstring". After you have the image you can simply display it or save it :

      $Res = file_get_contents($URL);
      $Img = imagecreatefromstring($Res);
      imagepng($Img,"FileName.png");
      imagedestroy($Img);

        来自  https://stackoverflow.com/questions/47291992/how-to-save-image-from-this-url-via-php




        How to get the filename, when taking image from a remote server? And how to save with original size and filename?

        // Take remote image
        $img = Image::make('http://image.info/demo.jpg');
        // how to save in the img/original/demo.jpg
        $img->save(????);

        I use Intervention, (http://image.intervention.io/api/make) to build CakePHP 3 image Behavior, I want to provide an easy uploading from remote servers, and keep the original image as a source for future manipulation.

        EDIT

        I ask, is there the Intervention Image method that returns the name of the file, when taken from the remote server. I know php copy(), I can also use the CakePHP File utilities, but it gives me the duplicate request on remote file.

          2 Answers 

          正确答案

          response() does not return the original filename like Salines was requesting. This should work

          $path = 'https://i.stack.imgur.com/koFpQ.png';
          $filename = basename($path);
          
          Image::make($path)->save(public_path('images/' . $filename));

          You can easily do it using the make(); and response(); methods. Check my code below.

          Just keep in mind that allow_url_fopen must be enabled to fetch a remote image.

          This will display my profile picture. If you are not using it with Laravel framework, just replace return with echo.

          $img = Image::make('https://i.stack.imgur.com/koFpQ.png');
          header('Content-Type: image/png');
          return $img->response();

          Hope that helps and upvote my reply if useful ;)

          If you need further help just ask.

            来自  https://stackoverflow.com/questions/32828606/intervention-image-save-image-directly-from-an-url-with...



            普通分类: