在php中使用imagecreate更改图像颜色

在php中使用imagecreate更改图像颜色,php,php-gd,Php,Php Gd,我必须创建动态学生卡图像。添加并将此图像对象放入学生档案照片中。但是,学生图像的颜色发生了变化 如何将学生档案照片与原色搭配 这是我的密码: header("Content-Type: image/jpeg"); $im = @imagecreate(602, 980) or die("Cannot Initialize new GD image stream"); $background_color = imagecolorallocate($im, 255, 255, 255); $car

我必须创建动态学生卡图像。添加并将此图像对象放入学生档案照片中。但是,学生图像的颜色发生了变化

如何将学生档案照片与原色搭配

这是我的密码:

header("Content-Type: image/jpeg");
$im = @imagecreate(602, 980)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 255, 255, 255);

$card_header = imagecreatefromjpeg('img/card/card-header.jpg');
imagecopy($im, $card_header, 0, 0, 0, 0, 602, 253);

$card_footer = imagecreatefromjpeg('img/card/card-footer.jpg');
imagecopy($im, $card_footer, 0, 834, 0, 0, 602, 146);

$student_photo = 'img/card/girls-profile.jpg'; //imagecreatefromjpeg($studentlist[0]->getCardPhoto());
// Get new sizes
list($width, $height) = getimagesize($student_photo);
$newwidth = 180;
$newheight = 220;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($student_photo);

// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

imagecopy($im, $thumb, 220, 220, 0, 0, $newwidth, $newheight);

imagejpeg($im, "uploads/card/test.jpeg");
imagedestroy($im);
标题Img:

页脚Img:

个人资料Img:

这是我的输出图像:


主要问题是您的
$im
也需要是真彩色图像。 其次,你必须实际填写你的背景。 您还可以跳过创建
$thumb
,直接将复制大小调整到
$im

这是一个正在运行的verison(我更改了您的路径,在我的机器上进行测试)


不客气。如果我的回答解决了你的问题,请考虑接受它。
<?php

    header('Content-Type: image/jpeg');

    $im = @imagecreatetruecolor(602, 980) // you want to create a truecolorimage here
    or die("Cannot Initialize new GD image stream");

    $background_color = imagecolorallocate($im, 255, 255, 255);
    imagefill($im, 0, 0, $background_color); // you have to actually use the allocated background color

    $card_header = imagecreatefromjpeg('card-header.jpg');
    imagecopy($im, $card_header, 0, 0, 0, 0, 602, 253);

    $card_footer = imagecreatefromjpeg('card-footer.jpg');
    imagecopy($im, $card_footer, 0, 834, 0, 0, 602, 146);

    $student_photo = 'girls-profile.jpg';

    // Get new sizes
    list($width, $height) = getimagesize($student_photo);
    $newwidth = 180;
    $newheight = 220;

    // Load
    //$thumb = imagecreatetruecolor($newwidth, $newheight); // you can skip allocating extra memory for a intermediate thumb
    $source = imagecreatefromjpeg($student_photo);

    // Resize
    imagecopyresized($im, $source, 220, 220, 0, 0, $newwidth, $newheight, $width, $height); // and copy the thumb directly

    imagejpeg($im);

    imagedestroy($im);
    // you should also destroy the other images
    imagedestroy($card_header);
    imagedestroy($card_footer);
    imagedestroy($source);