Issue #1768432 by drzraf: Fixed update of {image} table to Drupal7 incorrectly applie...
[project/image.git] / image_legacy.module
1 <?php
2
3 define('IMAGE_ORIGINAL', '_original');
4 define('IMAGE_PREVIEW', 'preview');
5 define('IMAGE_THUMBNAIL', 'thumbnail');
6
7 define('IMAGE_LINK_HIDDEN', 0);
8 define('IMAGE_LINK_SHOWN', 1);
9 define('IMAGE_LINK_NEW', 2);
10
11 /**
12 * Implementation of hook_help().
13 */
14 function image_legacy_help($path, $arg) {
15 switch ($path) {
16 case 'admin/help#image_legacy':
17 return t('TODO: Create admin help text.');
18 // OPTIONAL: Add additional cases for other paths that should display help text.
19 }
20 }
21
22
23 /**
24 * Implementation of hook_init().
25 */
26 function image_legacy_init() {
27
28 }
29
30 /**
31 * Implementation of hook_perm
32 */
33 function image_perm() {
34 return array('view original images', 'create images', 'edit own images', 'edit any images', 'delete own images', 'delete any images');
35 }
36
37 /**
38 * Creates an image filename.
39 *
40 * @param $filepath
41 * The full path and filename of the original image file,relative to Drupal
42 * root, eg 'sites/default/files/images/myimage.jpg'.
43 *
44 * @return
45 * A full path and filename with derivative image label inserted if required.
46 */
47 function _image_filename($filepath, $label = IMAGE_ORIGINAL, $temp = FALSE) {
48 // Get default path for a new file.
49 $path = file_directory_path() . '/' . variable_get('image_default_path', 'images');
50 if ($temp) {
51 $path .= '/temp';
52 }
53
54 $original_path = dirname($filepath);
55 $filename = basename($filepath);
56
57 if ($label && ($label != IMAGE_ORIGINAL)) {
58 // Keep resized images in the same path, where original is (does not
59 // apply to temporary files, these still use the default path).
60 if (!$temp && $original_path != '.') {
61 $path = $original_path;
62 }
63 // Insert the resized name in non-original images.
64 $pos = strrpos($filename, '.');
65 if ($pos === FALSE) {
66 // The file had no extension - which happens in really old image.module
67 // versions, so figure out the extension.
68 $image_info = image_get_info(file_create_path($path . '/' . $filename));
69 $filename = $filename . '.' . $label . '.' . $image_info['extension'];
70 }
71 else {
72 $filename = substr($filename, 0, $pos) . '.' . $label . substr($filename, $pos);
73 }
74 }
75
76 return file_create_path($path . '/' . $filename);
77 }
78
79 /**
80 * Helper function to return the defined sizes (or proper defaults).
81 *
82 * @param $size
83 * An optional string to return only the image size with the specified key.
84 * @param $aspect_ratio
85 * Float value with the ratio of image height / width. If a size has only one
86 * dimension provided this will be used to compute the other.
87 *
88 * @return
89 * An associative array with width, height, and label fields for the size.
90 * If a $size parameter was specified and it cannot be found FALSE will be
91 * returned.
92 */
93 function image_get_sizes($size = NULL, $aspect_ratio = NULL) {
94 $defaults = array(
95 IMAGE_ORIGINAL => array('width' => '', 'height' => '', 'label' => t('Original'), 'operation' => 'scale', 'link' => IMAGE_LINK_SHOWN),
96 IMAGE_THUMBNAIL => array('width' => 100, 'height' => 100, 'label' => t('Thumbnail'), 'operation' => 'scale', 'link' => IMAGE_LINK_SHOWN),
97 IMAGE_PREVIEW => array('width' => 640, 'height' => 640, 'label' => t('Preview'), 'operation' => 'scale', 'link' => IMAGE_LINK_SHOWN),
98 );
99
100 $sizes = array();
101 foreach (variable_get('image_sizes', $defaults) as $key => $val) {
102 // Only return sizes with a label.
103 if (!empty($val['label'])) {
104 // For a size with only one dimension specified, compute the other
105 // dimension based on an aspect ratio.
106 if ($aspect_ratio && (empty($val['width']) || empty($val['height']))) {
107 if (empty($val['height']) && !empty($val['width'])) {
108 $val['height'] = (int)round($val['width'] * $aspect_ratio);
109 }
110 elseif (empty($val['width']) && !empty($val['height'])) {
111 $val['width'] = (int)round($val['height'] / $aspect_ratio);
112 }
113 }
114 $sizes[$key] = $val;
115 }
116 }
117
118 // If they requested a specific size return only that.
119 if (isset($size)) {
120 // Only return an array if it's available.
121 return isset($sizes[$size]) ? $sizes[$size] : FALSE;
122 }
123
124 return $sizes;
125 }
126
127 /**
128 * Helper function to preserve backwards compatibility. This has been
129 * deprecated in favor of image_get_sizes().
130 *
131 * @TODO: Remove this in a future version.
132 */
133 function _image_get_sizes($size = NULL, $aspect_ratio = NULL) {
134 return image_get_sizes($size, $aspect_ratio);
135 }
136
137 /**
138 * Is a given size a built-in, required size?
139 *
140 * @param $size
141 * One of the keys in the array returned by image_get_sizes().
142 *
143 * @return boolean
144 */
145 function _image_is_required_size($size) {
146 return in_array($size, array(IMAGE_THUMBNAIL, IMAGE_PREVIEW, IMAGE_ORIGINAL));
147 }
148