Baran Topal

Baran Topal


October 2024
M T W T F S S
« Feb    
 123456
78910111213
14151617181920
21222324252627
28293031  

Categories


putting an avatar in the corner of another image in PHP

baranbaran

This is for fun and a gem if needed.


<?php
error_reporting(E_ALL);

// LOCATION OF THE IMAGES (COULD COME IN FROM $_GET URL STRING)
$original = 'path/to/image.png';
$overlay  = 'path/to/another/image.png';

// READ THE IMAGE AND THE OVERLAY FILE
// MAN PAGE http://php.net/manual/en/function.imagecreatefrompng.php
$im = ImageCreateFromPNG($original);
$wm = ImageCreateFromPNG($overlay);

// ADD OVERLAY USING LOCAL FUNCTION
imagelogo($im, $wm, imagesx($im), imagesy($im), imagesx($wm), imagesy($wm));

// SHOW THE IMAGE
header('Content-type: image/png');
ImagePNG($im);

// A FUNCTION TO ADD THE OVERLAY
// SEE http://php.net/manual/en/function.imagealphablending.php#77085
function imagelogo (&$dst_image, $src_image, $dst_w, $dst_h, $src_w, $src_h)
{
    ImageAlphaBlending($dst_image,TRUE);
    ImageAlphaBlending($src_image,TRUE);
    ImageCopy
    ( $dst_image
    , $src_image
    , 0
    , ($dst_h-$src_h)
    , 0
    , 0
    , $src_w
    , $src_h
    )
    ;
}

Nice? 🙂