1

I have the below script, it all works fine, apart from if I enter a value into the function arguments for the percentage, the overlay's are too large, they should scale down by #% of the max size.

Here is an example:

Meh...

But then if I change the "80" to "0" in the function call I get this:

Fits

If I change the same value to lets say "20" to scale the overlay down by 20%, I simply get the same as above...

Any ideas as to why it isn't scaling the overlay down by inputted %?

<?php
session_start();
//Set the content-type
header('Content-Type: image/png');

//Overlay URLS
$overlay_url = $_SESSION['ROOT_PATH']."images/logos/overlay.png";
$logo_url = $_SESSION['ROOT_PATH']."images/logos/".$_GET['logo'].".png";

//How much of the image will the overlays take up
$logo_percent = 0.60;
$overlay_percent = 0.90;

//Get the image size first
$size = explode(",",$_GET['size']);
$width = (int)$size[0];
$height = (int)$size[1];

$background = imagecreatetruecolor($width,$height);
$bg_color = imagecolorallocate($background,20,100,255);
imagefill($background, 0, 0, $bg_color);

//Apply company logo first
$company_logo = apply_watermark($background,$overlay_url,80,0,0,90);
//Now venue logo
imagepng(apply_watermark($company_logo,$logo_url,80,1,0,90));

//background(url/resource), watermarl(url), size percent, left0/right1, padding(px),rotate
function apply_watermark($bg,$wt,$p,$lr,$pad=0,$rt=false) {   
    //Load background image into memory,can apply more watermarks to same image
    if (gettype($bg) == "resource") {
        $background = $bg;
    } else {
        $background = imagecreatefromjpeg($bg);
    }

    //Get the width and height and generate watermark max size
    $bx = imagesx($background);
    $by = imagesy($background);
    $overlay_max = (($bx > $by) ? $bx : $by) / 100 * $p;

    //Create container for image
    $imagecontainer = imagecreatetruecolor($bx,$by);

    //Allow alpha channels to be saved and fill it with alpha
    imagesavealpha($imagecontainer,true);
    $alphacolor = imagecolorallocatealpha($imagecontainer,0,0,0,127);
    imagefill($imagecontainer,0,0,$alphacolor);

    //Copy background image into the container
    imagecopyresampled($imagecontainer,$background,0,0,0,0,$bx,$by,$bx,$by);

    //Load the watermark
    $overlay = imagecreatefrompng($wt);
    if($rt != false){$overlay = imagerotate($overlay,$rt,0);}

    //get the watermark width and height and generate the aspect ratio
    $ratio = $overlay_max / imagesx($overlay);
    if ($ratio > ($bx / $by)) {
        $scale = imagesx($overlay) / $bx;
    } else {
        $scale = imagesy($overlay) / $by;
    }
    $newwidth = (int)(imagesx($overlay) / $scale);
    $newheight = (int)(imagesy($overlay) / $scale);

    //Create container for the watermark and apply alpha to it
    $newoverlay = imagecreatetruecolor($newwidth,$newheight);
    imagesavealpha($newoverlay,true);
    imagefill($newoverlay,0,0,$alphacolor);

    //Copy the watermark to the watermark container with alpha
    imagecopyresized($newoverlay, $overlay, 0, 0, 0, 0, $newwidth, $newheight, imagesx($overlay), imagesy($overlay));

    //Copy the watermark to the background image container, choose left or right
    if ($lr == 0) {
        imagecopyresampled($imagecontainer,$newoverlay,0+$pad,($by-$newheight-$pad),0,0,$newwidth,$newheight,$newwidth,$newheight);
    } elseif ($lr == 1) {
        imagecopyresampled($imagecontainer,$newoverlay,($bx-$newwidth-$pad),($by-$newheight-$pad),0,0,$newwidth,$newheight,$newwidth,$newheight);
    }

    //Return the generated image back to the function call to further handle
    return $imagecontainer;
}
?>
Martyn Ball
  • 4,679
  • 8
  • 56
  • 126
  • The first image looked as if you had increased the size of the image and placed it onto the same sized "image frame"/"background image". – Horse SMith Mar 01 '14 at 05:37
  • Well I first create a blank canvas which will accept alpha and set it to the size defined in the URL variables. Then I load the watermark into memory and again create a truecolor image to save the alpha and merge it all together. Where about am I increasing the size @HorseSMith? – Martyn Ball Mar 03 '14 at 12:49
  • You really should try to debug this script yourself. This you can do by commenting out the header function and the imagepng function and just print out numbers to see if they are what you think they should be, and figure out why they aren't if they aren't – Horse SMith Mar 04 '14 at 03:58

0 Answers0