1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to generate multiple image from single template using php

Discussion in 'PHP' started by neilfurry, Aug 9, 2022.

  1. #1
    Hi,

    I need some help please. here is what i want to do:

    1. im making a script which will generate bulk id card populating the data from the database and displaying data to a single template and will generate that id cards in return.

    here is my script:
    
         $sql = mysqli_query($con, "SELECT * FROM employees");
    
         header('Content-type: image/png');
         $image = imagecreatefrompng('id-template.png');
         $color = imagecolorallocate($image, 0, 0, 0);
          imagepng($image); 
       
         while($rs = mysqli_fetch_assoc($sql)){
    
                   imagettftext($image, 13, 0, 60,117, $color, $font, $rs['empname']); //employee name location of the template
            
                   $save  = "id/".strtolower($rs['id']).".png";
                  
                   imagepng($image, $save, 9, PNG_NO_FILTER);
     
         }
    PHP:
    This produces id card, but the data passed to each id card overlaps with the other.

    Please help me fix this problem.

    Thank you
     
    Last edited by a moderator: Aug 10, 2022
    neilfurry, Aug 9, 2022 IP
  2. Efetobor Agbontaen

    Efetobor Agbontaen Active Member

    Messages:
    136
    Likes Received:
    41
    Best Answers:
    5
    Trophy Points:
    85
    #2
    It would have been nice to see a screenshot of what you mean. And also to put your code in code tags.

    However, just looking at the code, I see the most likely cause.
    The problem comes from here.
    imagettftext($image, 13, 0, 60,117, $color, $font, $rs['empname']);
    PHP:
    The imagettftext function writes text to an image starting from a certain x and y coordinate.
    Meaning that parameter 4 (in your code, 60) or parameter 5(in your code, 117) should change during each iteration depending on how you want the final out put to be. Those are the x and y coordinates respectively.

    For example, do this if you want the bulk id cards to line up vertically:
    $yCord = 117;
    while($rs = mysqli_fetch_assoc($sql)){
    
    imagettftext($image, 13, 0, 60, $yCord, $color, $font, $rs['empname']); //employee name location of the template
    
    $save = "id/".strtolower($rs['id']).".png";
    
    imagepng($image, $save, 9, PNG_NO_FILTER);
    
    /*
    Add to $yCord variable here.
    */
    $yCord += 200; //Adjust it until it looks perfect
    }
    PHP:
    Hope that helps.
    Please share a screenshot so we can be sure of exactly how you want it to appear
     
    Last edited: Aug 10, 2022
    Efetobor Agbontaen, Aug 10, 2022 IP
  3. sarahk

    sarahk iTamer Staff

    Messages:
    28,498
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #3
    after you save the card you need to re-create the variable so that it's fresh. Your line 15 (currently blank) should be

    $image = imagecreatefrompng('id-template.png');
    PHP:
     
    sarahk, Aug 10, 2022 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #4
    What do ID cards have to do with generating a composite IMAGE. Can't you just do the layout in the markup, or is this something you need as a single file for people to save?

    Generally when using web technologies? Using images to display text is usability and accessibility shit. And unreliable, and poor scaling depending on the media target.

    To me this doesn't look like anything that should be done server-side in the first place.
     
    deathshadow, Aug 11, 2022 IP
  5. sarahk

    sarahk iTamer Staff

    Messages:
    28,498
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #5
    There's that too!
     
    sarahk, Aug 11, 2022 IP