tags:

views:

742

answers:

4

Hello,

Is there a way to put text on a png and then merge it wit a jpg / gif image?

+2  A: 

you can draw any text in or on image with gd using ttf or type1 fonts ( if its enabled in gd in php).

http://hr.php.net/manual/en/image.examples-watermark.php

deresh
A: 

You don't say what language you're using, but Imagemagick has APIs in a few different languages (http://www.imagemagick.org/script/api.php).

edoloughlin
They mention PHP in the tags
St. John Johnson
+1  A: 

Here's how i do it.

/* load the image */
$im = imagecreatefrompng("image.png");

/* black for the text */
$black = imagecolorallocate($im, 0, 0, 0);

/* put the text on the image */
imagettftext($im, 12, 0, 0, 0, $black, "arial.ttf", "Hello World");

/* load the jpg */
$jpeg = imagecreatefromjpeg("image.jpeg");

/* put the png onto the jpeg */
/* you can get the height and width with getimagesize() */
imagecopyresampled($jpeg,$im, 0, 0, 0, 0, $jpeg_width, $jpeg_height, $im_width, $im_height);

/* save the image */
imagejpeg($jpeg, "result.jpeg", 100);

This is a pretty simple example though.

Ólafur Waage
A: 

Yes there is. Assuming that you are using PHP and GD library is available, you can use the code example at:

Watermark Your Images with another Image Using PHP and GD Library

The code would look something similar to this:

define( 'WATERMARK_OVERLAY_IMAGE', 'watermark.png' );
define( 'WATERMARK_OVERLAY_OPACITY', 50 );
define( 'WATERMARK_OUTPUT_QUALITY', 90 );

function create_watermark( $source_file_path, $output_file_path )
{
 list( $source_width, $source_height, $source_type ) = getimagesize( $source_file_path );

 if ( $source_type === NULL )
 {
  return false;
 }

 switch ( $source_type )
 {
  case IMAGETYPE_GIF:
   $source_gd_image = imagecreatefromgif( $source_file_path );
   break;
  case IMAGETYPE_JPEG:
   $source_gd_image = imagecreatefromjpeg( $source_file_path );
   break;
  case IMAGETYPE_PNG:
   $source_gd_image = imagecreatefrompng( $source_file_path );
   break;
  default:
   return false;
 }

 $overlay_gd_image = imagecreatefrompng( WATERMARK_OVERLAY_IMAGE );
 $overlay_width = imagesx( $overlay_gd_image );
 $overlay_height = imagesy( $overlay_gd_image );

 imagecopymerge(
  $source_gd_image,
  $overlay_gd_image,
  $source_width - $overlay_width,
  $source_height - $overlay_height,
  0,
  0,
  $overlay_width,
  $overlay_height,
  WATERMARK_OVERLAY_OPACITY
 );

 imagejpeg( $source_gd_image, $output_file_path, WATERMARK_OUTPUT_QUALITY );

 imagedestroy( $source_gd_image );
 imagedestroy( $overlay_gd_image );
}

The watermark.png file can be any image including transparent 8-bit PNG images. However, 24-bit transparent PNG images might not work as expected.

Salman A