ImageAPI patch/update to remove black background on generated images

source : https://drupal.org/node/422836

Here is the complete function from imageapi_gd.module with the FIX in.

The fix resolves the issue when creating images a black background gets generated on scale + crop - change the rgb values to whatever you what the bg color to be ;)

Hope this helps someone.

/**
 * 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;
  }

// _mR
imagefill($res, 0, 0, imagecolorallocate($res, 255, 255, 255));
$new = imageapi_gd_create_tmp($image, $width, $height);
imagecopy($new, $res, 0, 0, 0, 0, ($width + $x), ($height + $y));
$res = $new;
// _mR

  // 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;
}




Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.
Bg