RSI helpers  0.1
RSI helpers
Image.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi;
4 
5 /**
6  * Image helpers.
7  */
8 class Image{
9 
10  const IMAGE_SCALE_INSIDE = 1; //!< Scale image inside box (normally it will scale to fit at least the box).
11  const IMAGE_SCALE_ONLY_SMALLER = 2; //!< Only make an image smaller, not larger.
12 
13  /**
14  * Create an image resource from file.
15  * @param string $filename Image filename.
16  * @return resource Image resource (false if the image type is unsupported, the data is not in a recognised format, or the
17  * image is corrupt and cannot be loaded).
18  */
19  public static function fromFile($filename){
20  return imagecreatefromstring(file_get_contents($filename));
21  }
22  /**
23  * Convert an image resource to base64 encoded uri.
24  * @param resource $image Image resource.
25  * @param string $type Output image type (see PHP's IMAGETYPE_* constants).
26  * @param int $quality Quality for JPEG or PNG format.
27  * @return string False if unsupported type.
28  */
29  public static function dataUri($image,$type = IMAGETYPE_JPEG,$quality = 75){
30  ob_start();
31  switch($type){
32  case IMAGETYPE_PNG:
33  imagepng($image,null,$quality);
34  break;
35  case IMAGETYPE_GIF:
36  imagegif($image);
37  break;
38  case IMAGETYPE_JPEG:
39  imagejpeg($image,null,$quality);
40  break;
41  default:
42  ob_end_clean();
43  return false;
44  }
45  return 'data:' . image_type_to_mime_type($type) . ';base64,' . base64_encode(ob_get_clean());
46  }
47  /**
48  * Scale an image resource to fit a certain box size, maintaining aspect ratio.
49  * @param resource $image Image resource.
50  * @param int $width Box width.
51  * @param int $height Box height.
52  * @param int $options See IMAGE_SCALE_* constants.
53  * @param int $mode Scaling mode (see http://php.net/manual/en/function.imagescale.php).
54  * @return resource Scaled image resource, or false on failure (including image smaller than requested dimension and
55  * IMAGE_SCALE_ONLY_SMALLER in options).
56  */
57  public static function scaleBox($image,$width,$height,$options = 0,$mode = IMG_BILINEAR_FIXED){
58  $x_factor = $width / ($orig_width = imagesx($image));
59  $y_factor = $height / ($orig_height = imagesy($image));
60  $factor = $options & self::IMAGE_SCALE_INSIDE ? min($x_factor,$y_factor) : max($x_factor,$y_factor);
61  if(($options & self::IMAGE_SCALE_ONLY_SMALLER) && ($factor >= 1)) return false;
62  return imagescale($image,$orig_width * $factor,$orig_height * $factor,$mode);
63  }
64 
65 }
static scaleBox($image, $width, $height, $options=0, $mode=IMG_BILINEAR_FIXED)
Scale an image resource to fit a certain box size, maintaining aspect ratio.
Definition: Image.php:57
static dataUri($image, $type=IMAGETYPE_JPEG, $quality=75)
Convert an image resource to base64 encoded uri.
Definition: Image.php:29
Definition: Color.php:3
static fromFile($filename)
Create an image resource from file.
Definition: Image.php:19
Image helpers.
Definition: Image.php:8