imagecrop adds black line

0

I'm making a script that reads an xml and then trims the coordinates of it in the image and then saves it, but the "imagecrop" function always adds a black line at the end of the images and I just can not change the size and delete the black line since almost all arrive to the edge. I am using 2 codes, one generates the image and another low.

crop2.php     

$ini_filename = 'assets/sprites/achievements.png';
$im = imagecreatefrompng($ini_filename);

$xml = simplexml_load_file('assets/sprites/achievements.xml');
foreach ($xml->SubTexture as $sub) {
    $to_crop_array = array('x' => $sub['x'] , 'y' => $sub['y'], 'width' => $sub['width'], 'height' => $sub['height']);
    $thumb_im = imagecrop($im, $to_crop_array);

    if ($_GET['name'] == $sub['name']) {
        imagepng($thumb_im);
    }
}

and crop.php

<?php
ini_set('allow_url_fopen', TRUE);

$xml = simplexml_load_file('assets/sprites/achievements.xml');
foreach ($xml->SubTexture as $sub) {

    file_put_contents('images/' . $sub['name'] . '.png', file_get_contents('http://localhost/apk/crop2?name=' . $sub['name']));
}

And I wanted to know how to remove this line without damaging anything.

    
asked by anonymous 28.10.2014 / 08:57

1 answer

1

I took the essence of your problem, ie the bug in imagecrop (), to SOEN and asked for an alternative version that, in addition to not suffering from the same bug still works in versions prior to 5.5 of PHP. Here's the function:

function mycrop($src, array $rect)
{
    $dest = imagecreatetruecolor($rect['width'], $rect['height']);
    imagecopyresized(
        $dest,
        $src,
        0,
        0,
        $rect['x'],
        $rect['y'],
        $rect['width'],
        $rect['height'],
        $rect['width'],
        $rect['height']
    );

    return $dest;
}

To test I used the same bug script that Zuul commented, I just changed the color from white to yellow and sent a header () for the image to appear directly in the browser:

$image = imagecreatetruecolor(500, 500);
imagefill($image, 0, 0, imagecolorallocate($image, 255, 255, 0));

$ressource = mycrop($image, ['x' => 0, 'y' => 0, 'width' => 250, 'height' => 250]);

header( 'Content-type: image/jpeg' );

imagejpeg($ressource);

And while running you see a yellow square of 250px in width and height without the additional black border.

I hope it helps, so I can reward who gave me the solution.

    
04.11.2014 / 11:54