FRED™  3.0
FRED™: Framework for Rapid and Easy Development
Resize.php
Go to the documentation of this file.
1 <?php
2 
4 
5 class Resize extends \Rsi\Fred\Stream\Handler{
6 
7  public $mode = IMG_BILINEAR_FIXED;
8  public $jpegQuality = 80;
9  public $widths = []; //!< Allowed widths (small to large; empty = all).
10  public $heights = []; //!< Allowed heights (small to large; empty = all).
11  public $locked = true; //!< Allowed sizes will be locked together by key.
12 
13  /**
14  * Pick the next larger value from an array.
15  * @param array $sizes Allowed sizes.
16  * @param int $size Current size.
17  * @param array $other_sizes Allowed sizes for other dimension.
18  * @param int $other_size Current size for other dimension (will be adapted when locked).
19  */
20  protected function index($sizes,&$size,$other_sizes = null,&$other_size = null){
21  if($size && $sizes){
22  $ref = $size;
23  foreach($sizes as $index => $size){
24  if($this->locked && $other_sizes) $other_size = \Rsi\Record::get($other_sizes,$index);
25  if($size >= $ref) break;
26  }
27  }
28  }
29  /**
30  * Calculate the dimensions for the image.
31  * Taking into account missing dimensions, restrictions, and locking.
32  * @param int $orig_width Width of the original image.
33  * @param int $orig_height Height of the original image.
34  * @param int $new_width In: requested width; out: calculated width.
35  * @param int $new_height In: requested height; out: calculated height.
36  */
37  protected function dimensions($orig_width,$orig_height,&$new_width,&$new_height){
38  $this->index($this->widths,$new_width,$this->heights,$new_height);
39  if(!$new_height) $new_height = intdiv($orig_height * $new_width,$orig_width);
40  $this->index($this->heights,$new_height,$this->widths,$new_width);
41  if(!$new_width) $new_width = intdiv($orig_width * $new_height,$orig_height);
42  }
43 
44  public function read($handlers,$filename,$params = null){
45  $data = parent::read($handlers,$filename,$params);
46  $new_width = \Rsi\Record::get($params,'width');
47  $new_height = \Rsi\Record::get($params,'height');
48  if(($new_width || $new_height) && ($orig_image = imagecreatefromstring($data))){
49  $this->dimensions(imagesx($orig_image),imagesy($orig_image),$new_width,$new_height);
50  $new_image = imagescale($orig_image,$new_width,$new_height,$this->mode);
51  imagedestroy($orig_image);
52  ob_start();
53  switch(\Rsi\File::ext($filename)){
54  case 'gif': imagegif($new_image); break;
55  case 'png': imagepng($new_image); break;
56  default: imagejpeg($new_image,null,$this->jpegQuality);
57  }
58  $data = ob_get_clean();
59  imagedestroy($new_image);
60  }
61  return $data;
62  }
63 
64 }
index($sizes, &$size, $other_sizes=null, &$other_size=null)
Pick the next larger value from an array.
Definition: Resize.php:20
$locked
Allowed sizes will be locked together by key.
Definition: Resize.php:11
read($handlers, $filename, $params=null)
Definition: Resize.php:44
$widths
Allowed widths (small to large; empty = all).
Definition: Resize.php:9
dimensions($orig_width, $orig_height, &$new_width, &$new_height)
Calculate the dimensions for the image.
Definition: Resize.php:37
$heights
Allowed heights (small to large; empty = all).
Definition: Resize.php:10