download image from remote source and resize then save

后端 未结 2 871
深忆病人
深忆病人 2021-02-02 17:06

Do any of you know of a good php class I can use to download an image from a remote source, re-size it to 120x120 and save it with a file name of my choosing?

So basical

相关标签:
2条回答
  • 2021-02-02 17:52

    You can resize keeping the ratio of image

    $im = imagecreatefromstring($img);
    
    $width_orig = imagesx($im);
    
    $height_orig = imagesy($im);
    
    $width = '800';
    
    $height = '800';
    
    $ratio_orig = $width_orig/$height_orig;
    
    if ($width/$height > $ratio_orig) {
       $width = $height*$ratio_orig;
    } else {
       $height = $width/$ratio_orig;
    }
    
    0 讨论(0)
  • 2021-02-02 18:09

    You can try this:

    <?php    
    $img = file_get_contents('http://www.site.com/image.jpg');
    
    $im = imagecreatefromstring($img);
    
    $width = imagesx($im);
    
    $height = imagesy($im);
    
    $newwidth = '120';
    
    $newheight = '120';
    
    $thumb = imagecreatetruecolor($newwidth, $newheight);
    
    imagecopyresized($thumb, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    
    imagejpeg($thumb,'/images/myChosenName.jpg'); //save image as jpg
    
    imagedestroy($thumb); 
    
    imagedestroy($im);
    ?>
    


    More information about PHP image function : http://www.php.net/manual/en/ref.image.php

    0 讨论(0)
提交回复
热议问题