JBZoo\Utils\Image::direction PHP Method

direction() public static method

public static direction ( string $direction ) : string
$direction string
return string
    public static function direction($direction)
    {
        $direction = trim(strtolower($direction));
        if (in_array($direction, array('x', 'y', 'xy', 'yx'), true)) {
            return $direction;
        }
        return 'x';
    }

Usage Example

Beispiel #1
0
 /**
  * Flip an image horizontally or vertically
  *
  * @param mixed  $image GD resource
  * @param string $dir   Direction of fliping - x|y|yx|xy
  * @return resource
  */
 public static function flip($image, $dir)
 {
     $dir = Helper::direction($dir);
     $width = imagesx($image);
     $height = imagesy($image);
     $newImage = imagecreatetruecolor($width, $height);
     Helper::addAlpha($newImage);
     if ($dir === 'y') {
         for ($y = 0; $y < $height; $y++) {
             imagecopy($newImage, $image, 0, $y, 0, $height - $y - 1, $width, 1);
         }
     } elseif ($dir === 'x') {
         for ($x = 0; $x < $width; $x++) {
             imagecopy($newImage, $image, $x, 0, $width - $x - 1, 0, 1, $height);
         }
     } elseif ($dir === 'xy' || $dir === 'yx') {
         $newImage = self::flip($image, 'x');
         $newImage = self::flip($newImage, 'y');
     }
     return $newImage;
 }