phpimage-processing

PHP Issue with using imagefill() for a white background after imagecreatetruecolor()


I have a gallery tool in which all of my images have to fit into a 515px X 343px space while being centered. I have the proportions preserved and successfully center the image in that space with a black background on either side (top,right,bottom,left) Which ever are left open after the resize. Images may be taller or wider then the given specs.

I need to make that black background white and tried following the answers suggested at this question imagescreatetruecolor with a white background On an image which is taller I successfully turn the left side white but I can't get the right side of the image to be white. Here is the portion of my code.

$file1new = imagecreatetruecolor($framewidth, $frameheight);

if(isset($fromMidX) && $fromMidX > 0){
    $white = imagecolorallocate($file1new, 255, 255, 255); 
    $rightOffset = ($framewidth - $fromMidX) + 1;
    imagefill($file1new, 0, 0, $white);  //Line 1
    imagefill($file1new, $rightOffset, 0, $white); //Line 2 
}

The variables $framewidth = 515, $framheight = 343, $fromMidX is the x-coordinate offset. If I replace $rightOffset with a static amount of 510 on Line 2 the right side is still all black (even the last 5 px). Better yet, is I comment out line 1 but not line 2, the left side remains white and the right side remains black.

The way I understand how imagefill() works is that it starts from the given X,Y coordinates and floods what ever color is at that pixel with the new color, which in my case would be 255,255,255. So I figured the reason I was getting the white only on the one side is because my image was splitting the canvas in two. That is why I added the second imagefill() for the right side.

The only thing that gets me is that I am using the imagefill()'s before I even bring the object of my uploaded image into the equation. So I don't know how that could be effecting what gets filled with white.

Any insight would be greatly appreciated.

EDIT 1: After the code above I have this:

$source = imagecreatefromjpeg($image); //Line 3
imagecopyresampled($file1new, $source , $fromMidX, $fromMidY , 0, 0, $framewidth, /$frameheight, $w, $h); //Line 4
imagejpeg($file1new, $image,85);
imagedestroy($file1new);

The variable $image is the location of my uploaded image after

move_uploaded_file($_FILES['image']['tmp_name'], $location);
$image = $location;

If I comment out Line 3 and Line 4 then the resulting image is all white.

I also believed that I am flooding the image with all white before applying any resizing operations on my image.


Solution

  • Try refilling the right side after loading the requested image?

    <?php
    $source = imagecreatefromjpeg($image); //Line 3
    $file1new = imagecreatetruecolor($framewidth, $frameheight);
    
    $white = imagecolorallocate($file1new, 255, 255, 255);
    //fill whole image with white color
    imagefill($file1new, 0, 0, $white);  //Line 1
    
    //find right side of image
    $rightOffset = ($framewidth - $fromMidX) + 1;
    
    //insert source file into new image
    imagecopyresampled($file1new, $source , $fromMidX, $fromMidY , 0, 0, $framewidth, $frameheight, $w, $h); //Line 4
    
    //fill image right hand side with white
    imagefill($file1new, $rightOffset, 0, $white); //Line 2 
    
    imagejpeg($file1new, $image,85);
    imagedestroy($file1new);
    ?>