Sara
Sara

Reputation: 133

Place image in another image with PHP

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!

Upvotes: 5

Views: 10318

Answers (4)

Gabriel Spiteri
Gabriel Spiteri

Reputation: 4978

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);

Upvotes: 8

Emyr
Emyr

Reputation: 2371

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

Upvotes: 0

inquam
inquam

Reputation: 12932

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

Upvotes: 0

Shadikka
Shadikka

Reputation: 4276

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

Upvotes: 1

Related Questions