Last active
December 26, 2022 13:17
-
-
Save karamansky/9e0da024ba9b602a9111adaea636f750 to your computer and use it in GitHub Desktop.
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
public static function anaCreateImgFromText( $string, $image_random_name = 'tmp_name' ){ | |
// set charset | |
mb_internal_encoding("UTF-8"); | |
$content = ""; | |
$font = get_stylesheet_directory() . "/assets/fonts/Lato/regular.ttf"; | |
// text width | |
$width = 540; | |
//font size | |
$size = 16; | |
$line_num = 0; | |
// explode string to words and movo it to letter[] | |
for ($i = 0; $i < mb_strlen($string); $i++) { | |
$letter[] = mb_substr($string, $i, 1); | |
} | |
foreach ($letter as $l) { | |
$tmpstr = $content . " " . $l; | |
$fontBox = imagettfbbox($size, 0, $font, $tmpstr); | |
if ((($fontBox[2] > $width) && ($content !== "")) || $l == '•') { | |
$content .= "\n"; | |
$line_num += 1; | |
} | |
$content .= $l; | |
} | |
$fontHeight = imagefontheight($size) * 2; //2 - line-height | |
$im = imagecreatetruecolor(550, $line_num * $fontHeight + 30);//30 = padding-top 15px + padding-bottom 15px | |
// set background color | |
$lightBlue = imagecolorallocate($im, 248, 250, 255); | |
imagefill($im, 0, 0, $lightBlue); | |
// set font color | |
$black = imagecolorallocate($im, 0, 0, 0); | |
// ceil(($width - $fontBox[2]) / 2) - centered text | |
// y=15 - padding from top | |
imagettftext($im, $size, 0, ceil(($width - $fontBox[2]) / 2), 15, $black, $font, $content); | |
// get image url | |
$filePathDir = get_stylesheet_directory() . static::$tmp_folder; | |
if (!is_dir($filePathDir)) { | |
mkdir($filePathDir, 0777, true); | |
} | |
// add filename (must be random) | |
$filePath = $filePathDir . 'text.jpg'; // add $image_random_name | |
// generate image and save to server | |
imagejpeg($im, $filePath); | |
return $filePath; | |
} | |
function anaCreateTwitImage( $analyst_image_src, $analyst_text_data = [] ){ | |
// result image height | |
$height = 0; | |
// result image width | |
$width = 570; | |
$source = []; | |
$image_p = []; | |
// Analyst photo url | |
// like /home/user1/anachart.com/wp-content/themes/theme1/images/analyst.jpg | |
$image = [ | |
get_stylesheet_directory() . static::$tmp_folder . 'head.jpg', //$analyst_image_src | |
]; | |
//analyst text data array | |
// $analyst_text_data | |
$text_data = [ | |
'Analyst: SEAN WIELAND (PIPER SANDLER)', | |
'The Stock: CHNG (Change Healthcare Inc)', | |
'Price Target Change: $494->$549 = $55 (11.13%)', | |
'Average Time For PT To Be Met: 251', | |
'Potential Upside Change: $109.6->$164.6; 198%->133%', | |
'Change Price Target Met Ratio: 18/22 (81.82%)' | |
]; | |
// text data to string | |
$text_data = '• ' . implode('• ', $text_data); | |
// generate img from text | |
$image_path = anaCreateImgFromText($text_data); | |
// 1 - this is generated image from text | |
$image[1] = $image_path; | |
// get images data | |
foreach ($image as $key => $val) { | |
$source[$key]['source'] = Imagecreatefromjpeg($val); | |
$res_image[$key] = getimagesize($val); | |
} | |
// get the total height of the canvas | |
foreach ($res_image as $key => $val) { | |
$height += $val[1]; | |
$res_image[$key]['height'] = $val[1]; | |
// scale | |
$image_p[$key] = imagecreatetruecolor($width, $val[1]); | |
imagecopyresampled($image_p[$key], $source[$key]['source'], 0, 0, 0, 0, $val[0], $val[1], $val[0], $val[1]); | |
} | |
// create canvas | |
$new_image = imagecreatetruecolor($width, $height); | |
// add background color | |
$lightBlue = imagecolorallocate($new_image, 248, 250, 255); | |
imagefill($new_image, 0, 0, $lightBlue); | |
// add image to canvas | |
$dst_x = 15; //padding - 15px | |
$dst_y = 15; | |
foreach ($res_image as $key => $val) { | |
imagecopy($new_image, $image_p[$key], $dst_x, $dst_y, 0, 0, $val[0], $val[1]); | |
$dst_y += $val[1]; | |
} | |
$logo = get_stylesheet_directory() . static::$tmp_folder . 'logo.jpg'; | |
$logo_src = Imagecreatefromjpeg($logo); | |
$tmp_size = getimagesize($logo); | |
imagecopy($new_image, $logo_src, ceil($width - $tmp_size[0] - 15), 15, 0, 0, $tmp_size[0], $tmp_size[1]); | |
// create image | |
$res_data = Imagejpeg($new_image, get_stylesheet_directory() . static::$tmp_folder . 'twitter_analyst_img.jpg', 1000); // add $image_random_name | |
if( !$res_data ) return false; | |
// remove tmp images | |
unlink(get_stylesheet_directory() . static::$tmp_folder . 'text.jpg'); //$image_random_name | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment