123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417 |
- <?php
- function image_gd_settings() {
- if (image_gd_check_settings()) {
- $form['status'] = array(
- '#markup' => t('The GD toolkit is installed and working properly.')
- );
- $form['image_jpeg_quality'] = array(
- '#type' => 'textfield',
- '#title' => t('JPEG quality'),
- '#description' => t('Define the image quality for JPEG manipulations. Ranges from 0 to 100. Higher values mean better image quality but bigger files.'),
- '#size' => 10,
- '#maxlength' => 3,
- '#default_value' => variable_get('image_jpeg_quality', 75),
- '#field_suffix' => t('%'),
- );
- $form['#element_validate'] = array('image_gd_settings_validate');
- return $form;
- }
- else {
- 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')));
- return FALSE;
- }
- }
- function image_gd_settings_validate($form, &$form_state) {
-
- $value = $form_state['values']['image_jpeg_quality'];
- if (!is_numeric($value) || $value < 0 || $value > 100) {
- form_set_error('image_jpeg_quality', t('JPEG quality must be a number between 0 and 100.'));
- }
- }
- function image_gd_check_settings() {
-
- return function_exists('imagegd2');
- }
- function image_gd_resize(stdClass $image, $width, $height) {
- $res = image_gd_create_tmp($image, $width, $height);
- if (!imagecopyresampled($res, $image->resource, 0, 0, 0, 0, $width, $height, $image->info['width'], $image->info['height'])) {
- return FALSE;
- }
- imagedestroy($image->resource);
-
- $image->resource = $res;
- $image->info['width'] = $width;
- $image->info['height'] = $height;
- return TRUE;
- }
- function image_gd_rotate(stdClass $image, $degrees, $background = NULL) {
-
- if (!function_exists('imagerotate')) {
- watchdog('image', 'The image %file could not be rotated because the imagerotate() function is not available in this PHP installation.', array('%file' => $image->source));
- return FALSE;
- }
-
-
-
- $degrees -= floor($degrees / 360) * 360;
-
- if (isset($background)) {
- $background = array(
- 'red' => $background >> 16 & 0xFF,
- 'green' => $background >> 8 & 0xFF,
- 'blue' => $background & 0xFF,
- 'alpha' => 0,
- );
- }
- else {
-
- $background = array(
- 'red' => 255,
- 'green' => 255,
- 'blue' => 255,
- 'alpha' => 127
- );
- }
-
- $background_idx = imagecolorallocatealpha($image->resource, $background['red'], $background['green'], $background['blue'], $background['alpha']);
-
-
- if ($image->info['extension'] == 'gif') {
-
-
-
- $gif_transparent_id = imagecolortransparent($image->resource);
- if ($gif_transparent_id !== -1) {
-
-
- $transparent_gif_color = imagecolorsforindex($image->resource, $gif_transparent_id);
- if ($background['alpha'] >= 127) {
-
-
- $background_idx = $gif_transparent_id;
- }
- }
- else {
-
- if ($background['alpha'] >= 127) {
-
- $transparent_gif_color = $background;
- }
- }
- }
- $image->resource = imagerotate($image->resource, 360 - $degrees, $background_idx);
-
- if (isset($transparent_gif_color)) {
- $background = imagecolorexactalpha($image->resource, $transparent_gif_color['red'], $transparent_gif_color['green'], $transparent_gif_color['blue'], $transparent_gif_color['alpha']);
- imagecolortransparent($image->resource, $background);
- }
- $image->info['width'] = imagesx($image->resource);
- $image->info['height'] = imagesy($image->resource);
- return TRUE;
- }
- function image_gd_crop(stdClass $image, $x, $y, $width, $height) {
- $res = image_gd_create_tmp($image, $width, $height);
- if (!imagecopyresampled($res, $image->resource, 0, 0, $x, $y, $width, $height, $width, $height)) {
- return FALSE;
- }
-
- imagedestroy($image->resource);
- $image->resource = $res;
- $image->info['width'] = $width;
- $image->info['height'] = $height;
- return TRUE;
- }
- function image_gd_desaturate(stdClass $image) {
-
- if (!function_exists('imagefilter')) {
- watchdog('image', 'The image %file could not be desaturated because the imagefilter() function is not available in this PHP installation.', array('%file' => $image->source));
- return FALSE;
- }
- return imagefilter($image->resource, IMG_FILTER_GRAYSCALE);
- }
- function image_gd_load(stdClass $image) {
- $extension = str_replace('jpg', 'jpeg', $image->info['extension']);
- $function = 'imagecreatefrom' . $extension;
- if (function_exists($function) && $image->resource = $function($image->source)) {
- if (imageistruecolor($image->resource)) {
- return TRUE;
- }
- else {
-
-
-
- $resource = image_gd_create_tmp($image, $image->info['width'], $image->info['height']);
- if ($resource) {
- imagecopy($resource, $image->resource, 0, 0, 0, 0, imagesx($resource), imagesy($resource));
- imagedestroy($image->resource);
- $image->resource = $resource;
- }
- }
- return (bool) $image->resource;
- }
- return FALSE;
- }
- function image_gd_save(stdClass $image, $destination) {
- $scheme = file_uri_scheme($destination);
-
- if ($scheme && file_stream_wrapper_valid_scheme($scheme)) {
-
- $local_wrappers = file_get_stream_wrappers(STREAM_WRAPPERS_LOCAL);
- if (!isset($local_wrappers[$scheme])) {
- $permanent_destination = $destination;
- $destination = drupal_tempnam('temporary://', 'gd_');
- }
-
- $destination = drupal_realpath($destination);
- }
- $extension = str_replace('jpg', 'jpeg', $image->info['extension']);
- $function = 'image' . $extension;
- if (!function_exists($function)) {
- return FALSE;
- }
- if ($extension == 'jpeg') {
- $success = $function($image->resource, $destination, variable_get('image_jpeg_quality', 75));
- }
- else {
-
- if ($extension == 'png') {
- imagealphablending($image->resource, FALSE);
- imagesavealpha($image->resource, TRUE);
- }
- $success = $function($image->resource, $destination);
- }
-
- if (isset($permanent_destination) && $success) {
- return (bool) file_unmanaged_move($destination, $permanent_destination, FILE_EXISTS_REPLACE);
- }
- return $success;
- }
- function image_gd_create_tmp(stdClass $image, $width, $height) {
- $res = imagecreatetruecolor($width, $height);
- if ($image->info['extension'] == 'gif') {
-
-
- $transparent = imagecolortransparent($image->resource);
- if ($transparent >= 0) {
-
-
- $palette_size = imagecolorstotal($image->resource);
- if ($palette_size == 0 || $transparent < $palette_size) {
-
-
-
-
-
-
- $transparent_color = imagecolorsforindex($image->resource, $transparent);
- $transparent = imagecolorallocate($res, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
-
- imagefill($res, 0, 0, $transparent);
- imagecolortransparent($res, $transparent);
- }
- else {
- imagefill($res, 0, 0, imagecolorallocate($res, 255, 255, 255));
- }
- }
- }
- elseif ($image->info['extension'] == 'png') {
- imagealphablending($res, FALSE);
- $transparency = imagecolorallocatealpha($res, 0, 0, 0, 127);
- imagefill($res, 0, 0, $transparency);
- imagealphablending($res, TRUE);
- imagesavealpha($res, TRUE);
- }
- else {
- imagefill($res, 0, 0, imagecolorallocate($res, 255, 255, 255));
- }
- return $res;
- }
- function image_gd_get_info(stdClass $image) {
- $details = FALSE;
- $data = @getimagesize($image->source);
- if (isset($data) && is_array($data)) {
- $extensions = array('1' => 'gif', '2' => 'jpg', '3' => 'png');
- $extension = isset($extensions[$data[2]]) ? $extensions[$data[2]] : '';
- $details = array(
- 'width' => $data[0],
- 'height' => $data[1],
- 'extension' => $extension,
- 'mime_type' => $data['mime'],
- );
- }
- return $details;
- }
|