DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. How We Use PHP to Generate Instagram Images

How We Use PHP to Generate Instagram Images

In this brief tutorial, we learn how one company uses PHP to create automated Instagram posts with quotes and avatars.

Mads Phikamphon user avatar by
Mads Phikamphon
·
Apr. 20, 20 · Tutorial
Like (2)
Save
Tweet
Share
4.53K Views

Join the DZone community and get the full member experience.

Join For Free

Unlike most people's Instagram, our Instagram is almost 100% automatic. For example, we use code to generate our images.

Why Automatic Images

On our website, we interview people who do great in fitness. Most of the interviews have great, motivating quotes that people in fitness love, so, inspired by Foundr's Instagram, it was kind of obvious that our Instagram images should also be quotes.

All our interviews have a personalized avatar attached to them, so we decided to also add the avatar and the name of the person behind the quote to each image.

Getting the Quotes

In our interviews, the quotes are shown using a Wordpress shortcode (since our site runs on WP). Like this: 

[quote]I once did 5,500 burpees in 24 hours.[/quote]

The shortcode above leads to this being generated on the page (live here):

Generated quote

When the shortcode is run, the quote is saved to a custom table in the database (wp_[prefix]_quotes). Once a week, an editor take a look at any new quotes and mark the ones that might do well on Instagram.

Putting the Images Together

First, we create a base image, which is just a white image with the right dimensions. In our case, that's 600 x 600 since that resolution is good enough for Instagram and square-like Instagram likes.

PHP
 




x


 
1
$quote_image = imagecreatetruecolor( 600, 600 );
2
imagefill($quote_image, 0, 0, imagecolorallocate($quote_image, 255, 255, 255));



We then set up the colors for the texts:

PHP
 




xxxxxxxxxx
1


 
1
$text_color_black = imagecolorallocate($quote_image, 0, 0, 0);
2
$text_color_orange = imagecolorallocate($quote_image, 255, 127, 39);


Adding the text to the images is done using imagettftext(). That function takes a lot of parameters and it's no secret that we tried many different positions and font sizes before the text ended up looking perfect on the images (especially because the names and quotes all have different lengths). 

We add one line at a time to the image using imagettftext(). Like this:

PHP
 




xxxxxxxxxx
1


 
1
for($i = 0; $i < sizeof($quote_lines); $i++) { 
2
    $text_line_y = ...; // increment the y so the lines appear below each other. 
3

          
4
    imagettftext($quote_image, $text_size, 0, $text_line_x, $text_line_y, $text_color_black, $font_path, $quote_lines[$i]); 
5
}



The avatar image we add using  imagecopyresampled(). Like this: 

PHP
 




xxxxxxxxxx
1


 
1
imagecopyresampled($quote_image, $avatar_image_portrait, $avatar_x, $avatar_y, 0, 0, $avatar_size, $avatar_size, 600, 600);


The parameters  $avatar_size is the size of the avatar image — and since our avatar images are square, we can use the same parameter twice.

Finally, we add our logo. Smart readers might have spotted the two orange "//" in our logo could be text — and yes, that's what it is. 2 x 3 "/" characters placed very close to each other with  imagettftext():

PHP
 




xxxxxxxxxx
1


 
1
imagettftext($quote_image, $logo_text_size, 0, $logo_x, $logo_y, $text_color_orange, $font_path, "/");
2
imagettftext($quote_image, $logo_text_size, 0, $logo_x + 3, $logo_y, $text_color_orange, $font_path, "/");
3
imagettftext($quote_image, $logo_text_size, 0, $logo_x + 6, $logo_y, $text_color_orange, $font_path, "/");
4
imagettftext($quote_image, $logo_text_size, 0, $logo_x + 14, $logo_y, $text_color_orange, $font_path, "/");
5
imagettftext($quote_image, $logo_text_size, 0, $logo_x + 17, $logo_y, $text_color_orange, $font_path, "/");
6
imagettftext($quote_image, $logo_text_size, 0, $logo_x + 20, $logo_y, $text_color_orange, $font_path, "/");


After the logo has been added, we just need to save the image and drop the temporary image we have been working on:

PHP
 




xxxxxxxxxx
1


 
1
imagepng($quote_image, $filename);
2
imagedestroy($quote_image);


Other temporary images must also be destroyed, like  imagedestroy($avatar_image_portrait);.


PHP Instagram

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Why Open Source Is Much More Than Just a Free Tier
  • Deploying Java Serverless Functions as AWS Lambda
  • Using AI and Machine Learning To Create Software
  • Top Authentication Trends to Watch Out for in 2023

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: