0

I've written some code that copies a chunk of a big image to a new smaller image and saves it to the server. That all works just fine

However, I would like it to trim off any white background around the text and this is the part that doesn't work

Here is my code

$sourceImage = imagecreatefromjpeg($image); 
$tempImg = imagecreatetruecolor($data[2],$data[3]);
imagecopy($tempImg,$sourceImage, 0, 0, $data[0], $data[1], $data[2], $data[3]);
imagejpeg($tempImg,$destImage,90);
imagedestroy($tempImg);
chmod($destImage,0775);
// these next two lines don't work
$original_img = imagecreatefromjpeg($destImage);
$cropped_destImage = imagecropauto($original_img , IMG_CROP_THRESHOLD, 2, 16777215);

Can anyone see what I'm doing wrong here ?

Thanks

2 Answers 2

1

Found it!!

I hadn't realized I needed to turn the resource back into a jpeg :

$original_img = imagecreatefromjpeg($destImage);
$cropped_destImage = imagecropauto($original_img , IMG_CROP_THRESHOLD, 5, 16777215);
imagejpeg($cropped_destImage,$destImage,90);
Sign up to request clarification or add additional context in comments.

Comments

0

I have looked for you and this is what i found: PHP GD Text with Transparency/Alpha background

This person had a similar problem. It could be worthwhile to read this topic because it has useful pieces of codes to deal with text that already has a background in it.

1 Comment

Thanks for your reply but I think I wasn't very clear in the question : I don't need to have transparency around the text. I just need to trim the white background to the edges of the text. At the moment the text floats in a sea of white that needs reducing to the bare minimum - it would be the same problem if it was anything else on the image, surrounded with white bg

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.