Imagecopyresampled () results in splitting the background color of imagefill ()

I have a script that takes an image, and when resampled, a short size (width or height) on a square colored background. This works great for images with a longer WIDTH, but for whatever reason, any image with a higher height of the result is splitting the background padding - correct color on the left and black by default on the right. If I play with an x ​​offset then the background padding only extends to the right up to where the image is placed.

The computed values ​​are expected for vertical images, so I can't figure out what's going on here. Note that 'imagecopy ()' produces the same behavior.

  • original image - 155 x 400 pixels
  • adjusted source dimensions for square aspect ratio = 400 x 400 pixels
  • final thumbnail size 250 x 250 pixels

Here's some code with static values ​​for one example:

$thumb = imagecreatetruecolor(250, 250);

imagecopyresampled($thumb, $source, 77, 0, 0, 0, 250, 250, 400, 400);

$blue = imagecolorallocate($thumb, 0xDE, 0xE6, 0xF9);
imagefill($thumb, 0, 0, $blue);

      

Using the same image rotated 90 degrees (400 x 155 pixels), so it's longer horizontally. Apply full background fill:

imagecopyresampled($thumb, $source, 0, 77, 0, 0, 250, 250, 400, 400);

      

For a vertical image, my coordinate values ​​(77, 0) place the image on the canvas imagecreatetruecolor () centered exactly where I want, but by changing any other imagecopyresampled () values, stretch or shrink the overselected image, or crop it.

Am I missing something simple? See screenshots here: http://i.stack.imgur.com/5CxHU.jpg (vertical problem) and http://i.stack.imgur.com/wvhzP.jpg (OK horizontally)

+3


source to share


1 answer


This vertical problem must have something to do with PHP's resampling / imagefill algorithm (?), But here is a workaround that now works for centering all my vertical images inside my square canvas:

1) First you have to fill your image placeholder so that the background fill continues to the right edge in the overselected image, increasing the height of the thumbnail by offsetting the x-axis (sounds odd, but it works) ... we'll crop this later:

$thadj_height = $th_height + $th_x;
$thumb = imagecreatetruecolor($th_width, $thadj_height);

      

2) Repeat as usual with padding the background (note padding is applied AFTER the oversampling operator, odd, but just works that way) ... remember that $ thumb has a higher height than $ th_width, $ th_height will take up

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

      

3) Temporarily save the output of the image to then apply the new function to it - set the quality to lossless as we will be reusing it:

imagejpeg($thumb, "resampled/output_temp.jpg", 100);
imagedestroy($thumb);

      



4) Extract the temporary file and take the new dimensions (replace the previous variables):

$file = "resampled/output_temp.jpg";
$image = file_get_contents($file);
$source = imagecreatefromstring($image);
list($src_width, $src_height) = getimagesize($file);

      

5) Create a new image placeholder, square, as originally intended in my case:

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

      

6) Now copy the temporarily added thumbnail to the placeholder square that will crop the spacer:

imagecopy($thumb, $source, 0, 0, 0, 0, $src_width, $src_height);

header('Content-Type: image/jpeg');
echo imagejpeg($thumb);
imagedestroy($thumb);

      

Again, none of this is required to center my horizontal images on a square canvas, but this is a workaround that will help eliminate the split-fill background.

0


source







All Articles