Position a logo on another image

5

I am having the user upload an image of a logo and then try to position it on another image that I have on the server.

The position where the receipt will be superimposed as parameters by POST. The logo I receive is always a PNG, which may or may not have a transparent background and the same background must be maintained.

The problem: I do not know why I create black spaces, can you tell me what mistake I made?

Code :

session_start();
$posiX =    $_POST['x'];
$posiY = $_POST['y'];

$datoImg = $_SESSION['genImg'];

$logo = $datoImg['logo']; //'logo.png'
$fondo = 'original.jpg';

foto($fondo, $logo, $posiX, $posiY);

function foto($img_original, $marcadeagua, $posiX, $posiY){
    $trozosimagenorig=explode(".",$img_original);
    $extensionimagenorig=$trozosimagenorig[count($trozosimagenorig)-1];
    if (preg_match("/jpg|jpeg|JPG|JPEG/", $extensionimagenorig)) {
        $imgm=imagecreatefromjpeg($img_original);
    }
    if (preg_match("/png|PNG/", $extensionimagenorig)) {
        $imgm=imagecreatefrompng($img_original);
    }
    if (preg_match("/gif|GIF/", $extensionimagenorig)) {
        $imgm=imagecreatefromgif($img_original);
    }

    $marcadeagua= imagecreatefrompng($marcadeagua);

    $xmarcaagua = $posiX;
    $ymarcaagua = $posiY;
    $ximagen= imagesx($imgm);
    $yimagen=imagesy($imgm);

    imagecopy($imgm, $marcadeagua, $xmarcaagua, $ymarcaagua,
     0, 0, $xmarcaagua, $ymarcaagua);
    //se copia la imagen
    imagejpeg($imgm,'../assets/originalrand.jpg');
}

Result obtained: Create those black spaces:

Original images:

Background image:

Logo image:

    
asked by Albert Arias 17.05.2017 в 20:36
source

1 answer

5

The truth that you almost had it fixed. To copy one image over another, use (pay attention to the order of each argument):

bool imagecopy ( resource $dst_im , resource $src_im , 
                      int $dst_x  , int $dst_y , 
                      int $src_x  , int $src_y , 
                      int $src_w  , int $src_h )

I will separate the arguments so that it is read better. The first line takes the image resources (usually generated with imagecreatefrom***() .) Then the positions in the destination where the image will be located ( $dst_x , $dst_y ), the positions from where it will take from the origin ( $src_x , $src_y ), and the dimensions to take from the origin ( $src_w , $src_h ).

Having this clear, the rest is to locate those values (comment in the code).


Code

<?php

//cambio los valores a unos fijos como prueba para que quede más claro
$posiX = 400;
$posiY = 150;
$logo = 'https://i.stack.imgur.com/g1TyX.png';
$fondo = 'https://i.stack.imgur.com/ChYVl.jpg';

foto($fondo, $logo, $posiX, $posiY);

function foto($img_original, $marcadeagua, $posiX, $posiY){
    //Crear el destino (fondo)
    if (preg_match("/\.jpe?g$/i", $img_original)) { //simplifiqué el regex
        $imgm = imagecreatefromjpeg($img_original);
    }
    elseif (preg_match("/\.png$/i", $img_original)) {
        $imgm = imagecreatefrompng($img_original);
    }
    elseif (preg_match("/\.gif$/i", $img_original)) {
        $imgm = imagecreatefromgif($img_original);
    }

    //tomar el origen (logo)
    $marcadeagua= imagecreatefrompng($marcadeagua);

    //las posiciones en donde ubicar - se reciben por POST (hardcoddeadas en este ejemplo)
    $xmarcaagua = $posiX;
    $ymarcaagua = $posiY;
    //se obtiene el ancho y el largo del logo
    $ximagen = imagesx($marcadeagua);
    $yimagen = imagesy($marcadeagua);

    //COPIAR (observar las variables que se usan)
    imagecopy($imgm, $marcadeagua, 
              $xmarcaagua, $ymarcaagua,
              0, 0,
              $ximagen, $yimagen);


    //Generar el archivo
    imagejpeg($imgm, '../assets/originalrand.jpg');


    //faltaba destruirla (hay que ser prolijos)
    imagedestroy( $imgm );
}


Result

I uploaded an example running on a free hosting: link


Extras:

  • Serve the image

    If instead of generating a file, you would like PHP to serve the image:

    header( "Content-type: image/jpeg" );
    imagejpeg($imgm);
    
  • Generate transparent background in the logo if you do not have it

    Let's say the logo has a white background. If we wanted all the white to be taken as transparent:

    //tomar el origen (logo)
    $marcadeagua= imagecreatefrompng($marcadeagua);
    
    // asignar un recurso con color blanco
    $fondoBlanco = imagecolorallocate($marcadeagua, 255, 255, 255);
    // Hacer que el blanco sea transparente
    imagecolortransparent($marcadeagua, $fondoBlanco);
    
    // y luego lo copiamos sobre la otra imagen ......
    
  • answered by 18.05.2017 / 00:26
    source