Advertisement
Guest User

Untitled

a guest
Jun 13th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.49 KB | None | 0 0
  1. <?php
  2.    
  3.     function image_multiline_text($image, $size, $angle, $xpos, $ypos, $color, $font, $text, $max_width) {
  4.         $split = explode(" ", $text);
  5.         $string = "";
  6.         $new_string = "";
  7.         imagettfbbox($size, 0, $font, $text);
  8.         for ($i = 0; $i < count($split); $i++) {
  9.             $new_string.= $split[$i] . " ";
  10.             // check size of string
  11.             $tbbox = imagettfbbox($size, 0, $font, $new_string);
  12.             if ($tbbox[4] < $max_width) {
  13.                 $string = $new_string;
  14.             }
  15.             else {
  16.                 imagettftext($image, $size, $angle, $xpos, $ypos, $color, $font, $string);
  17.                 $string = "";
  18.                 $new_string = "";
  19.                 $i--;
  20.                 $ypos += $size * 1.15;
  21.             }
  22.         }
  23.         return imagettftext($image, $size, $angle, $xpos, $ypos, $color, $font, $string); // "draws" the rest of the string;
  24.     }
  25.  
  26.     $path = 'template2.png';
  27.     $del = false;
  28.     if ($_FILES['image']['error'] == 0) {   // If file was sent
  29.         $sourcePath = $_FILES['image']['tmp_name'];
  30.         $targetPath = "upload/" . $_FILES['image']['name'];
  31.         move_uploaded_file($sourcePath, $targetPath); // Moving Uploaded file
  32.         $path = "upload/" . $_FILES['image']['name'];
  33.         $del = true;
  34.     }
  35.     $font_file = dirname(__FILE__) . '/font/Lobster.ttf';
  36.     $im = imagecreatefromstring(file_get_contents($path));
  37.     $im_size = getimagesize($path);
  38.     $stamp = imagecreatefrompng('template2.png');
  39.     $stamp_size = getimagesize('template2.png');
  40.     $dw = $im_size[0] - $stamp_size[0];
  41.     $dh = $im_size[1] - $stamp_size[1];
  42.     $res = imagecreatetruecolor($stamp_size[0], $stamp_size[1]);
  43.     if ($im_size[1] / $im_size[0] * $stamp_size[0] > $stamp_size[1]) {
  44.         $scaled = imagecreatetruecolor($stamp_size[0], $im_size[1] / $im_size[0] * $stamp_size[0]);
  45.         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]);
  46.         imagecopy($scaled, $stamp, 0, (imagesy($scaled) - $stamp_size[1]) / 2, 0, 0, imagesx($stamp), imagesy($stamp));
  47.         imagecopyresized($res, $scaled, 0, 0, 0, (imagesy($scaled) - $stamp_size[1]) / 2, $stamp_size[0], $stamp_size[1], $stamp_size[0], $stamp_size[1]);
  48.     }
  49.     else {
  50.         $scaled = imagecreatetruecolor($im_size[0] / $im_size[1] * $stamp_size[1], $stamp_size[1]);
  51.         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]);
  52.         imagecopy($scaled, $stamp, (imagesx($scaled) - $stamp_size[0]) / 2, 0, 0, 0, imagesx($stamp), imagesy($stamp));
  53.         imagecopyresized($res, $scaled, 0, 0, (imagesx($scaled) - $stamp_size[0]) / 2, 0, $stamp_size[0], $stamp_size[1], $stamp_size[0], $stamp_size[1]);
  54.     }
  55.     $font_size = 50;
  56.     $color = imagecolorallocate($res, 255, 255, 255);
  57.     $dx = 50;
  58.     $dy = 50;
  59.     $box = image_multiline_text($res, $font_size, 0, $dx, $font_size + $dy, $color, $font_file, $_POST['text'], imagesx($stamp) - $dx);
  60.     imagettftext($res, $font_size, 0, $dx, $box[3] + $font_size + $dy, $color, $font_file, $_POST['tag']);
  61.     ob_start(); // Let's start output buffering.
  62.         imagepng($res); //This will normally output the image, but because of ob_start(), it won't.
  63.         $contents = ob_get_contents(); //Instead, output above is saved to $contents
  64.     ob_end_clean(); //End the output buffer.
  65.     echo "data:image/png;base64," . base64_encode($contents);
  66.     imagepng($res, "upload/post_".$_FILES['image']['name'], 0);
  67.     imagedestroy($im);
  68.     imagedestroy($scaled);
  69.     imagedestroy($res);
  70.     if ($del)
  71.         unlink($path);
  72. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement