PHP GD没有按一定比例调整图像大小

时间:2014-03-01 05:10:33

标签: php gd

我有下面的脚本,一切正常,除了我在百分比的函数参数中输入一个值,叠加太大,它们应该按最大尺寸的#%缩小。

以下是一个例子:

Meh...

但是如果我在函数调用中将“80”更改为“0”,我会得到:

Fits

如果我改变相同的值让我们说“20”将叠加缩小20%,我只是得到与上面相同的...

有关为什么不按输入%缩放叠加层的任何想法?

<?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;
}
?>

0 个答案:

没有答案