Jump to content

Image manipulation, auto image resizing,.


Jragon

Recommended Posts

Hey guys,

 

I am making a quotes siggy, code:

<?php

/**
* @author Jragon
* @copyright 2010
*/

$textfile = "quotes.txt";
$quotes = array();
if (file_exists($textfile)) {
    $quotes = explode("\n", file_get_contents($textfile));
    srand((float)microtime() * 10000000);
    $string = $quotes[array_rand($quotes)];
    $string = wordwrap($string, 100, "\n", true);
    text_to_image($string, 800);
} else {
    $string = "Sig file non-existant...";
}
function text_to_image($text, $image_width, $colour = array(0, 244, 34), $background =
    array(0, 0, 0))
{
    $font = 5;
    $line_height = 15;
    $padding = 2;
    $text = wordwrap($text, ($image_width/10));
    $lines = explode("\n", $text);
    $image = imagecreate($image_width, ((count($lines) * $line_height)) + ($padding *
        2));
    $background = imagecolorallocate($image, $background[48], $background[44], $background[44]);
    $colour = imagecolorallocate($image, $colour[0], $colour[1], $colour[2]);
    imagefill($image, 0, 0, $background);
    $i = $padding;
    foreach ($lines as $line) {
        imagestring($image, $font, $padding, $i, trim($line), $colour);
        $i += $line_height;
    }
    header("Content-type: image/jpeg");
    imagejpeg($image);
    imagedestroy($image);
    exit;
}
?>

quote

 

What i want it to do is have the lines the same legnth, and when a quote is smaller the image to shrink.

 

 

Thanks

 

jragon

Link to comment
Share on other sites

This is my new code:

<?php

/**
* @author Jragon
* @copyright 2010
*/

$textfile = "quotes.txt";
$quotes = array();
if (file_exists($textfile)) {
    $quotes = explode("\n", file_get_contents($textfile));
    srand((float)microtime() * 10000000);
    $string = $quotes[array_rand($quotes)];
    $string = wordwrap($string, 100, "\n", true);
} else {
    $string = "Sig file non-existant...";
}
function text_to_image($text){
    //test
    $width = imagefontwidth($font_size) * strlen($string);
    $height = imagefontheight($font_size);
    echo $width . "<br />" . $hight;
    //end test
    $colour = array(0, 244, 34);
    $background = array(0, 0, 0);
    $font_size = 5;
    $line_height = 15;
    $padding = 2;
    $text = wordwrap($text, ($image_width/10));
    $lines = explode("\n", $text);
    $image = imagecreate($width, $height);
    $background = imagecolorallocate($image, $background[48], $background[44], $background[44]);
    $colour = imagecolorallocate($image, $colour[0], $colour[1], $colour[2]);
    imagefill($image, 0, 0, $background);
    $i = $padding;
    foreach ($lines as $line) {
        imagestring($image, $font, $padding, $i, trim($line), $colour);
        $i += $line_height;
    }
    header("Content-type: image/jpeg");
    imagejpeg($image);
    imagedestroy($image);
    exit;
}
?>

 

It now outputs just the width and doesnt make a picture

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.