Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Andy Tan
Andy Tan
35,969 Points

Black background when using imagecreatefromjpeg with offset image

Hi There

I'm using imagecopyresample to copy an image into another image into a grid. There are some instances where I need the image to be offset because that's how the user has set the image, but the area of the image that is empty cause it's offset it has a black background.

How can I have a white background instead, I've been trying to come up with a solution with no results, if anyone can help me that'll be great. Heres the code that creates the image with the offset

$jpeg_quality = 90;
$path = "/";
$src = 'wildbush-black.jpg';

list($width, $height) = getimagesize($src);
$new_width = $width ;
$new_height = $height ;

$img_src = imagecreatefromjpeg($src);
$img_dest = ImageCreateTrueColor( $new_width, $new_height );

imagecopyresampled(
    $img_dest, //destination image
    $img_src, //source image
    0, //top left coordinate of the destination image in x direction
    0, //top left coordinate of the destination image in y direction
    -10, //top left coordinate in x direction of source image that I want copying to start at
    10, //top left coordinate in y direction of source image that I want copying to start at
    $new_width, //190, thumbnail width
    $new_height, //190, thumbnail height
    $width, //how wide the rectangle from the source image we want thumbnailed is
    $height //how high the rectangle from the source image we want thumbnailed is
);

imagejpeg($img_dest,"test.jpg",$jpeg_quality);

This is the image that is the output of it

1 Answer

Andy Tan
Andy Tan
35,969 Points

I did something different and used imagecopy to just select what part of the image I wanted to copy to, so just ignore above