Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP GD未按一定百分比缩小图像大小_Php_Gd - Fatal编程技术网

PHP GD未按一定百分比缩小图像大小

PHP GD未按一定百分比缩小图像大小,php,gd,Php,Gd,我有下面的脚本,一切正常,除了如果我在函数参数中输入一个百分比值,覆盖太大,它们应该按最大大小的%缩小 以下是一个例子: 但是如果我在函数调用中将80改为0,我会得到: 如果我将相同的值改为20,将覆盖层缩小20%,我只会得到与上面相同的结果 你知道为什么它没有按输入的%缩小覆盖吗 <?php session_start(); //Set the content-type header('Content-Type: image/png'); //Overlay URLS $overlay

我有下面的脚本,一切正常,除了如果我在函数参数中输入一个百分比值,覆盖太大,它们应该按最大大小的%缩小

以下是一个例子:

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

如果我将相同的值改为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;
}
?>

第一张图片看起来像是增加了图片的大小,并将其放置在相同大小的图片框/背景图片上。我首先创建一个空白画布,它将接受alpha并将其设置为URL变量中定义的大小。然后我将水印加载到内存中,再次创建一个truecolor图像来保存alpha并将其合并在一起。我在哪里增加@HorseSMith的大小?你真的应该自己调试这个脚本。这可以通过注释header函数和imagepng函数来实现,只需打印出数字,看看它们是否是您认为应该的数字,如果不是,则找出它们不是的原因