I am using a script to write text into an image. Users can select their font size, color, image size, etc. I am having trouble centering the text in the image. Can anyone help me out with this?
Are you using GD or ImageMagick (what is your code thus far?) also the "save as" thats dependent on the browser, if you were to use mod_rewrite in an .htaccess you could make the url to the php look like a jpeg that might make it easier, but the browser should do such automatically noticing a image header.
<?php header('Content-type: image/png'); // Start The Image $im = @imagecreate($_POST['width'] , $_POST['height']); $center2 = $_POST['width'] - $_POST['size']; $center2 = $center2 / 2; $num = strlen($_POST['word'] * $_POST['size']); $center1 = $_POST['height']; - $num; $center1 = $center1 / 2; // Set Text Color function hexrgb($hexstr) { $int = hexdec($hexstr); return array("red" => 0xFF & ($int >> 0x10), "green" => 0xFF & ($int >> 0x8), "blue" => 0xFF & $int); } $colorarr = hexrgb($_POST['bgcolor']); $background_color = imagecolorallocate($im, $colorarr['red'], $colorarr['green'] , $colorarr['blue']); $colorarr2 = hexrgb($_POST['color']); $text_color = imagecolorallocate($im, $colorarr2['red'], $colorarr2['green'] , $colorarr2['blue']); // Size $_POST['size'] = $size; if($size == '0' || $size > 99 || strlen($size) == 0) { $size = '12'; } else { } // Words/Font if(strlen($_POST['word']) < 1 && strlen($_POST['word']) > 0) { $Word = $_POST['word']; } elseif(strlen($_POST['word']) > 0) { $Word = $_POST['word']; } else { $Word = 'Preview'; } if(strlen($_POST['sel']) < 1 && strlen($_POST['font']) > 0) { $Font = $_POST['font']; } elseif(strlen($_POST['font']) > 0) { $Font = $_POST['font']; } else { $Font = 'Pretendo'; } if(file_exists('./fonts' . $_POST['sel'] . '.ttf')) { $Font = './fonts' . $_POST['sel'] . '.ttf'; } elseif(file_exists('/fonts' . $_POST['sel'] . '.ttf')) { $Font = '/fonts' . $_POST['sel'] . '.ttf'; } elseif(file_exists('./' . $_POST['sel'] . '.ttf')) { $Font = './' . $_POST['sel'] . '.ttf'; } else { $Font = '/home/nintendo/public_html/pbfun/fonts/' . $_POST['sel'] . '.ttf'; } // Write Text imagefttext($im, $size, 0, $center2, $center1, $text_color, $Font, $_POST['word']); // Clearer Text imagepng($im); imagedestroy($im); ?> As you can see I already tried to center it but it is unsucessful.