problem when pixelating image in php

0

What I'm trying to do is pixelate an image of a url, add another image to it and finally save that image.

I have obtained this internet code to pixelate images but I think it does not work correctly. It does not show me the pixelated image nor does it save it. It just keeps the screen black.

<?php 

$pixel = 15;

    $getImagen = 'https://ep00.epimg.net/elpais/imagenes/2017/06/05/album/1496652756_562670_1496654035_album_normal.jpg';


    $imagen = imagecreatefromjpeg($getImagen); 
    if(!$imagen) exit('ERROR');
    list($ancho,$alto)=getimagesize($getImagen);
    $superficieTotal = $ancho*$alto;    
    //
    $superficieRecorrida = 0;
    $auxX=0;
    $auxY=0;
    while($superficieRecorrida <= $superficieTotal){
        $posX=0;$posY=0;$data = array();
        while($posX <= $pixel and (($auxX + $posX) < $ancho)){
            $posY=0;
            while($posY <= $pixel and (($auxY + $posY) < $alto)){
                $rgb = imagecolorat($imagen, ($auxX + $posX), ($auxY + $posY));
                $r = ($rgb >> 16) & 0xFF;
                $g = ($rgb >> 8) & 0xFF;
                $b = $rgb & 0xFF;
                $data[] = array($r,$g,$b);
                $posY++;
            }
            $posX++;
        }

        // Busco promedio
        $r = 0; $g = 0; $b = 0;
        foreach($data as $d){
            $r+= $d[0];
            $g+= $d[1];
            $b+= $d[2];
        }
        $totalArray = count($data);
        if($totalArray == 0) $totalArray = 1;
        $r = $r/$totalArray;
        $g = $g/$totalArray;
        $b = $b/$totalArray;
        $colorPromedio = imagecolorallocate($imagen, $r, $g, $b);
        imagefilledrectangle($imagen, $auxX, $auxY, ($auxX + $pixel), ($auxY + $pixel), $colorPromedio);
        //
        $auxX+= $pixel;
        if($auxX >= $ancho){
            $auxX = 0;
            $auxY+= ($pixel+1);
        }       
        $superficieRecorrida+= $pixel*$pixel;

    }
    //
    Header("Content-type: image/jpeg");
    imagejpeg($imagen);
    imagedestroy($imagen);

Thank you for your future responses. a greeting.

    
asked by Mani Zeta 20.09.2017 в 12:31
source

2 answers

0

Here I leave you with the solution to my question.

        <?php

        $unico= uniqid();

        function redimensionar($imagenoriginal,$maximo){
            global $unico;

            //Cargo en memoria la imagen que quiero redimensionar
                $imagen_original = imagecreatefromjpeg($imagenoriginal);

                //Obtengo el ancho de la imagen quecargue
                $ancho_original = imagesx($imagen_original);

                //Obtengo el alto de la imagen que cargue
                $alto_original = imagesy($imagen_original);

                //SI QUEREMOS UN ANCHO FINAL FIJO, calculamos el ALTO de forma proporcionada
                $ancho_final = $maximo;

                //Ancho final en pixeles
                $alto_final = ($ancho_final / $ancho_original) * $alto_original;

                //Creo una imagen vacia, con el alto y el ancho que tendrá la imagen redimensionada
                $imagen_redimensionada = imagecreatetruecolor($ancho_final, $alto_final);


                //Copio la imagen original con las nuevas dimensiones a la imagen en blanco que creamos en la linea anterior
                imagecopyresampled($imagen_redimensionada, $imagen_original, 0, 0, 0, 0, $ancho_final, $alto_final, $ancho_original, $alto_original);

                //Guardo la imagen ya redimensionada        
                imagejpeg($imagen_redimensionada, $unico.".jpg");

        }


        function ponerFiltro($unico){
            // Creo una imagen png con el logo  o la imagen que utilizaremos
        // como marca de agua
        $marca = imagecreatefrompng('18.png');
        //Creo una imagen temporal de la imagen a marcar
        $imagentemporal = imagecreatefromjpeg($unico);

        // Establecer los márgenes inferiores y derecho de la imagen de marca de agua

        $margenderecho = 0;
        $margeninferior = 0;
        // Establecemos la posicion donde se ubicara la marca
        $posicionx= imagesx($marca);
        $posiciony= imagesy($marca);

        // Copiar la marca de agua sobre la imagen temporal y
        // calculamos la posición de la imagen marca o logo.
        imagecopy($imagentemporal, $marca, imagesx($imagentemporal) - $posicionx- $margenderecho, imagesy($imagentemporal) - $posiciony- $margeninferior, 0, 0, imagesx($marca), imagesy($marca));

        //Guardamos la imagen marcada en un nuevo archivo
        imagepng($imagentemporal, $unico.'nuevaimagen.png');
        // Mostramos imagen con marca y liberar Ram
        //header('Content-type: image/png');
        //imagepng($imagentemporal);
        //imagedestroy($imagentemporal);
        }

        //Redimensiono la imagen a 100px
        redimensionar("https://k38.kn3.net/taringa/2/4/3/0/6/6/13/soyluuchoox/A1D.jpg?1843",100);

        //Vuelvo a redimensionar la imagen a 640px
        redimensionar($unico.".jpg",640);

        //Añado la marca de agua
        ponerFiltro($unico.".jpg");

        //Elimino la foto original y me quedo con la que tiene la marca de agua
        unlink($unico.".jpg");

        //Una vez que se haya enviado el post se eliminar la imagen.

        //unlink($unico.'.jpgnuevaimagen.png');
        ?>

Thanks and best regards.

    
answered by 21.09.2017 / 09:49
source
0

How long ago I did not play php I'm putting the errors that I've been committing to get the final result (and I think that you want).

Possibly these first steps you can skip them. The first thing I did was try to configure ST3 but I was not able (I could not find the GD library - > Call to undefined function imagecreatefromjpeg() ). So in the end I installed WAMP ( following these guidelines ) and ready.

Once the code was working, the error I found was the direct assignment of a URL to a variable. The solution was to download from the URL the photo I want to modify to a local directory and continue working with the downloaded image as in the code you already have.

The only thing that is modified are these first lines of code:

copy('https://ep00.epimg.net/elpais/imagenes/2017/06/05/album/1496652756_562670_1496654035_album_normal.jpg', 'photo.jpg');
$getImagen = 'photo.jpg';

I hope it has helped you;)

    
answered by 20.09.2017 в 19:51