Last active
February 14, 2023 13:34
-
-
Save mklasen/8eaa45cf4e2d84d0b0d369e211cb7d26 to your computer and use it in GitHub Desktop.
Generate images for Sympose person pages
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 | |
/** | |
* Plugin Name: Generate person images | |
*/ | |
use Treinetic\ImageArtist\lib\Text\Color; | |
use Treinetic\ImageArtist\lib\Overlays\Overlay; | |
use Treinetic\ImageArtist\lib\Text\TextBox; | |
use Treinetic\ImageArtist\lib\Text\Font; | |
use Treinetic\ImageArtist\lib\Image; | |
use Treinetic\ImageArtist\lib\Shapes\Square; | |
require plugin_dir_path( __FILE__ ) . 'vendor/autoload.php'; | |
class Sympose_Person_Image_Generator { | |
public function __construct() { | |
$this->hooks(); | |
} | |
public function hooks() { | |
add_action( | |
'init', | |
function() { | |
if ( isset( $_GET['debug_image'] ) ) { | |
$this->create_image(); | |
die; | |
} | |
} | |
); | |
} | |
public function create_image() { | |
$post = get_post( 63147 ); | |
$background_image_id = 91376; | |
$person_image_id = sympose_get_image( $post->ID ); | |
$bg_img = wp_get_attachment_image_url( $background_image_id, 'full' ); | |
$person = wp_get_attachment_image_url( $person_image_id, 'full' ); | |
$title = $post->post_title; | |
$desc = get_post_meta( $post->ID, '_sympose_description', true ); | |
$logo = sympose_get_option( '_sympose_ticket_logo' ); | |
$data = array( | |
'text' => array( | |
'title' => $title, | |
'desc' => $desc, | |
), | |
'images' => array( | |
'logo' => $logo, | |
'background' => $bg_img, | |
'person' => $person, | |
), | |
); | |
$img = $this->create_image_with_background( $data ); | |
$overlay = new Overlay( $img->getWidth(), $img->getHeight(), new Color( 0, 0, 0, 80 ) ); | |
$img->merge( $overlay, 0, 0 ); | |
$img = $this->add_logo( $img, $data ); | |
$img = $this->add_person( $img, $data ); | |
$img = $this->add_name( $img, $data ); | |
$img = $this->add_description( $img, $data ); | |
$img->dump(); | |
} | |
public function create_image_with_background( $data ) { | |
$temp = tmpfile(); | |
fwrite( $temp, file_get_contents( $data['images']['background'] ) ); | |
$path = stream_get_meta_data( $temp )['uri']; | |
$img = new Image( $path ); | |
$img->resize( 1200, 627); | |
return $img; | |
} | |
public function add_logo( $img, $data ) { | |
$temp = tmpfile(); | |
fwrite( $temp, file_get_contents( $data['images']['logo'] ) ); | |
$path = stream_get_meta_data( $temp )['uri']; | |
$square = new Square( $path ); | |
$square->build(); | |
$square->scaleToHeight( 150 ); | |
$square->scaleToWidth( 150 ); | |
$img->merge( $square, $img->getWidth() - $square->getWidth() - 15 , 15 ); | |
return $img; | |
} | |
public function add_person( $img, $data ) { | |
$temp = tmpfile(); | |
fwrite( $temp, file_get_contents( $data['images']['person'] ) ); | |
$path = stream_get_meta_data( $temp )['uri']; | |
$circle = new Square( $path ); | |
$circle->scaleToHeight( 300 ); | |
$circle->scaleToWidth( 300 ); | |
$circle->build(); | |
return $img->merge( $circle, 30, ( $img->getHeight() - $circle->getHeight() - 30 ) ); | |
} | |
public function add_name( $img, $data ) { | |
$textBox = new TextBox( $img->getWidth() - 330, 100, false ); | |
$textBox->setColor( Color::getColor( Color::$WHITE ) ); | |
$textBox->setFont( Font::getFont( Font::$NOTOSERIF_REGULAR ) ); | |
$textBox->setSize( 50 ); | |
$textBox->setMargin( 2 ); | |
$textBox->setText( $data['text']['title'] ); | |
$img->setTextBox( $textBox, 350, 490 ); | |
return $img; | |
} | |
public function add_description( $img, $data ) { | |
$textBox = new TextBox( 500, 100, false ); | |
$textBox->setColor( new Color( 141, 141, 141 ) ); | |
$textBox->setFont( Font::getFont( Font::$NOTOSERIF_REGULAR ) ); | |
$textBox->setSize( 25 ); | |
$textBox->setMargin( 2 ); | |
$textBox->setText( $data['text']['desc'] ); | |
$img->setTextBox( $textBox, 350, 560 ); | |
return $img; | |
} | |
} | |
new Sympose_Person_Image_Generator(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment