Visa bild från imagefield i blockvy

Events happening in the community are now at Drupal community events on www.drupal.org.
Fredde's picture

Jag vill ha ett block som enbart visar bilden jag laddat upp till en node via ett CCK imagefield.
Hittar inte något fält som lyckas med detta? Borde ju varit "bifogade bilder". Men det funka inte..

Comments

Man kan skriva vanlig

bambi_2's picture

Man kan skriva vanlig html-kod i blocket med sökvägen till bilden ( img src="......." ).

I mitt fall blev det img src="/internsida/sites/all/themes/foliage/sta_logga_sm.gif"

dvs bilden ligger i just det temat jag använder och allt i mappen internsida på webhotellet.

OBS! Det ska vara taggar omkring, men om jag skriver dit det blir det bara ett rött kryss dvs bilden hittas inte.

A-B

Ann-Britt

Ja då hade jag ju lika gärna

Fredde's picture

Ja då hade jag ju lika gärna kunnat göra en statisk sida av det hela. Poängen är att vyn ska hämta ut bilder från noder av en viss nodtyp. Klickar man på bilden ska man komma till dess nod.
Gör jag en ny nod av den typen ska den nya noden komma upp i blocket med sin bild.

Det fungerade utmärkt när jag inte körde med CCK imagefield utan körde med standard Image module. Då hette fältet något i stil med "bifogad bild". Det måste väl gå att hämta ut bilder även från imagefield på detta sätt??

I Views lägger du till ett

magnus's picture

I Views lägger du till ett fält av typen "Innehåll". Där kommer du att hitta ditt bildfält. Kombinera gärna med Imagecache och välj vilken utmatning du vill ha på bilden i blocket.

Tack!

Fredde's picture

Tack! Jag lyckades till slut.. Nu återstår bara att få imagecache att fungera på one.com... Eller finns det något annat sätt att visa bilden mindre i blocket?

htaccess i underkataloger

itangalo's picture

Det borde gå bra (typ) att använda ImageCache på one.com, om man bara kommer ihåg att redigera alla .htaccess-filer som dyker upp i katalogerna för ImageCache-lagring.

Lycka till.
//Johan Falk, NodeOne
**
Vill du lära dig mer om Drupal? Kolla in samlingen av tips och guider!
Har du en fråga om Drupal? Missa inte FAQ-sidan på forumet!
Nu finns första Drupalboken på svenska! Börja med Drupal -- smart webbpublicering med open source
Sugen på en Drupalkurs? Kolla in NodeOnes utbud av Drupalkurser

Problemen med ImageCache på

timbrandin's picture

Problemen med ImageCache på one härstammar i deras beslut att byta till php 5.3.
Se denna issue:
http://drupal.org/node/540486

Antingen laddar man hem någon av patcharna om man vet hur man gör.
Annars finns ett annat jättelätt sätt också, öppna:
imageapi_gd.module & imageapi.module och ta bort "&" i funktionsdefinitionerna, som t.ex.

function func(&$object, ... ) {
}

ska bli:

function func($object, ... ) {
}

Glöm inte att kommentera raderna "Options" i htaccess filen i sites/default/files t.ex.:

#Options None
#Options +FollowSymLinks

Knepigt

Fredde's picture

Jag har gjort det ni tipsat om. Men jag får det fortfarande inte att fungera. Blir fortfarande ett frågetecken istället för bild. Filerna du nämner är filer till ImageAPI. Det ska inte fixas något i filerna till själva ImageCache då?

Nej de ska inte behövas. Men

timbrandin's picture

Nej de ska inte behövas.
Men när du gör en Preset, får du en bild under Förhansvisning?

I så fall funkar ImageAPI & ImageCache iaf.

Icke

Fredde's picture

Nej jag får bara ett frågetecken där också tyvärr.

<?php// $Id:

timbrandin's picture

<?php
// $Id: imageapi.module,v 1.23.2.3 2009/03/18 08:35:59 drewish Exp $

/
* @file
*
* An ImageAPI supporting additional image plugins as modules.
* Images are treated as objects, and images are not written per
* manipulation as Drupal's core image handling works.
*
*
* imageapi image api workflow...
* $image = imageapi_image_open($path) to get an image object for $path...
* image_X($image, $arg1, $arg2)  to manipulate image object...
* imageapi_image_close($image) to overwrite original image.
*
*/

/

* Implementation of hook_perm().
*/
function imageapi_perm() {
  return array('administer imageapi');
}

/
* Implementation of hook_menu().
*/
function imageapi_menu() {
  $items = array();
  $items['admin/settings/imageapi'] = array(
    'title' => 'ImageAPI',
    'description' => 'Configure ImageAPI.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('imageapi_settings'),
    'access arguments' => array('administer imageapi'),
  );

  $toolkits = imageapi_get_available_toolkits();
  if ($toolkits) {
    $items['admin/settings/imageapi/list'] = array(
      'title' => 'List',
      'type' => MENU_DEFAULT_LOCAL_TASK,
      'weight' => -1,
    );
    $items['admin/settings/imageapi/config'] = array(
      'title' => 'Configure',
      'type' => MENU_LOCAL_TASK,
      'page callback' => 'drupal_get_form',
      'page arguments' => array(imageapi_default_toolkit() .'_settings_form'),
      'access arguments' => array('administer imageapi'),
    );
    foreach ($toolkits as $module => $info) {
      if (function_exists($module .'_settings_form')) {
        $items['admin/settings/imageapi/config/'. $module] = array(
          'title' => '@name',
          'title arguments' => array('@name' => $info['name']),
          'page callback' => 'drupal_get_form',
          'page arguments' => array($module .'_settings_form'),
          'access arguments' => array('administer imageapi'),
          'type' => $module == imageapi_default_toolkit() ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK,
        );
      }
      else {
        drupal_set_message(t('ImageAPI toolkit missing settings form'), 'error');
      }
    }
  }
  return $items;
}

function imageapi_settings() {
  $form = array();

  $toolkits = imageapi_get_available_toolkits();

  switch (count($toolkits)) {
    case 0:
      $form['imageapi_toolkits']['#value'] = t('There are no image toolkit modules enabled. Toolkit modules can be enabled from the <a href="!admin-build-modules">module configuration page</a>.', array('!admin-build-modules' => url('admin/build/modules')));
      return $form;

    case 1:
      $toolkit = key($toolkits);
      // The variable needs to be manually set. Otherwise, if a user has two
      // toolkits and disables the selected one they won't be able to select the
      // remaing toolkit.
      variable_set('imageapi_image_toolkit', $toolkit);
      $form['imageapi_image_toolkit']['#value'] = t('The %toolkit module is the only enabled image toolkit. Drupal will use it for resizing, cropping and other image manipulations.', array('%toolkit' => $toolkits[$toolkit]['name']));
      return $form;

    default:
      $options = array();
      foreach ($toolkits as $module => $info) {
        $options[$module] = $info['name'];
      }
      $form['imageapi_image_toolkit'] = array(
        '#type' => 'radios',
        '#title' => t('Select a default image processing toolkit'),
        '#default_value' => imageapi_default_toolkit(),
        '#options' => $options,
        '#description' => t('This setting lets you choose which toolkit Drupal uses resizing, cropping and other image manipulations.'),
      );
  }

  return system_settings_form($form);
}

/

* Return a list of available toolkits.
*
* @return
*   An array of the enabled image toolkit modules. The module name is the key
*   and the value is a sub-array with the module's 'name' and 'description'.
*/
function imageapi_get_available_toolkits() {
  static $toolkits;

  if (!isset($toolkits)) {
    $toolkits = array();
    foreach (module_implements('imageapi_toolkit', TRUE) as $module) {
      $info = drupal_parse_info_file(drupal_get_path('module', $module) .'/'. $module .'.info');
      $toolkits[$module] = array('name' => $info['name'], 'description' => $info['description']);
    }
  }

  return $toolkits;
}

/
* Retrieve the name of the currently used toolkit.
*
* @return
*   String containing the name of the toolkit, or FALSE if none is available.
*/
function imageapi_default_toolkit() {
  $toolkit = variable_get('imageapi_image_toolkit', 'imageapi_gd');
  // Verify that the image toolkit is available.
  if (isset($toolkit) && module_exists($toolkit)) {
    return $toolkit;
  }
  // If it's not fall back to first available toolist.
  foreach (imageapi_get_available_toolkits() as $toolkit => $info) {
    return $toolkit;
  }
  return FALSE;
}


/

* Invokes the given method using the currently selected toolkit.
*
* @param $method
*   A string containing the method to invoke.
* @param $image
*   An image object returned by imageapi_image_open().
* @param $params
*   An optional array of parameters to pass to the toolkit method.
* @return
*   Mixed values (typically Boolean indicating successful operation).
*/
function imageapi_toolkit_invoke($method, &$image, array $params = array()) {
  $function = $image->toolkit . '_image_' . $method;
  if (function_exists($function)) {
    array_unshift($params, $image);
    return call_user_func_array($function, $params);
  }
  watchdog('imageapi', 'The selected image handling toolkit %toolkit can not correctly process %function.', array('%toolkit' => $image->toolkit, '%function' => $function), WATCHDOG_ERROR);
  return FALSE;
}

/
* Scales an image to the exact width and height given.
*
* This function achieves the target aspect ratio by cropping the original image
* equally on both sides, or equally on the top and bottom. This function is
* useful to create uniform sized avatars from larger images.
*
* The resulting image always has the exact target dimensions.
*
* @param $image
*   An image object returned by imageapi_image_open().
* @param $width
*   The target width, in pixels.
* @param $height
*   The target height, in pixels.
* @return
*   TRUE or FALSE, based on success.
*/
function imageapi_image_scale_and_crop(&$image, $width, $height) {
  $scale = max($width / $image->info['width'], $height / $image->info['height']);
  $x = ($image->info['width'] * $scale - $width) / 2;
  $y = ($image->info['height'] * $scale - $height) / 2;

  if (imageapi_image_resize($image, $image->info['width'] * $scale, $image->info['height'] * $scale)) {
    return imageapi_image_crop($image, $x, $y, $width, $height);
  }
  return FALSE;
}

/

* Scales an image to the given width and height while maintaining aspect
* ratio.
*
* The resulting image can be smaller for one or both target dimensions.
*
* @param $image
*   An image object returned by imageapi_image_open().
* @param $width
*   The target width, in pixels. This value is omitted then the scaling will
*   based only on the height value.
* @param $height
*   The target height, in pixels. This value is omitted then the scaling will
*   based only on the width value.
* @param $upscale
*   Boolean indicating that files smaller than the dimensions will be scalled
*   up. This generally results in a low quality image.
* @return
*   TRUE or FALSE, based on success.
*/
function imageapi_image_scale(&$image, $width = NULL, $height = NULL, $upscale = FALSE) {
  $aspect = $image->info['height'] / $image->info['width'];

  if ($upscale) {
    // Set width/height according to aspect ratio if either is empty.
    $width = !empty($width) ? $width : $height / $aspect;
    $height = !empty($height) ? $height : $width / $aspect;
  }
  else {
    // Set impossibly large values if the width and height aren't set.
    $width = !empty($width) ? $width : 9999999;
    $height = !empty($height) ? $height : 9999999;

    // Don't scale up.
    if (round($width) >= $image->info['width'] && round($height) >= $image->info['height']) {
      return TRUE;
    }
  }

  if ($aspect < $height / $width) {
    $height = $width * $aspect;
  }
  else {
    $width = $height / $aspect;
  }

  return imageapi_image_resize($image, $width, $height);
}

/
* Resize an image to the given dimensions (ignoring aspect ratio).
*
* @param $image
*   An image object returned by imageapi_image_open().
* @param $width
*   The target width, in pixels.
* @param $height
*   The target height, in pixels.
* @return
*   TRUE or FALSE, based on success.
*/
function imageapi_image_resize(&$image, $width, $height) {
  $width = (int) round($width);
  $height = (int) round($height);

  return imageapi_toolkit_invoke('resize', $image, array($width, $height));
}

/

* Rotate an image by the given number of degrees.
*
* @param $image
*   An image object returned by imageapi_image_open().
* @param $degrees
*   The number of (clockwise) degrees to rotate the image.
* @param $background
*   An hexadecimal integer specifying the background color to use for the
*   uncovered area of the image after the rotation. E.g. 0x000000 for black,
*   0xff00ff for magenta, and 0xffffff for white. For images that support
*   transparency, this will default to transparent. Otherwise it will
*   be white.
* @return
*   TRUE or FALSE, based on success.
*/
function imageapi_image_rotate(&$image, $degrees, $background = NULL) {
  return imageapi_toolkit_invoke('rotate', $image, array($degrees, $background));
}

/
* Sharpen an image given some sharpening parameters.
*
* NOTE: These parameters only have an effect when Imagemagick is used.
*       GD will used a fixed convolution matrix as described in imageapi_gd.module
*
* @param $image
*   An imageapi image object returned by imageapi_image_open().
* @param $radius
*   The radius of the gaussian, in pixels, not counting the center pixel. (default 0.5)
* @param $sigma
*   The standard deviation of the gaussian, in pixels. (default 0.5)
* @param $amount
*   The percentage of the difference between the original and the blur image that is
*   added back into the original. (default 100)
* @param $threshold
*   The threshold, as a fraction of max RGB levels, needed to apply the difference
*   amount. (default 0.05)
* @return
*   True or FALSE, based on success.
*/
function imageapi_image_sharpen(&$image, $radius, $sigma, $amount, $threshold) {
  return imageapi_toolkit_invoke('sharpen', $image, array($radius, $sigma, $amount, $threshold));
}

/

* Crop an image to the rectangle specified by the given rectangle.
*
* @param $image
*   An image object returned by imageapi_image_open().
* @param $x
*   The top left coordinate, in pixels, of the crop area (x axis value).
* @param $y
*   The top left coordinate, in pixels, of the crop area (y axis value).
* @param $width
*   The target width, in pixels.
* @param $height
*   The target height, in pixels.
* @return
*   TRUE or FALSE, based on success.
*/
function imageapi_image_crop(&$image, $x, $y, $width, $height) {
  $aspect = $image->info['height'] / $image->info['width'];
  if (empty($height)) $height = $width / $aspect;
  if (empty($width)) $width = $height * $aspect;

  $width = (int) round($width);
  $height = (int) round($height);

  return imageapi_toolkit_invoke('crop', $image, array($x, $y, $width, $height));
}

/
* Convert an image to grayscale.
*
* @param $image
*   An image object returned by imageapi_image_open().
* @return
*   TRUE or FALSE, based on success.
*/
function imageapi_image_desaturate(&$image) {
  return imageapi_toolkit_invoke('desaturate', $image);
}

/

* Open an image file and return an image object.
*
* Any changes to the file are not saved until imageapi_image_close() is called.
*
* @param $file
*   Path to an image file.
* @param $toolkit
*   An optional, image toolkit name to override the default.
* @return
*   An image object or FALSE if there was a problem loading the file. The
*   image object has the following properties:
*    - 'source' - The original file path.
*    - 'info' - The array of information returned by image_get_info()
*    - 'toolkit' - The name of the image toolkit requested when the image was
*      loaded.
*   Image tookits may add additional properties. The caller is advised not to
*   monkey about with them.
*/
function imageapi_image_open($file, $toolkit = FALSE) {
  if (!$toolkit) {
    $toolkit = imageapi_default_toolkit();
  }
  if ($toolkit) {
    $image = new stdClass();
    $image->source = $file;
    $image->info = image_get_info($file);
    $image->toolkit = $toolkit;
    if (imageapi_toolkit_invoke('open', $image)) {
      return $image;
    }
  }
  return FALSE;
}

/
* Close the image and save the changes to a file.
*
* @param $image
*   An image object returned by imageapi_image_open(). The object's 'info'
*   property will be updated if the file is saved successfully.
* @param $destination
*   Destination path where the image should be saved. If it is empty the
*   original image file will be overwritten.
* @return
*   TRUE or FALSE, based on success.
*/
function imageapi_image_close($image, $destination = NULL) {
  if (empty($destination)) {
    $destination = $image->source;
  }
  if ($return = imageapi_toolkit_invoke('close', $image, array($destination))) {
    // Clear the cached file size and refresh the image information.
    clearstatcache();
    $image->info = image_get_info($destination);

    if (@chmod($destination, 0664)) {
      return $return;
    }
    watchdog('imageapi', 'Could not set permissions on destination file: %file', array('%file' => $destination));
  }
  return FALSE;
}


/

* Convert a hex string to its RGBA (Red, Green, Blue, Alpha) integer
* components.
*
* @param $hex
*   A string specifing an RGB color in the formats:
*   '#ABC','ABC','#ABCD','ABCD','#AABBCC','AABBCC','#AABBCCDD','AABBCCDD'
* @return
*   An array with four elements for red, green, blue, and alpha.
*/
function imageapi_hex2rgba($hex) {
  $hex = ltrim($hex, '#');
  if (preg_match('/^[0-9a-f]{3}$/i', $hex)) {
    // 'FA3' is the same as 'FFAA33' so r=FF, g=AA, b=33
    $r = str_repeat($hex{0}, 2);
    $g = str_repeat($hex{1}, 2);
    $b = str_repeat($hex{2}, 2);
    $a = '0';
  }
  elseif (preg_match('/^[0-9a-f]{6}$/i', $hex)) {
    // #FFAA33 or r=FF, g=AA, b=33
    list($r, $g, $b) = str_split($hex, 2);
    $a = '0';
  }
  elseif (preg_match('/^[0-9a-f]{8}$/i', $hex)) {
    // #FFAA33 or r=FF, g=AA, b=33
    list($r, $g, $b, $a) = str_split($hex, 2);
  }
  elseif (preg_match('/^[0-9a-f]{4}$/i', $hex)) {
    // 'FA37' is the same as 'FFAA3377' so r=FF, g=AA, b=33, a=77
    $r = str_repeat($hex{0}, 2);
    $g = str_repeat($hex{1}, 2);
    $b = str_repeat($hex{2}, 2);
    $a = str_repeat($hex{3}, 2);
  }
  else {
    //error: invalide hex string, TODO: set form error..
    return FALSE;
  }

  $r = hexdec($r);
  $g = hexdec($g);
  $b = hexdec($b);
  $a = hexdec($a);
  return array($r, $g, $b, $a);
}

<?php // $Id:

timbrandin's picture

<?php // $Id: imageapi_gd.module,v 1.13.2.7 2009/04/17 00:15:21 drewish Exp $

/
* @file
* GD2 toolkit functions
*/

/

* Implementation of hook_imageapi_toolkit().
*
* this hook only gets called to see is a module implements the imageapi hooks...
*/
function imageapi_gd_imageapi_toolkit() {
}


/
* Settings form for the toolkit.
*/
function imageapi_gd_settings_form() {
  $form['imageapi_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('imageapi_jpeg_quality', 75),
    '#field_suffix' => '%',
  );
  return system_settings_form($form);
}

/

* Open an image file.
*
* @param $image
*   An image object. The $image->resource value will populated by this call.
* @return
*   TRUE or FALSE, based on success.
*/
function imageapi_gd_image_open($image) {
  $extension = str_replace('jpg', 'jpeg', $image->info['extension']);
  $function = 'imagecreatefrom'. $extension;
  return (function_exists($function) && $image->resource = $function($image->source));
}

/
* Save an image file to a destination.
*
* @param $image
*   An image object.
* @param $destination
*   A string file path where the image should be saved.
* @param $extension
*   A string containing one of the following extensions: gif, jpg, jpeg, png.
* @return
*   TRUE or FALSE, based on success.
*/
function imageapi_gd_image_close($image, $destination) {
  $extension = str_replace('jpg', 'jpeg', $image->info['extension']);
  $function = 'image'. $extension;
  if (!function_exists($function)) {
    return FALSE;
  }
  if ($extension == 'jpeg') {
    return $function($image->resource, $destination, variable_get('imageapi_jpeg_quality', 75));
  }
  else {
    // Always save PNG images with full transparency.
    if ($extension == 'png') {
      imagealphablending($image->resource, FALSE);
      imagesavealpha($image->resource, TRUE);
    }
    return $function($image->resource, $destination);
  }
}

/

* Crop an image using the GD toolkit.
*
* @param $image
*   An image object. The $image->resource, $image->info['width'], and
*   $image->info['height'] values will be modified by this call.
* @param $x
*   The starting x offset at which to start the crop, in pixels.
* @param $y
*   The starting y offset at which to start the crop, in pixels.
* @param $width
*   The width of the cropped area, in pixels.
* @param $height
*   The height of the cropped area, in pixels.
* @return
*   TRUE or FALSE, based on success.
*/
function imageapi_gd_image_crop($image, $x, $y, $width, $height) {
  $res = imageapi_gd_create_tmp($image, $width, $height);

  if (!imagecopyresampled($res, $image->resource, 0, 0, $x, $y, $width, $height, $width, $height)) {
    return FALSE;
  }

  // Destroy the original image and return the modified image.
  imagedestroy($image->resource);
  $image->resource = $res;
  $image->info['width'] = $width;
  $image->info['height'] = $height;
  return TRUE;
}

/
* Scale an image to the specified size using GD.
*
* @param $image
*   An image object. The $image->resource, $image->info['width'], and
*   $image->info['height'] values will be modified by this call.
* @param $width
*   The new width of the resized image, in pixels.
* @param $height
*   The new height of the resized image, in pixels.
* @return
*   TRUE or FALSE, based on success.
*/
function imageapi_gd_image_resize($image, $width, $height) {
  $res = imageapi_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);
  // Update image object.
  $image->resource = $res;
  $image->info['width'] = $width;
  $image->info['height'] = $height;
  return TRUE;
}

/

* Rotate an image the given number of degrees.
*
* @param $image
*   An image object. The $image->resource, $image->info['width'], and
*   $image->info['height'] values will be modified by this call.
* @param $degrees
*   The number of (clockwise) degrees to rotate the image.
* @param $background
*   An hexadecimal integer specifying the background color to use for the
*   uncovered area of the image after the rotation. E.g. 0x000000 for black,
*   0xff00ff for magenta, and 0xffffff for white. For images that support
*   transparency, this will default to transparent. Otherwise it will
*   be white.
* @return
*   TRUE or FALSE, based on success.
*/
function imageapi_gd_image_rotate($image, $degrees, $background) {
  // PHP installations using non-bundled GD do not have imagerotate.
  if (!function_exists('imagerotate')) {
    require_once drupal_get_path('module', 'imageapi_gd') .'/imagerotate.inc';
  }

  $width = $image->info['width'];
  $height = $image->info['height'];

  // Convert the hexadecimal background value to a color index value.
  if (isset($background)) {
    $rgb = array();
    for ($i = 16; $i >= 0; $i -= 8) {
      $rgb[] = (($background >> $i) & 0xFF);
    }
    $background = imagecolorallocatealpha($image->resource, $rgb[0], $rgb[1], $rgb[2], 0);
  }
  // Set the background color as transparent if $background is NULL.
  else {
    // Get the current transparent color.
    $background = imagecolortransparent($image->resource);

    // If no transparent colors, use white.
    if ($background == 0) {
      $background = imagecolorallocatealpha($image->resource, 255, 255, 255, 0);
    }
  }

  // Images are assigned a new color pallete when rotating, removing any
  // transparency flags. For GIF images, keep a record of the transparent color.
  if ($image->info['extension'] == 'gif') {
    $transparent_index = imagecolortransparent($image->resource);
    if ($transparent_index != 0) {
      $transparent_gif_color = imagecolorsforindex($image->resource, $transparent_index);
    }
  }

  $image->resource = imagerotate($image->resource, 360 - $degrees, $background);

  // GIFs need to reassign the transparent color after performing the rotate.
  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 imageapi_gd_image_sharpen($image, $radius, $sigma, $amount, $threshold) {
  $threshold = round($threshold * 255);
  $image->resource = imageapi_gd_unsharp_mask($image->resource, $radius, $sigma, $amount, $threshold);
  return TRUE;
}

/
* Convert an image resource to grayscale.
*
* Note that transparent GIFs loose transparency when desaturated.
*
* @param $image
*   An image object. The $image->resource value will be modified by this call.
* @return
*   TRUE or FALSE, based on success.
*/
function imageapi_gd_image_desaturate($image) {
  // PHP installations using non-bundled GD do not have imagefilter.
  if (!function_exists('imagefilter')) {
    require_once drupal_get_path('module', 'imageapi_gd') .'/imagefilter.inc';
  }

  return imagefilter($image->resource, IMG_FILTER_GRAYSCALE);
}

/

* Create a truecolor image preserving transparency from a provided image.
*
* @param $image
*   An image object.
* @param $width
*   The new width of the new image, in pixels.
* @param $height
*   The new height of the new image, in pixels.
* @return
*   A GD image handle.
*/
function imageapi_gd_create_tmp($image, $width, $height) {
  $res = imagecreatetruecolor($width, $height);

  if ($image->info['extension'] == 'gif') {
    // Grab transparent color index from image resource.
    $transparent = imagecolortransparent($image->resource);

    if ($transparent >= 0) {
      // The original must have a transparent color, allocate to the new image.
      $transparent_color = imagecolorsforindex($image->resource, $transparent);
      $transparent = imagecolorallocate($res, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);

      // Flood with our new transparent color.
      imagefill($res, 0, 0, $transparent);
      imagecolortransparent($res, $transparent);
    }
  }
  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;
}

/**
* $sigma is currently unused for _gd_sharp_mask due to 3x3 convolution matrix limit.
* we should explore properly implementing sigma.
*/
function imageapi_gd_unsharp_mask($img, $radius, $sigma, $amount, $threshold)    {

  //////////////////////////////////////////////////////////////
  ////  
  ////                  Unsharp Mask for PHP - version 2.1.1  
  ////  
  ////    Unsharp mask algorithm by Torstein H�nsi 2003-07.  
  ////             thoensi_at_netcom_dot_no.  
  ////               Please leave this notice.  
  ////  
  //////////////////////////////////////////////////////////////
 
  // http://vikjavev.no/computing/ump.php

  // $img is an image that is already created within php using
  // imgcreatetruecolor. No url! $img must be a truecolor image.

  // Attempt to calibrate the parameters to Photoshop:
  if ($amount > 500) $amount = 500;
  $amount = $amount * 0.016;
  if ($radius > 50) $radius = 50;
  $radius = $radius * 2;
  if ($threshold > 255) $threshold = 255;

  $radius = abs(round($radius)); // Only integers make sense.
  if ($radius == 0) {
    return $img; imagedestroy($img);
    break;
  }

  $w = imagesx($img); $h = imagesy($img);
  $img_canvas = imagecreatetruecolor($w, $h);
  $img_blur = imagecreatetruecolor($w, $h);

  // Gaussian blur matrix:
  //                         
  //    1    2    1         
  //    2    4    2         
  //    1    2    1         
  //                         
  ////////////////////////////////////////////////// 

  $matrix = array(
    array( 1, 2, 1 ),
    array( 2, 4, 2 ),
    array( 1, 2, 1 )
    );

  imagecopy($img_blur, $img, 0, 0, 0, 0, $w, $h);
  imageconvolution($img_blur, $matrix, 16, 0);

  if ($threshold > 0) {
    // Calculate the difference between the blurred pixels and the original
    // and set the pixels
    for ($x = 0; $x < $w-1; $x++)    { // each row
      for ($y = 0; $y < $h; $y++)    { // each pixel

        $rgb_orig = imagecolorat($img, $x, $y);
        $r_orig = (($rgb_orig >> 16) & 0xFF);
        $g_orig = (($rgb_orig >> 8) & 0xFF);
        $b_orig = ($rgb_orig & 0xFF);

        $rgb_blur = imagecolorat($img_blur, $x, $y);

        $r_blur = (($rgb_blur >> 16) & 0xFF);
        $g_blur = (($rgb_blur >> 8) & 0xFF);
        $b_blur = ($rgb_blur & 0xFF);

        // When the masked pixels differ less from the original
        // than the threshold specifies, they are set to their original value.
        $r_new = (abs($r_orig - $r_blur) >= $threshold)
          ? max(0, min(255, ($amount * ($r_orig - $r_blur)) + $r_orig))
          : $r_orig;
        $g_new = (abs($g_orig - $g_blur) >= $threshold)
          ? max(0, min(255, ($amount * ($g_orig - $g_blur)) + $g_orig))
          : $g_orig;
        $b_new = (abs($b_orig - $b_blur) >= $threshold)
          ? max(0, min(255, ($amount * ($b_orig - $b_blur)) + $b_orig))
          : $b_orig;

        if (($r_orig != $r_new) || ($g_orig != $g_new) || ($b_orig != $b_new)) {
          $pix_col = imagecolorallocate($img, $r_new, $g_new, $b_new);
          imagesetpixel($img, $x, $y, $pix_col);
        }
      }
    }
  }
  else{
    for ($x = 0; $x < $w; $x++)    { // each row
      for ($y = 0; $y < $h; $y++)    { // each pixel
        $rgb_orig = imagecolorat($img, $x, $y);
        $r_orig = (($rgb_orig >> 16) & 0xFF);
        $g_orig = (($rgb_orig >> 8) & 0xFF);
        $b_orig = ($rgb_orig & 0xFF);

        $rgb_blur = imagecolorat($img_blur, $x, $y);

        $r_blur = (($rgb_blur >> 16) & 0xFF);
        $g_blur = (($rgb_blur >> 8) & 0xFF);
        $b_blur = ($rgb_blur & 0xFF);

        $r_new = ($amount * ($r_orig - $r_blur)) + $r_orig;
        if ($r_new>255) $r_new=255;
        elseif ($r_new<0) $r_new=0;

        $g_new = ($amount * ($g_orig - $g_blur)) + $g_orig;
        if ($g_new>255) $g_new=255;
        elseif ($g_new<0) $g_new=0;

        $b_new = ($amount * ($b_orig - $b_blur)) + $b_orig;
        if ($b_new>255) $b_new=255;
        elseif ($b_new<0) $b_new=0;

        $rgb_new = ($r_new << 16) + ($g_new <<8) + $b_new;
        imagesetpixel($img, $x, $y, $rgb_new);
      }
    }
  }
  imagedestroy($img_canvas);
  imagedestroy($img_blur);

  return $img;
}

nästan att de blev lite för

timbrandin's picture

nästan att de blev lite för mycket skrivet på en ggn där :P
önskar man kunde bifoga filer!!!!!!
// Tim

Hmmm.. Intressant.. Sidan

Fredde's picture

Hmmm.. Intressant.. Sidan balla ur totalt när jag lade in de filerna... Vit tom sida!

Ska jag ta en titt på det?

timbrandin's picture

Ska jag ta en titt på det?
tim[at]sypreme.se

Sweden

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: