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
https://forums.phpfreaks.com/topic/218723-image-manipulation-auto-image-resizing/
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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.