PHP: black bar on image when using the GD library

I have a little problem with the GD library in PHP - I resize the image and then want to crop it to 320 pixels (width) / 240 pixels (height). Let me say that the image size is 320 pixels / 300 pixels. When I crop it, a 1-px black bar appears at the bottom of the image. I do not know why. I use imagecrop, imagecreatefromjpegandimagecopyresampled

Here is an example:

enter image description here

Thank you for your time.

The code

$filename = '../store/projects/project-123.jpg';
$mime = mime_content_type($filename);
list($w, $h) = getimagesize($filename);

$prop = $w / $h;
$new_w = 0;
$new_h = 0;

if ($prop <= 4/3) {
    $new_w = 320;
    $new_h = (int)floor($h*($new_w/$w));
} else {
    $new_h = 240;
    $new_w = (int)floor($w*($new_h/$h));
}

$thumb = imagecreatetruecolor($new_w, $new_h);

if (strcmp($mime,'image/png') == 0) {
    header('Content-Type: image/png');
    $source = imagecreatefrompng($filename);
} else {
    header('Content-Type: image/jpeg');
    $source = imagecreatefromjpeg($filename);
}

imagecopyresampled($thumb, $source, 0, 0, 0, 0, $new_w, $new_h, $w, $h);

$filename = '../store/projects-thumbs/project-123.jpg';

$crop_data = array('x' => 0 , 'y' => 0, 'width' => 320, 'height'=> 240);
$thumb = imagecrop($thumb, $crop_data);

imagejpeg($thumb, $filename, 100);  


imagedestroy($thumb);
imagedestroy($source);
+4
source share
1 answer

imagecrop()has a known bug that adds a black bottom border.

, imagecopyresized(). . SO, imagecrop().

+3

All Articles