PHP code example of uginroot / image-resize

1. Go to this page and download the library: Download uginroot/image-resize library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

uginroot / image-resize example snippets


$image = ImageResize::createFromString(file_get_content($path));

$image = ImageResize::createFromPath($path);

$content = file_get_content($path);
$resource = imagecreatefromstring($content);
$image = new ImageCreate($resource, $content);
$image = new ImageCreate($resource); // $image->resetOrientation() not working

ImageCreate::FORMAT_JPEG;
ImageCreate::FORMAT_PNG;
ImageCreate::FORMAT_WEBP;

$image->save($path);
// save(string $path [, int $format = ImageCreate::FORMAT_JPEG [, bool $owerwrite = false [, int $mode = 0666]]])

echo $image->getContent();
// getContent([int $format = ImageCreate::FORMAT_JPEG]);

$image->print();
// $image->print([int $format = ImageCreate::FORMAT_JPEG]);

(string)$image === $image->getContent(ImageCreate::FORMAT_JPEG); // true

$image->copyResource();

$image->getWidth();

$image->getHeight();

$image->resetOrientation();

$image->scale(50);
// scale(int|float 50)

$image->resize(100, 100);
// resize(int $width, int $heihht [, bool $increase = true])

$image->resizeToHeight(100);
// resizeToHeight(int $height [, bool $increase = true])

$image->resizeToWidth(100);
// resizeToWidth(int $width [, bool $increase = true])

$image->resizeToLongSide(100);
// resizeToLongSide(int $side [, $increase = true])

$image->resizeToShortSide(100);
// resizeToShortSide(int $side [, $increase = true])

$image->resizeToBestFit(100, 100);
// resizeToBestFit(int $width, int $height [, $increase = true])

$image->resizeToWorstFit(100, 100);
// resizeToWorstFit(int $width, int $height [, $increase = true])

$image->crop(0, 0, 100, 100);
// crop(int $x, int $y, int $width, int $height)

ImageResize::POSITION_CENTER;
ImageResize::POSITION_TOP;
ImageResize::POSITION_RIGHT;
ImageResize::POSITION_BOTTOM;
ImageResize::POSITION_LEFT;
ImageResize::POSITION_TOP_LEFT;
ImageResize::POSITION_TOP_RIGHT;
ImageResize::POSITION_BOTTOM_LEFT;
ImageResize::POSITION_BOTTOM_RIGHT;

$image->cropPosition(100, 100);
// cropPosition(int $width, int $height [, int $position = ImageResize::POSITION_CENTER])

$image->resizeCover(100, 100);
// resizeCover(int $width, int $height [, int $position = ImageResize::POSITION_CENTER])

$image->resizeContain(100, 100);
// resizeContain(int $width, int $height [, int $position = ImageResize::POSITION_CENTER [, int $color = 0x000000]])

ImageResize::SIDE_TOP;
ImageResize::SIDE_RIGHT;
ImageResize::SIDE_BOTTOM;
ImageResize::SIDE_LEFT;
ImageResize::SIDE_ALL;

$image->cropEdge(50, ImageResize::SIDE_ALL);
// cropEdge(int $cutLength [, int $side = ImageResize::SIDE_ALL])

$image->addBorder(10);
// addBorder(int $borderWidth [, int $side = ImageResize::SIDE_ALL [, int $color = 0x000000]])

ImageResize::FIT_CANCEL; // cancel if wathermark size grate then $image
ImageResize::FIT_RESIZE; // resize wathermak
ImageResize::FIT_AS_IS; // crop if watermark out of bounds

$watermark = ImageResize::createFromPath($path);
$image->setWatermark($watermark);
// setWatermark(ImageResize $watermark [, int $position = ImageResize::POSITION_BOTTOM_RIGHT [, int $padding = 16 [, int $fit = ImageResize::FIT_AS_IS]]]);

$image->change(function(&$resource){
    $resource = imagerotate($resource, 90, 0x000000);
});
// change(callable(&$resource) $callback)