Search code examples
phpdrupaldrupal-6drupal-modules

Use imageapi_image_overlay properly?


I want to use imagecache Actions module to create image overlays. The user should upload one image and I want to scale and crop it and then overlay a transparent PNG over it.

I have installed drupal 6 and ImageCache and ImageCache Actions modules. ImageCache Actions defines the imageapi_image_overlay function.

I have created a ImageCache Preset with the name 590x160_Newsletter to scale and crop the image.

Image Tool is GD

Heres what I want to do: User uploads an image. This Image is scaled and cropped with the ImageCache preset. Then I want to overlay it with an image (PNG with transparency). I cannot choose this to be in the preset, because it depends on some other settings in the node, which overlay image I want to use.

The scale and crop does work good, but the $image2 after calling imageapi_image_overlay is still the same (same path, same image) though it says 'success'. But it should be altered like the API reference says

Here is a test code

/* PHP */
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

print "<body style=\"background: lightgreen\">";
$path = "path/to/source/image.jpg";
$opath = "path/to/overlay/image.png";
print '<xmp>';
$image = imageapi_image_open($path);
print '$image: ' . print_r($image,1) . "\n";

$image2 =imageapi_image_open(imagecache_create_path('590x160_Newsletter',  $path));
print '$image2: ' . print_r($image2,1) . "\n";

$overlay = imageapi_image_open($opath);
print imageapi_image_overlay($image2, $overlay, 0, 0, 100, TRUE) ? "success\n" : "failure\n";

print '$image2 after: ' . print_r($image2,1) . "\n";
print '$overlay: ' . print_r($overlay,1) . "\n";
print '</xmp>';
print "<img src=\"$image->source\" />";
print "<img src=\"$image2->source\" />";
print "<img src=\"$overlay->source\" />";

Solution

  • I found out:

    You have to save the image for yourself:

    The Object $image2 has two significant elements: $image2->source which is the filename of the original image and $image2->resource which is the file resource, that is the PHP image resource. This resource is altered in the process, but not saved to disk.

    imagejpeg ($image2->resource, $images2->source); will save the file as JPG under the same name.