5

I have an image on which the text is being dynamically generated via a php form. I have the location of a logo image saved to a variable from a mysql database. Is there a way to take this image and apply it to a fixed position in the image? If need be, it will have to be resized smaller to fit in this image area.

I already have a script that looks like this:

        $img = imagecreatefromjpeg('coupon/coupontemplate.jpg');
        $textColor = imagecolorallocate($img, 0, 0, 0); // black text

        //Write first the coupon title
        imagefttext($img, 16, 0, 20, 34, $textColor, 'coupon/arialbd.ttf', $title);

        //Then write the coupon description
        imagettftextbox($img, 13, 0, 20, 45, $textColor, 'coupon/arial.ttf', $description, 440);
        //If the checkbox to include logo is checked...
        if ($_POST['clogo'] == 'y') {

            $logo = $row['imagecompany'];
            $logo_file = "../directory/memberimages/$logo";
            $logo_file_type = getimagesize($logo_file);

            if ($logo_file_type['mime'] == 'image/jpg') {
                $logoImage = imagecreatefromjpeg($logo_file);
            } else if ($logo_file_type['mime'] == 'image/gif') {
                $logoImage = imagecreatefromgif($logo_file);
            } else if ($logo_file_type['mime'] == 'image/png') {
                $logoImage = imagecreatefrompng($logo_file);
            }

            }

        // Output image to the browser
        //header('Content-Type: image/jpeg');
        //imagejpeg($img);

        // Or save to file
        imagejpeg($img, 'my-text.jpg');
        imagedestroy($img);

    }
//}

Anyone have ideas how to do this -- get the image from the specified location and put it on the other image? Thanks!

Sara
  • 133
  • 5
  • 11
  • means u want to put logo on each generated image? image on an image. right?? – xkeshav May 26 '11 at 08:26
  • 1
    Incidentally, the general name for doing this to an image is "watermarking" -- that might help you [find more resources](http://www.google.co.uk/search?sourceid=chrome&ie=UTF-8&q=php+image+watermarking). – Matt Gibson May 26 '11 at 08:53

4 Answers4

8

Merging images can be done something like this: reference

<?php
 # If you don't know the type of image you are using as your originals.
 $image = imagecreatefromstring(file_get_contents($your_original_image);
 $frame = imagecreatefromstring(file_get_contents($your_frame_image));

 # If you know your originals are of type PNG.
 $image = imagecreatefrompng($your_original_image);
 $frame = imagecreatefrompng($your_frame_image);

 imagecopymerge($image, $frame, 0, 0, 0, 0, 50, 50, 100);

 # Save the image to a file
 imagepng($image, '/path/to/save/image.png');

 # Output straight to the browser.
 imagepng($image);
Community
  • 1
  • 1
Gabriel Spiteri
  • 4,896
  • 12
  • 43
  • 58
  • Do you know if the frame could be the already generated image in progress that I have? the $img variable? – Sara May 26 '11 at 08:49
  • yeah sure if its already in your file system you can: if your using the imagecreatefromstring function pass this file_get_contents($src) ... $src can be the path to your already generated image. Otherwise if you already know the image type of your already generated file you can use the imagecreatefrompng/jpeg/gif function and again pass the path/url to your already generated image – Gabriel Spiteri May 26 '11 at 08:55
1

I think you're looking for either imagecopyresampled() or imagecopyresized().

Shadikka
  • 4,116
  • 2
  • 16
  • 19
0

You could use the imagecopy function of. A quick search revealed this post that might probably help you out.

http://forums.bit-tech.net/showthread.php?t=117270

inquam
  • 12,664
  • 15
  • 61
  • 101
0

The function you want is imagecopy:

bool imagecopy ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h )

http://php.net/manual/en/function.imagecopy.php

Emyr
  • 2,351
  • 18
  • 38