1

I have a resampling script that centers resampled images on a square canvas. For horizontally-oriented images that are centered vertically on a white background a line is added along the BOTTOM edge of the resampled image. For vertically-oriented images that are centered horizontally the line appears on the RIGHT edge of the resampled image.

$thumb = imagecreatetruecolor($th_width, $th_height);

imagecopyresampled($thumb, $source, $th_x, $th_y, 0, 0, $th_width, $th_height, $src_width, $src_height);

$bgcolor = imagecolorallocate($thumb, 255, 255, 255);
imagefill($thumb, 0, 0, $bgcolor);

The line is there regardless of background fill, it just shows up most on white. Resample border issue

What causes this? No amount of adjusting parameter values will get rid of it (they just offset the resampled image on the background or distort it).

cartimus
  • 41
  • 3

1 Answers1

0

I am providing my own workaround for this issue. It appears the image border artifact is a COMBINATION result of 'imagecopyresampled()' AND centering offsets. If I just resample the image as-is THEN center and fill the image, the border is avoided. Here is the workaround:

1) RESAMPLE your image as-is (same aspect ratio) then SAVE to retain changes:

$thumb = imagecreatetruecolor($res_width, $res_height);

imagecopyresampled($thumb, $source, 0, 0, 0, 0, $res_width, $res_height, $src_width, $src_height);

// SAVE new image - set quality to lossless since reusing it:
imagejpeg($thumb, "resampled/output_temp.jpg", 100);

2) Retrieve the temporary image:

$file = "resampled/output_temp.jpg";
$image = file_get_contents($file);
$source = imagecreatefromstring($image);

// Get the RESAMPLED image dimensions
list($src_width, $src_height) = getimagesize($file);

3) NOW apply CENTERING and FILL:

$thumb = imagecreatetruecolor($th_width, $th_height);

// COPY the image
imagecopy($thumb, $source, $th_x, $th_y, 0, 0, $src_width, $src_height);

$bgcolor = imagecolorallocate($thumb, 255, 255, 255);

// Replace default black background - this must be placed AFTER imagecopy()
imagefill($thumb, 0, 0, $bgcolor);

// Save image (uses default quality setting 75)
imagejpeg($thumb, "resampled/" . $newfile);

// Optionally free up memory:
imagedestroy($thumb);

The result is a clean image. For correcting incomplete imagefill() on the background when centering vertically-oriented images see this post: imagecopyresampled() results in split color background imagefill()

Community
  • 1
  • 1
cartimus
  • 41
  • 3