Last active
December 17, 2015 11:39
-
-
Save dave-jay/5603504 to your computer and use it in GitHub Desktop.
Create Vertical-Image PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function createVertialTextImage($font_size, $width, $height, $text, $image_name, $gap=false, $angle='90') { | |
$font = _PATH . 'media/upload/fonts/arialbd.ttf'; // path to font file | |
$bbox = imagettfbbox($font_size, $angle, $font, $text[0]); | |
$_width = $angle == '90' ? $width : $width; //($bbox[2]-$bbox[0]); | |
$im = imagecreatetruecolor($_width, $height); | |
$white = imagecolorallocate($im, 255, 255, 255); | |
$grey = imagecolorallocate($im, 128, 128, 128); | |
$black = imagecolorallocate($im, 0, 0, 0); | |
imagefilledrectangle($im, 0, 0, $_width - 1, $height - 1, $white); | |
$gap = $gap ? 10 : 1; | |
if ($angle == '90') { | |
$bbox = imagettfbbox($font_size, $angle, $font, $text[0]); | |
$x = $bbox[0] + (imagesx($im) / 2) - ($bbox[4] / 2) + 10; | |
$y = $bbox[1] + (imagesy($im) / 2) - ($bbox[5] / 2) - 5; | |
imagettftext($im, $font_size, $angle, $font_size + $gap + 2, $y + 5, $black, $font, $text); | |
} else { | |
$bbox = imagettfbbox($font_size, $angle, $font, $text[0]); | |
$x = $bbox[0] + (imagesx($im) / 2) - ($bbox[4] / 2) + 10; | |
$y = $bbox[1] + (imagesy($im) / 2) - ($bbox[5] / 2) - 5; | |
imagettftext($im, $font_size, $angle, $x - $x1, $height - $h1, $black, $font, $text); | |
} | |
imagepng($im, "{$image_name}_{$width}.png"); | |
imagedestroy($im); | |
} | |
// To create image, use below function call | |
createVertialTextImage('14','100','200','Vertical Text','v_image_name','90') | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment