Problem with imagecopy () throws error

1

I have this function but when I run it, it throws me this error:

  

imagedestroy (): supplied argument is not a valid Image resource

     

imagecopy (): supplied argument is not a valid Image resource

     

imagejpeg (): supplied argument is not a valid Image resource

<?php 
$posiX=400;
$posiY=150;

$logo="http://dominio.com/images/ejemplo.png";
 $fondo"http://dominio.com/images/ejemplo.jpg"; 
foto($fondo, $logo, $posiX, $posiY);

function foto($img_original, $marcadeagua, $posiX, $posiY){


list($old_x, $old_y, $type) = getimagesize($img_original);
    switch ($type)
    {
        case 'jpg':   //   gif -> jpg
            $src_img = imagecreatefromjpg($img_original);
            break;

        case 'jpeg':   //   jpeg -> jpg
            $src_img = imagecreatefromjpeg($img_original); 
            break;

        case 'png':  //   png -> jpg
            $src_img = imagecreatefrompng($img_original);
            break;
    }

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

//las posiciones en donde ubicar - se reciben por POST
$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($src_img, $marcadeagua, $xmarcaagua, $ymarcaagua,0, 0,
          $ximagen, $yimagen); // linea donde se genera el error imagecopy()


//Generar el archivo
imagejpeg($src_img, '../images/originalrand.jpg');


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


 ?>

What's wrong? I do not understand what I'm doing wrong.

    
asked by Alvaro Santafe 14.12.2017 в 17:40
source

1 answer

1

The problem comes from the use of list() in PHP7 its behavior has been changed and it can not be used anymore with indexes of type string.

See list () notes

Because list() has been inverted, you are not assigning the values that you expect in the variables, so no condition of the case is fulfilled and imagecreatefrom_*() is never executed.

First let's see that it returns getimagesize($img_original) with a png image of 200px wide and 100px high:

Array
(
    [0] => 200 // Ancho
    [1] => 100 // Alto
    [2] => 3
    [3] => width="200" height="100"
    [bits] => 8
    [mime] => image/png
)

The data that interests us about the array is the mime [mime] => image/png , the width [0] => 200 and the high [1] => 100 . Knowing this we correct some things of the function:

function foto($img_original, $marcadeagua, $posiX, $posiY) {
    // list($old_x, $old_y, $type) = getimagesize($img_original);

    //optenemos array con la info
    $info = getimagesize($img_original);
    // pasamos el mime al switch y corregimos los case, ñadmos un default
    switch ($info['mime'])
    {
        case 'image/jpg':
            $src_img = imagecreatefromjpg($img_original);
            break;
        case 'image/jpeg':
            $src_img = imagecreatefromjpeg($img_original); 
            break;
        case 'image/png':
            $src_img = imagecreatefrompng($img_original);
            break;
        default:
            return false;
    }

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

    //las posiciones en donde ubicar - se reciben por POST
    $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($src_img, $marcadeagua, $xmarcaagua, $ymarcaagua,0, 0,
              $ximagen, $yimagen); // linea donde se genera el error imagecopy()

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

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



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

We have eliminated the list() , instead we have an array $info . In the switch we pass the mime obtained $info['mime'] and we adjust the case to match the mimes.

    
answered by 14.12.2017 / 21:49
source