Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function image_multiline_text($image, $size, $angle, $xpos, $ypos, $color, $font, $text, $max_width) {
- $split = explode(" ", $text);
- $string = "";
- $new_string = "";
- imagettfbbox($size, 0, $font, $text);
- for ($i = 0; $i < count($split); $i++) {
- $new_string.= $split[$i] . " ";
- // check size of string
- $tbbox = imagettfbbox($size, 0, $font, $new_string);
- if ($tbbox[4] < $max_width) {
- $string = $new_string;
- }
- else {
- imagettftext($image, $size, $angle, $xpos, $ypos, $color, $font, $string);
- $string = "";
- $new_string = "";
- $i--;
- $ypos += $size * 1.15;
- }
- }
- return imagettftext($image, $size, $angle, $xpos, $ypos, $color, $font, $string); // "draws" the rest of the string;
- }
- $path = 'template2.png';
- $del = false;
- if ($_FILES['image']['error'] == 0) { // If file was sent
- $sourcePath = $_FILES['image']['tmp_name'];
- $targetPath = "upload/" . $_FILES['image']['name'];
- move_uploaded_file($sourcePath, $targetPath); // Moving Uploaded file
- $path = "upload/" . $_FILES['image']['name'];
- $del = true;
- }
- $font_file = dirname(__FILE__) . '/font/Lobster.ttf';
- $im = imagecreatefromstring(file_get_contents($path));
- $im_size = getimagesize($path);
- $stamp = imagecreatefrompng('template2.png');
- $stamp_size = getimagesize('template2.png');
- $dw = $im_size[0] - $stamp_size[0];
- $dh = $im_size[1] - $stamp_size[1];
- $res = imagecreatetruecolor($stamp_size[0], $stamp_size[1]);
- if ($im_size[1] / $im_size[0] * $stamp_size[0] > $stamp_size[1]) {
- $scaled = imagecreatetruecolor($stamp_size[0], $im_size[1] / $im_size[0] * $stamp_size[0]);
- imagecopyresized($scaled, $im, 0, 0, 0, 0, $stamp_size[0], $im_size[1] / $im_size[0] * $stamp_size[0], $im_size[0], $im_size[1]);
- imagecopy($scaled, $stamp, 0, (imagesy($scaled) - $stamp_size[1]) / 2, 0, 0, imagesx($stamp), imagesy($stamp));
- imagecopyresized($res, $scaled, 0, 0, 0, (imagesy($scaled) - $stamp_size[1]) / 2, $stamp_size[0], $stamp_size[1], $stamp_size[0], $stamp_size[1]);
- }
- else {
- $scaled = imagecreatetruecolor($im_size[0] / $im_size[1] * $stamp_size[1], $stamp_size[1]);
- imagecopyresized($scaled, $im, 0, 0, 0, 0, $im_size[0] / $im_size[1] * $stamp_size[1], $stamp_size[1], $im_size[0], $im_size[1]);
- imagecopy($scaled, $stamp, (imagesx($scaled) - $stamp_size[0]) / 2, 0, 0, 0, imagesx($stamp), imagesy($stamp));
- imagecopyresized($res, $scaled, 0, 0, (imagesx($scaled) - $stamp_size[0]) / 2, 0, $stamp_size[0], $stamp_size[1], $stamp_size[0], $stamp_size[1]);
- }
- $font_size = 50;
- $color = imagecolorallocate($res, 255, 255, 255);
- $dx = 50;
- $dy = 50;
- $box = image_multiline_text($res, $font_size, 0, $dx, $font_size + $dy, $color, $font_file, $_POST['text'], imagesx($stamp) - $dx);
- imagettftext($res, $font_size, 0, $dx, $box[3] + $font_size + $dy, $color, $font_file, $_POST['tag']);
- ob_start(); // Let's start output buffering.
- imagepng($res); //This will normally output the image, but because of ob_start(), it won't.
- $contents = ob_get_contents(); //Instead, output above is saved to $contents
- ob_end_clean(); //End the output buffer.
- echo "data:image/png;base64," . base64_encode($contents);
- imagepng($res, "upload/post_".$_FILES['image']['name'], 0);
- imagedestroy($im);
- imagedestroy($scaled);
- imagedestroy($res);
- if ($del)
- unlink($path);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement