Instagram自动语录发帖生成器代码问题:Caption显示不符合预期
I built an Instagram auto quote post generator, but the generated caption doesn't match my expected style. Here's my code:
$folder = "files/autoquotemaker/"; $overlay = $folder."overlay.png"; $font_quote = "files/_font/"."Chicle-Regular.ttf"; $font_copyright = "files/_font/"."KrinkesRegularPERSONAL.ttf"; $filename = $folder.md5(rand(000,999)).".png"; $quote = @$_POST['quote'] ? $_POST['quote'] : 'YOUR QUOTE'; $copyright = @$_POST['copyright'] ? $_POST['copyright'] : 'bacotangw9'; $usernameig = @$_POST['usernameig'] ? $_POST['usernameig'] : bacotangw__; $backgrond = @$_POST['background']; $caption = "MIME-Version: 1.0" . "\r\n"; $caption .= "Content-type:text/html;charset=iso-8859-1" . "\r\n"; $caption = "Quotes dari @$usernameig \r\n \r\n yuk buat quotes mu sendiri dengan cara klik link yg ada di bio \r\n #quotes #quotestoday #qotd #quote #quotesoftheday #todayquotes #todayquote #dailyquotes #quotesdaily";My expected caption style is:
Quotes from @$usernameig let's make your own quotes by clicking on the link in the bio #quotes #quotestoday #qotd #quote #quotesoftheday #todayquotes #todayquote #dailyquotes #quotesdaily
What's wrong with my code?
Let's break down the exact issues in your code that are causing the caption to deviate from your expected format:
1. Unnecessary MIME headers get overwritten (and they're irrelevant)
You first initialize $caption with email-style MIME headers, but then immediately overwrite the entire variable with your actual caption text. Worse, these headers serve no purpose for Instagram captions—Instagram doesn’t parse MIME headers for post captions, so you can delete those two lines entirely.
2. Hardcoded Indonesian text instead of your desired English phrasing
Your current caption uses Indonesian phrases (Quotes dari, yuk buat quotes mu sendiri dengan cara klik link yg ada di bio) but your expected output uses English. You need to replace those lines with the exact English wording you want.
3. Unwanted line breaks (\r\n)
Your code adds \r\n \r\n and another \r\n before the hashtags, which creates visible line breaks in the Instagram caption. Since your expected output has no line breaks, you should remove all these newline characters.
4. Missing quotes around the default username value
The line $usernameig = @$_POST['usernameig'] ? $_POST['usernameig'] : bacotangw__; has a subtle bug: bacotangw__ isn’t wrapped in quotes, so PHP will treat it as a constant. If that constant isn’t defined, this will throw an error (or in older PHP versions, fall back to using the string bacotangw__ by accident). Wrap it in single quotes to make it a proper string literal.
Fixed Code Snippet
Here’s the corrected section (including the fixed username line) to match your expected format:
$folder = "files/autoquotemaker/"; $overlay = $folder."overlay.png"; $font_quote = "files/_font/"."Chicle-Regular.ttf"; $font_copyright = "files/_font/"."KrinkesRegularPERSONAL.ttf"; $filename = $folder.md5(rand(000,999)).".png"; $quote = @$_POST['quote'] ? $_POST['quote'] : 'YOUR QUOTE'; $copyright = @$_POST['copyright'] ? $_POST['copyright'] : 'bacotangw9'; // Fixed: added quotes around default username to avoid constant errors $usernameig = @$_POST['usernameig'] ? $_POST['usernameig'] : 'bacotangw__'; $backgrond = @$_POST['background']; // Removed irrelevant MIME headers, used English text, and removed line breaks $caption = "Quotes from @$usernameig let's make your own quotes by clicking on the link in the bio #quotes #quotestoday #qotd #quote #quotesoftheday #todayquotes #todayquote #dailyquotes #quotesdaily";
Quick Extra Tips
- Using
@to suppress errors (like@$_POST['quote']) is generally not recommended—it hides useful debugging info. Consider checking for existing values with!empty()instead:$quote = !empty($_POST['quote']) ? $_POST['quote'] : 'YOUR QUOTE'; rand(000,999)generates a number between 0 and 999, but leading zeros won’t be preserved (since it’s an integer). If you need a 3-digit string with leading zeros, usestr_pad(rand(0,999), 3, '0', STR_PAD_LEFT)instead.
内容的提问来源于stack exchange,提问作者Adi Darmawan

