Skip to content

Instantly share code, notes, and snippets.

@thinkclay
Created November 28, 2012 04:57
Show Gist options
  • Save thinkclay/4159108 to your computer and use it in GitHub Desktop.
Save thinkclay/4159108 to your computer and use it in GitHub Desktop.
building images from scratch
<?php
function wrap_text($txt, $color = 000000, $space = 4, $font = 2, $w = 300, $h = 300)
{
if (strlen($color) != 6)
$color = 000000;
$int = hexdec($color);
$fw = imagefontwidth($font);
$txt = explode("\n", wordwrap($txt, ($w / $fw) -2, "\n"));
$lines = count($txt);
// Create an image and add in all kinds of transparency rules
$im = imagecreatetruecolor($w, $h);
imagealphablending($im, false);
$col = imagecolorallocatealpha($im,255,255,255,127);
imagefilledrectangle($im,0,0,485, 500,$col);
imagealphablending($im, true);
imagesavealpha($im, true);
$color = imagecolorallocatealpha($im, 255, 255, 255, 0);
$y = 8;
foreach ($txt as $text)
{
imagestring($im, $font, 10, $y, $text, $color);
$y += (imagefontheight($font) + $space);
}
imagepng($im, 'test.png');
return $im;
}
// Load the stamp and the photo to apply the watermark to
$base = imagecreatefromjpeg('base.jpg');
$quote = imagecreatefromjpeg('quote.jpg');
$sq1 = imagecreatefromjpeg('IMG_5906.jpg');
$sq2 = imagecreatefromjpeg('IMG_5581.jpg');
$sq3 = imagecreatefromjpeg('product.jpg');
imagecopymerge($base, $quote, 0, 0, 0, 0, 128, 128, 100);
imagecopymerge($base, $sq1, 130, 0, 0, 0, 128, 128, 100);
imagecopymerge($base, $sq2, 0, 130, 0, 0, 128, 128, 100);
imagecopyresized($base, $sq3, 130, 130, 0, 0, 128, 128, imagesx($sq3), imagesy($sq3));
$str = 'Features including separate rear-entry panel, goggle pocket with welded zip, back panel reinforcing and intelligent carry strap placement';
$text = wrap_text($str, ffffff, 1, 2, 128, 128);
// Merge the stamp onto our photo with alpha
imagecopyresampled($base, $text, 0, 0, 0, 0, 128, 128, 128, 128);
// Save the image to file and free memory
imagealphablending($base, true);
imagepng($base, 'photo_stamp.png');
imagedestroy($base);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment