Divide an image into 4 equal parts using php

2

Currently I am trying to divide an image into 4 other images of the same size from the first one. The result I'm looking for is that the height of the original image is divided by 4 and from there, I get those 4 possible images, each one starting where the height of the next would end. I explain it graphically so you can understand it better:

Original image:

Expected result

Code with which I managed to get the first cut:

    $filename= "imagen.png";
    list($w, $h, $type, $attr) = getimagesize($filename);
    $src_im = imagecreatefrompng($filename);

    $src_x = '0';
    $src_y = '0';
    $src_w = $w; // ancho
    $src_h = $h/4; // alto
    $dst_x = '0';
    $dst_y = '0';

    $dst_im = imagecreatetruecolor($src_w, $src_h);
    $white = imagecolorallocate($dst_im, 255, 255, 255);
    imagefill($dst_im, 0, 0, $white);

    imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);

    header("Content-type: image/png");
    imagepng($dst_im);
    print_r($dst_im);

What would be the best way to save the 4 cuts?

  

Extra: How could I make the number of parts to be trimmed be X instead of 4?

    
asked by Hechi 31.08.2016 в 22:50
source

1 answer

1

From what you have, you need to change very few things to get what you want. This is a way to do it trying to modify your code as little as possible:

  • Pass parameters in the GET to be able to select the image and indicate how many parts you want to cut. For example, the call to your PHP file would be like this:

    recortadorImagenes.php?pic=NOMBRE_DE_LA_IMAGEN&num=NUMERO_DE_IMAGENES
    
  • Read those parameters with PHP:

    $filename = $_GET["pic"];
    $number   = $_GET["num"];
    
  • Calculate the height of the new cropped images (total height of the image / number of images instead of 4):

    $src_h = $h/$number;
    
  • Create a loop in which all sub-images will be created:

    for ($x = 0; $x < $number; $x++) {
    
  • Change the beginning of where the image will be copied to the destination so that it is from the position that corresponds in the loop:

    imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, ($x * $src_h), $src_w, $src_h);
    
  • Export the sub-image to a file instead of showing it on the screen (it would be complicated to be several instead of just one). This is achieved by passing a second parameter to the function imagepng , the name will be unique if you use the index of the loop (There may be conflicts and the file would be overwritten, you must also make sure you have permission):

    imagepng($dst_im, str_replace(".png", "_$x.png", $filename));
    

    If the image is called "image.png", then the sub-images will be "image_0.png", "image_1.png", ....

And here is the code that does it (with comments on the lines that have changed):

<?php

    // leer los parámetros del GET
    $filename = $_GET["pic"];
    $number   = $_GET["num"];

    list($w, $h, $type, $attr) = getimagesize($filename);
    $src_im = imagecreatefrompng($filename);

    $src_x = '0';
    $src_y = '0';
    $src_w = $w; // ancho
    $src_h = $h/$number; // calcular el alto dependiendo del número de subimágenes
    $dst_x = '0';
    $dst_y = '0';

    // hacer un bucle para tantas imágenes
    for ($x = 0; $x < $number; $x++) {
        $dst_im = imagecreatetruecolor($src_w, $src_h);
        $white = imagecolorallocate($dst_im, 255, 255, 255);
        imagefill($dst_im, 0, 0, $white);

        // cambiar el principio de donde se copia a partir de la imagen que toque
        imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, ($x * $src_h), $src_w, $src_h);

        // guardar como imagen en lugar de mostrar por pantalla
        imagepng($dst_im, str_replace(".png", "_$x.png", $filename));
    }
    
answered by 01.09.2016 / 01:57
source