Resize image in PHP

前端 未结 13 1797
长情又很酷
长情又很酷 2020-11-22 04:18

I\'m wanting to write some PHP code which automatically resizes any image uploaded via a form to 147x147px, but I have no idea how to go about it (I\'m a relative PHP novice

13条回答
  •  無奈伤痛
    2020-11-22 04:39

    You can give a try to TinyPNG PHP library. Using this library your image gets optimized automatically during resizing process. All you need to install the library and get an API key from https://tinypng.com/developers. To install a library, run the below command.

    composer require tinify/tinify
    

    After that, your code is as follows.

    require_once("vendor/autoload.php");
    
    \Tinify\setKey("YOUR_API_KEY");
    
    $source = \Tinify\fromFile("large.jpg"); //image to be resize
    $resized = $source->resize(array(
        "method" => "fit",
        "width" => 150,
        "height" => 100
    ));
    $resized->toFile("thumbnail.jpg"); //resized image
    

    I have a written a blog on the same topic http://artisansweb.net/resize-image-php-using-tinypng

提交回复
热议问题