-1

I have to create dynamic student card image. add put this image object in student profile photo. But, student image color are changed.

How to put student profile photo with original color?

Here is my code:

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);

Header Img:

enter image description here

Footer Img:

enter image description here

Profile Img:

enter image description here

Here is my output image:

enter image description here

Bharat Chauhan
  • 3,204
  • 5
  • 39
  • 52

1 Answers1

1

Main problem is that your $im needs to be a true color image as well. Secondly, you have to actually fill your background. You can also skip creating $thumb and copyresized directly into your $im.

Heres a working verison (i changed your paths to test it on my machine)

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

Keep in mind that your profile picture current gets distorted though, you may wan't to either make sure the profile pictures always have the correct aspect ratio or you may want to crop the image. See here for more details: PHP crop image to fix width and height without losing dimension ratio

Fitzi
  • 1,641
  • 11
  • 17
  • 1
    You're welcome. If my answer solved your problem, please consider accepting it. https://stackoverflow.com/help/someone-answers – Fitzi Feb 14 '19 at 10:25