00001 <?php
00002
00003
00017 function image_gd_info() {
00018 return array('name' => 'gd', 'title' => t('GD2 image manipulation toolkit'));
00019 }
00020
00024 function image_gd_settings() {
00025 if (image_gd_check_settings()) {
00026 $form = array();
00027 $form['status'] = array(
00028 '#value' => t('The GD toolkit is installed and working properly.')
00029 );
00030
00031 $form['image_jpeg_quality'] = array(
00032 '#type' => 'textfield',
00033 '#title' => t('JPEG quality'),
00034 '#description' => t('Define the image quality for JPEG manipulations. Ranges from 0 to 100. Higher values mean better image quality but bigger files.'),
00035 '#size' => 10,
00036 '#maxlength' => 3,
00037 '#default_value' => variable_get('image_jpeg_quality', 75),
00038 '#field_suffix' => t('%'),
00039 );
00040 $form['#element_validate'] = array('image_gd_settings_validate');
00041
00042 return $form;
00043 }
00044 else {
00045 form_set_error('image_toolkit', t('The GD image toolkit requires that the GD module for PHP be installed and configured properly. For more information see <a href="@url">PHP\'s image documentation</a>.', array('@url' => 'http://php.net/image')));
00046 return FALSE;
00047 }
00048 }
00049
00053 function image_gd_settings_validate($form, &$form_state) {
00054
00055 $value = $form_state['values']['image_jpeg_quality'];
00056 if (!is_numeric($value) || $value < 0 || $value > 100) {
00057 form_set_error('image_jpeg_quality', t('JPEG quality must be a number between 0 and 100.'));
00058 }
00059 }
00060
00067 function image_gd_check_settings() {
00068 if ($check = get_extension_funcs('gd')) {
00069 if (in_array('imagegd2', $check)) {
00070
00071 return TRUE;
00072 }
00073 }
00074 return FALSE;
00075 }
00076
00080 function image_gd_resize($source, $destination, $width, $height) {
00081 if (!file_exists($source)) {
00082 return FALSE;
00083 }
00084
00085 $info = image_get_info($source);
00086 if (!$info) {
00087 return FALSE;
00088 }
00089
00090 $im = image_gd_open($source, $info['extension']);
00091 if (!$im) {
00092 return FALSE;
00093 }
00094
00095 $res = imagecreatetruecolor($width, $height);
00096 if ($info['extension'] == 'png') {
00097 $transparency = imagecolorallocatealpha($res, 0, 0, 0, 127);
00098 imagealphablending($res, FALSE);
00099 imagefilledrectangle($res, 0, 0, $width, $height, $transparency);
00100 imagealphablending($res, TRUE);
00101 imagesavealpha($res, TRUE);
00102 }
00103 elseif ($info['extension'] == 'gif') {
00104
00105 $transparency_index = imagecolortransparent($im);
00106 if ($transparency_index >= 0) {
00107
00108 $transparent_color = imagecolorsforindex($im, $transparency_index);
00109
00110 $transparency_index = imagecolorallocate($res, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
00111
00112 imagefill($res, 0, 0, $transparency_index);
00113
00114 imagecolortransparent($res, $transparency_index);
00115
00116 $number_colors = imagecolorstotal($im);
00117
00118 imagetruecolortopalette($res, TRUE, $number_colors);
00119 }
00120 }
00121 imagecopyresampled($res, $im, 0, 0, 0, 0, $width, $height, $info['width'], $info['height']);
00122 $result = image_gd_close($res, $destination, $info['extension']);
00123
00124 imagedestroy($res);
00125 imagedestroy($im);
00126
00127 return $result;
00128 }
00129
00133 function image_gd_rotate($source, $destination, $degrees, $background = 0x000000) {
00134 if (!function_exists('imageRotate')) {
00135 return FALSE;
00136 }
00137
00138 $info = image_get_info($source);
00139 if (!$info) {
00140 return FALSE;
00141 }
00142
00143 $im = image_gd_open($source, $info['extension']);
00144 if (!$im) {
00145 return FALSE;
00146 }
00147
00148 $res = imageRotate($im, $degrees, $background);
00149 $result = image_gd_close($res, $destination, $info['extension']);
00150
00151 return $result;
00152 }
00153
00157 function image_gd_crop($source, $destination, $x, $y, $width, $height) {
00158 $info = image_get_info($source);
00159 if (!$info) {
00160 return FALSE;
00161 }
00162
00163 $im = image_gd_open($source, $info['extension']);
00164 $res = imageCreateTrueColor($width, $height);
00165 imageCopy($res, $im, 0, 0, $x, $y, $width, $height);
00166 $result = image_gd_close($res, $destination, $info['extension']);
00167
00168 imageDestroy($res);
00169 imageDestroy($im);
00170
00171 return $result;
00172 }
00173
00184 function image_gd_open($file, $extension) {
00185 $extension = str_replace('jpg', 'jpeg', $extension);
00186 $open_func = 'imageCreateFrom' . $extension;
00187 if (!function_exists($open_func)) {
00188 return FALSE;
00189 }
00190 return $open_func($file);
00191 }
00192
00205 function image_gd_close($res, $destination, $extension) {
00206 $extension = str_replace('jpg', 'jpeg', $extension);
00207 $close_func = 'image' . $extension;
00208 if (!function_exists($close_func)) {
00209 return FALSE;
00210 }
00211 if ($extension == 'jpeg') {
00212 return $close_func($res, $destination, variable_get('image_jpeg_quality', 75));
00213 }
00214 else {
00215 return $close_func($res, $destination);
00216 }
00217 }
00218