imagecrop adds black line

Asked

Viewed 139 times

0

I’m making a script that reads an xml and then cuts its coordinates 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’t change the size or delete the black line since almost all of them reach the edge. I’m using 2 codes, one generates the image and the other 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.

1 answer

1


I took the essence of your problem, that is, the imagecrop() bug, to SOEN and asked for an alternative version that, besides not suffering from the same bug still works in versions prior to 5.5 of PHP. That is 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 only 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 performing you see a 250px yellow square of width and height without the additional black border.

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

  • If possible, I would like confirmation as to the solution because it came from SOEN and, who developed it, could receive due thanks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.