“Meme Generator PHP Class”


Table of Contents

  1. What is it?
  2. Included files
  3. Usage examples

A) What is it? - top

This is very customizable PHP class that can help You generate memes. It takes and image and two lines of texts and put it exactly at the center top and center bottom part of the image.

It's main capabilities are:


B) Included files - top

Package contains 3 main elements:


C) Usage examples - top

Usage is pretty straighforward. You need to include class file, create a new object and then set basic properties.

Minimal set of properties is just a source image file path and one the texts: top, bottom or both.

	
include 'Meme_generator.php';

$mg = new Meme_generator();

// OR YOU CAN LOAD WHOLE CONFIG FROM ONE ARRAY
// possible keys: 
// * meme_font / meme_output_dir / meme_font_to_image_ratio / meme_margins_to_image_ratio / meme_image_path / meme_top_text / 
// * meme_bottom_text / meme_font
// * meme_watermark_file / meme_watermark_margins / meme_watermark_opacity
// $config['meme_image_path'] = './tmp/philosoraptor.jpg';
// $config['meme_top_text'] = 'What if I\'m just a meme generating other memes?';
// $config['meme_bottom_text'] = 'We need to go deeper.. (UTF test: £ zażółćg ęślą jaźń ‹›';
// $config['meme_font'] = './fonts/DejaVuSansMono-Bold.ttf'; 
// $config['meme_output_dir'] = './tmp/';
// $mg->load_config( $config );

if( ! file_exists($_FILES['myfile']['tmp_name']) || !is_uploaded_file($_FILES['myfile']['tmp_name'] ) ) {
        // Some existing testing file
        $example_image_path = './tmp/philosoraptor.jpg';
} else {
        // Uploaded file
        $example_image_path = $_FILES['myfile']['tmp_name'];
}

// Output example image - put texts only
$mg->set_top_text( $_POST['top_text'] );
$mg->set_bottom_text( $_POST['bottom_text'] );
$mg->set_output_dir( './tmp/' ); // default to ./ if not set
$mg->set_image( $example_image_path );
$output_image1 = $mg->generate();

// Output example image - put texts and watermark
$mg->clear();
$mg->set_top_text( $_POST['top_text'] );
$mg->set_bottom_text( $_POST['bottom_text'] );
$mg->set_output_dir( './tmp/' ); // default to ./ if not set
$mg->set_image( $example_image_path );
$mg->set_watermark( './tmp/php.gif' );
$mg->set_watemark_opacity(80);
$output_image2 = $mg->generate();