0

We are processing nearly 20M existing images (not files), all of which have been converted to the raw PNG data. Each image is always 640x480, and we want them all to be saved as square 640x640 files, with the original image on the top of the larger canvas, leaving the additional lower 160px for descriptive text to be added.

Our PNG data being retrieved is like this:

data:image/png;base64,iVBORw0 ... kJggg==

I've seen lots of posts and examples showing work with resizing, transparencies, colors, etc., but cannot determine where to start creating a new empty 640x640 "canvas" with existing PNG data.

If this is a duplicate question, I'd be happy to delete it if I can get some direction on how to begin. Without knowing the basics to get started, googling things like "image create png canvas" hasn't been helpful. Clearly, image processing is new to me, so apologize if I'm not being clear enough. Thanks in advance.

GDP
  • 8,109
  • 6
  • 45
  • 82

1 Answers1

1

This should work for you:

$imgData = 'data:image/png;base64,iVBORw0 ... kJggg==';
$img = imagecreatefromstring(base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $imgData)));
$targetImage = imagecreatetruecolor(640, 640);
imagecopyresampled($targetImage, $img, 0, 0, 0, 0, 640, 480, 640, 480);
imagepng($targetImage, $yourPath);

Of course you might want to add some checks, background color etc. but I think you will be ready to start with this.

marcus.kreusch
  • 648
  • 5
  • 15
  • Having some trouble with getting the original image at top left without being stretched to the new height, but yes......this is what Ive been looking for - thankyou! – GDP Jan 09 '18 at 19:09
  • 1
    My fault: it should be imagecopyresampled instead of imagecopy. Would be glad if you accepted this as the right answer :-) – marcus.kreusch Jan 09 '18 at 19:11