Php 显示前调整最终图像的大小

Php 显示前调整最终图像的大小,php,Php,我已经创建了一个PHP脚本来生成一个化身。当我显示图像时,尺寸为250px 250px。我想在最后调整头像的大小(当所有的修改都完成后),然后再显示它,但我找不到功能。 这是我的代码: if(!empty($_GET['pseudo'])) { $query1 = $bdd->prepare("SELECT username, user_id, VID FROM phpbb_users LEFT JOIN 0php_users ON 0php_users.phpbb_id = ph

我已经创建了一个PHP脚本来生成一个化身。当我显示图像时,尺寸为250px 250px。我想在最后调整头像的大小(当所有的修改都完成后),然后再显示它,但我找不到功能。 这是我的代码:

if(!empty($_GET['pseudo']))
{
    $query1 = $bdd->prepare("SELECT username, user_id, VID FROM phpbb_users LEFT JOIN 0php_users ON 0php_users.phpbb_id = phpbb_users.user_id WHERE username_clean = ?");
    $query1->execute(array(strtolower($_GET['pseudo'])));
    if($query1->rowCount() == 1)
    {
    $data1 = $query1->fetch();

    $query2 = $bdd->prepare("SELECT nom FROM phpbb_user_group
                            RIGHT JOIN 0php_hubs ON phpbb_user_group.group_id = 0php_hubs.id_groupe
                            WHERE user_id = ?");
    $query2->execute(array($data1['user_id']));
    $data2 = $query2->fetch();

    $image = imagecreatefrompng("avatar.png");

    $background = imagecolorallocate($image, 0, 0, 0);
    imagecolortransparent($image, $background);
    imagealphablending($image, false);
    imagesavealpha($image, true);
    if(($_GET['param']=='ivao')&&(!empty($data1['VID'])))
    {
        $BoolIvao = true;
        $ivao = imagecreatefrompng("http://status.ivao.aero/R/".$data1['VID'].".png");
        imagecopy($image, $ivao, 87, 173, 0, 0, 150, 30);
    }

    $couleur = imagecolorallocate($image, 0, 0, 0);
    $largeur_source = imagesx($image); 
    $fontfile = 'calibri.ttf';
    $angle = 0;
    $police = 18;
    $text_size = imagettfbbox($police, $angle, $fontfile, 'Hub de '.$data2['nom']); 
    $text_size2 = imagettfbbox($police, $angle, $fontfile, $data1['username']); 
    $text_width = (($text_size[2] + $text_size[4]) / 2) - (($text_size[0] + $text_size[6]) / 2);
    $text_width2 = (($text_size2[2] + $text_size2[4]) / 2) - (($text_size2[0] + $text_size2[6]) / 2);
    $x = ($largeur_source - $text_width)/2;
    $x2 = (176 - $text_width2)/2 + 74;
    $y2 = ($BoolIvao == true)?160:175;
    imagettftext($image, $police, $angle, $x2, $y2, $couleur, $fontfile, $data1['username']);
    imagettftext($image, $police, $angle, $x, 35, $couleur, $fontfile, 'Hub de '.$data2['nom']);
    imagepng($image); 
    $query2->closeCursor();
    }
    $query1->closeCursor();

}

谢谢

您需要使用
imagecopyresampled
来调整图像大小

<?php
if(!empty($_GET['pseudo']))
{
    $query1 = $bdd->prepare("SELECT username, user_id, VID FROM phpbb_users LEFT JOIN 0php_users ON 0php_users.phpbb_id = phpbb_users.user_id WHERE username_clean = ?");
    $query1->execute(array(strtolower($_GET['pseudo'])));
    if($query1->rowCount() == 1)
    {
        $data1 = $query1->fetch();

        $query2 = $bdd->prepare("SELECT nom FROM phpbb_user_group
                        RIGHT JOIN 0php_hubs ON phpbb_user_group.group_id = 0php_hubs.id_groupe
                        WHERE user_id = ?");
            $query2->execute(array($data1['user_id']));
            $data2 = $query2->fetch();

            $image = imagecreatefrompng("avatar.png");

        list($width, $height) = getimagesize("avatar.png"); # get dimensions

        $background = imagecolorallocate($image, 0, 0, 0);
        imagecolortransparent($image, $background);
        imagealphablending($image, false);
        imagesavealpha($image, true);
        if(($_GET['param']=='ivao')&&(!empty($data1['VID'])))
        {
            $BoolIvao = true;
            $ivao = imagecreatefrompng("http://status.ivao.aero/R/".$data1['VID'].".png");
            imagecopy($image, $ivao, 87, 173, 0, 0, 150, 30);
        }

        $couleur = imagecolorallocate($image, 0, 0, 0);
        $largeur_source = imagesx($image);
        $fontfile = 'calibri.ttf';
        $angle = 0;
        $police = 18;
        $text_size = imagettfbbox($police, $angle, $fontfile, 'Hub de '.$data2['nom']);
        $text_size2 = imagettfbbox($police, $angle, $fontfile, $data1['username']);
        $text_width = (($text_size[2] + $text_size[4]) / 2) - (($text_size[0] + $text_size[6]) / 2);
        $text_width2 = (($text_size2[2] + $text_size2[4]) / 2) - (($text_size2[0] + $text_size2[6]) / 2);
        $x = ($largeur_source - $text_width)/2;
        $x2 = (176 - $text_width2)/2 + 74;
        $y2 = ($BoolIvao == true)?160:175;
        imagettftext($image, $police, $angle, $x2, $y2, $couleur, $fontfile, $data1['username']);
        imagettftext($image, $police, $angle, $x, 35, $couleur, $fontfile, 'Hub de '.$data2['nom']);

        /* resize image */
        $small_im = imagecreatetruecolor(250, 250);
        imagecopyresampled($small_im, $image, 0, 0, 0, 0, 250, 250, $width, $height);
        /* resize image */

        imagepng($small_im);
        $query2->closeCursor();
    }
    $query1->closeCursor();
}

这是我经常使用的代码,通过PHP调整图像大小(我希望它能帮助您)祝您好运:
注意:
$filetemp
指文件的临时名称。
此外,在这段代码中,我将图像保存为
png
,您可以将其更改为所需的扩展名

<?php
    $temporary = pathinfo($filename, PATHINFO_FILENAME); 
    $new_images = $temporary.".png";
    $width=60; //*** Fix Width & Heigh (Autu caculate) ***//
    $size=GetimageSize($filetemp);
    $height=round($width*$size[1]/$size[0]);
    $images_orig = ImageCreateFromJPEG($filetemp);
    $photoX = ImagesX($images_orig);
    $photoY = ImagesY($images_orig);
    $images_fin = ImageCreateTrueColor($width, $height);
    ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
    imagepng($images_fin,"YourFolder/".$new_images);
    ImageDestroy($images_orig);
    ImageDestroy($images_fin);

?>

RTFM?是的,这就是我想要的功能。谢谢