resize, crop, watermark image with php

transform image with php using gregwar image

  • install composer
    curl -sS https://getcomposer.org/installer | php
  • install gregwar/image
    ./composer.phar require gregwar/image
  • create directory img/ and img/thumbnail/
    mkdir -p img/thumbnail/
  • download owl image to directory img/

  • create an img.php file with this content and run it ( from command line or browser )
<?php

require __DIR__ . '/vendor/autoload.php';

use Gregwar\Image\Image;

define('IMG_DIR', 'img/');
define('FONTS_DIR', 'vendor/gregwar/image/Gregwar/Image/demo/fonts/');
define('THUMBNAIL_DIR', IMG_DIR . 'thumbnail/');

$source = 'owl.jpg'; // source image file
$font = 'CaviarDreams.ttf'; // font file
$size = 200; // image size ( square )

// resize
$image = Image::open(IMG_DIR . $source)
        -> resize($size, $size, 'blue')
        -> save(THUMBNAIL_DIR .'owl-resize-'.$size.'x'.$size.'.jpg');

// crop
$image = Image::open(IMG_DIR . $source)
        -> zoomCrop($size, $size, 0, 'left', 'center' )
        -> save(THUMBNAIL_DIR . 'owl-crop-'.$size.'x'.$size.'.jpg');

// negate
$image = Image::open(IMG_DIR . $source)
        -> zoomCrop($size, $size, 0, 'left', 'center' )
        -> negate()
        -> save(THUMBNAIL_DIR . 'owl-negate-'.$size.'x'.$size.'.jpg');

// grayscale
$image = Image::open(IMG_DIR . $source)
        -> zoomCrop($size, $size, 0, 'left', 'center' )
        -> grayscale()
        -> save(THUMBNAIL_DIR . 'owl-grayscale-'.$size.'x'.$size.'.jpg');

// sepia
$image = Image::open(IMG_DIR . $source)
        -> zoomCrop($size, $size, 0, 'left', 'center' )
        -> sepia()
        -> save(THUMBNAIL_DIR . 'owl-sepia-'.$size.'x'.$size.'.jpg');

// export to another format
$image = Image::open(IMG_DIR . $source)
        -> zoomCrop($size, $size, 0, 'left', 'center' )
        -> sepia()
        -> save(THUMBNAIL_DIR . 'owl-sepia-'.$size.'x'.$size.'.png','png');

// edge     
$image = Image::open(IMG_DIR . $source)
        -> zoomCrop($size, $size, 0, 'left', 'center' )
        -> edge()
        -> save(THUMBNAIL_DIR . 'owl-edge-'.$size.'x'.$size.'.jpg');

// brightness
# lighter image
$image = Image::open(IMG_DIR . $source)
        -> zoomCrop($size, $size, 0, 'left', 'center' )
        -> brightness(50)
        -> save(THUMBNAIL_DIR . 'owl-lighter-'.$size.'x'.$size.'.jpg');  
# darker image   
$image = Image::open(IMG_DIR . $source)
        -> zoomCrop($size, $size, 0, 'left', 'center' )
        -> brightness(-50)
        -> save(THUMBNAIL_DIR . 'owl-darker-'.$size.'x'.$size.'.jpg');

// colorize 
$image = Image::open(IMG_DIR . $source)
        -> zoomCrop($size, $size, 0, 'left', 'center' )
        -> colorize(0, 30, 30)
        -> save(THUMBNAIL_DIR . 'owl-colorized-'.$size.'x'.$size.'.jpg');

// watermark
$image = Image::open(IMG_DIR . $source)
        -> zoomCrop($size, $size, 0, 'left', 'center' )
        -> write(
                FONTS_DIR . $font, // font
                '© savingti.me ' . date('Y'),  // text
                81, // x
                31, // y
                15, // size
                -45, // angle
                'black', // color
                'center' // position
            )
        -> write(
                FONTS_DIR . $font, // font
                '© savingti.me ' . date('Y'),  // text
                79, // x
                29, // y
                15, // size
                -45, // angle
                'black', // color
                'center' // position
            )
        -> write(
                FONTS_DIR . $font, // font
                '© savingti.me ' . date('Y'),  // text
                80, // x
                30, // y
                15, // size
                -45, // angle
                'white', // color
                'center' // position
            )
        -> save(THUMBNAIL_DIR . 'owl-watermark-'.$size.'x'.$size.'.jpg');
  • generated image will be output to img/thumbnail/

  • work in symfony too with gregwar/image-bundle