naehrstoff / image_resize

With the Image Resize plugin enabled, you can easily create thumbnails of your images with the Frog CMS

This URL has Read+Write access

image_resize / ImageResize.php
100644 187 lines (154 sloc) 5.579 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
 
/**
* Helper functions, many from Drupal
* http://drupal.org
*/
 
class ImageResize {
 
  public function image_scale($source, $destination, $width, $height)
  {
      $info = ImageResize::image_get_info($source);
 
      // don't scale up
      if ($width > $info['width'] && $height > $info['height']) {
          return false;
      }
 
      $aspect = $info['height'] / $info['width'];
      if (!$height || ($width && $aspect < $height / $width)) {
          $width = (int)min($width, $info['width']);
          $height = (int)round($width * $aspect);
      } else {
          $height = (int)min($height, $info['height']);
          $width = (int)round($height / $aspect);
      }
 
      return ImageResize::image_gd_resize($source, $destination, $width, $height);
  }
  
  public function image_scale_cropped($source, $destination, $width, $height)
  {
      $info = ImageResize::image_get_info($source);
 
      // don't scale up
      if ($width > $info['width'] && $height > $info['height']) {
          return false;
      }
            
      /* If we are square. */
      if (!$height || !$width || $height == $width) {
          if ($info['width'] > $info['height']) {
              $source_width = $source_height = $info['height'];
              $source_y = 0;
              $source_x = round(($info['width'] - $info['height']) / 2);
          } else {
              $source_width = $source_height = $info['width'];
              $source_x = 0;
              $source_y = round(($info['height'] - $info['width']) / 2);
          }
          if ($width) {
              $height = $width;
          } else {
              $width = $height;
          }
      /* We are not square. */
      } else {
 
          $x_ratio = $width / $info['width'];
          $y_ratio = $height / $info['height'];
 
          if (($x_ratio * $info['width']) >= $width && ($x_ratio * $info['height']) >= $height) {
              $aspect = $width / $height;
              $x_ratio * $info['width'];
              $source_width = $info['width'];
              $source_height = round($source_width / $aspect);
              $source_x = 0;
              $source_y = round(($info['height'] - $source_height) / 2);
          } else {
              $aspect = $height / $width;
              $source_height = $info['height'];
              $source_width = round($source_height / $aspect);
              $source_x = round(($info['width'] - $source_width) / 2);
              $source_y = 0;
 
          }
      }
 
 
      return ImageResize::image_gd_resize($source, $destination, $width, $height, $source_x, $source_y, $source_width, $source_height);
  }
  
  
  /**
* GD2 has to be available on the system
*
* @return boolean
*/
  public function gd_available() {
    if ($check = get_extension_funcs('gd')) {
      if (in_array('imagegd2', $check)) {
        // GD2 support is available.
        return true;
      }
    }
    return false;
  }
  
  
  /**
* Get details about an image.
*
* @return array containing information about the image
* 'width': image's width in pixels
* 'height': image's height in pixels
* 'extension': commonly used extension for the image
* 'mime_type': image's MIME type ('image/jpeg', 'image/gif', etc.)
*/
  function image_get_info($file) {
    if (!is_file($file)) {
      return false;
    }
 
    $details = false;
    $data = @getimagesize($file);
 
    if (is_array($data)) {
      $extensions = array('1' => 'gif', '2' => 'jpg', '3' => 'png');
      $extension = array_key_exists($data[2], $extensions) ? $extensions[$data[2]] : '';
      $details = array('width' => $data[0],
                       'height' => $data[1],
                       'extension' => $extension,
                       'mime_type' => $data['mime']);
    }
 
    return $details;
  }
  
  
  /**
* Scale an image to the specified size using GD.
*/
  function image_gd_resize($source, $destination, $width, $height, $source_x = 0, $source_y = 0, $source_width = null, $source_height = null) {
    if (!file_exists($source)) {
      return false;
    }
 
    $info = ImageResize::image_get_info($source);
    if (!$info) {
      return false;
    }
 
    $im = ImageResize::image_gd_open($source, $info['extension']);
    if (!$im) {
      return false;
    }
    
    /* Get source dimensions from GD info is not passed as parameters. */
    $source_width = is_null($source_width) ? $info['width'] : $source_width;
    $source_height = is_null($source_height) ? $info['height'] : $source_height;
    
    $res = imageCreateTrueColor($width, $height);
    imageCopyResampled($res, $im, 0, 0, $source_x, $source_y, $width, $height, $source_width, $source_height);
    $result = ImageResize::image_gd_close($res, $destination, $info['extension']);
 
    imageDestroy($res);
    imageDestroy($im);
 
    return $result;
  }
  
  
  /**
* GD helper function to create an image resource from a file.
*/
  function image_gd_open($file, $extension) {
    $extension = str_replace('jpg', 'jpeg', $extension);
    $open_func = 'imagecreatefrom'. $extension;
    if (!function_exists($open_func)) {
      return false;
    }
    return $open_func($file);
  }
  
 
  /**
* GD helper to write an image resource to a destination file.
*/
  function image_gd_close($res, $destination, $extension) {
    $extension = str_replace('jpg', 'jpeg', $extension);
    $close_func = 'image'. $extension;
    if (!function_exists($close_func)) {
      return false;
    }
    return $close_func($res, $destination);
  }
}