Log in

View Full Version : Write text over (existing) image



[Nicolas]
02-06-2011, 01:30 AM
How could I use and/or transform this code to work with an existing image? I'd like for someone to be able to type in text and it would show up on an existing image.

<?php
// Create image handle
$im = imagecreatetruecolor(200, 200);

// Allocate colors
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);

// Load the PostScript Font
$font = imagepsloadfont('font.pfm');

// Write the font to the image
imagepstext($im, 'Sample text is simple', $font, 12, $black, $white, 50, 50);

// Output and free memory
header('Content-type: image/png');

imagepng($im);
imagedestroy($im);
?>


Please. Thanks!
EDIT: Also, how can I change the font.pfm to a font.ttf? Thanks!

djr33
02-06-2011, 01:45 AM
Regarding the font, look here: http://www.php.net/manual/en/function.imagettftext.php
Looks like you can remove the "load font" line and replace the existing line with that.


To use an existing image, you will replace imagecreatetruecolor() with imagecreatefromjpeg(), or ...gif(), ...png(), etc.
Here's the one for jpeg as an example:
http://www.php.net/manual/en/function.imagecreatefromjpeg.php


I can also share with you some image functions that I use to simplify the process of working with images of various filetypes. If you always use the same filetype (such as jpeg) then this won't be important, but if you are using user-uploaded images, then this is a good way to approach it:

function imagefiletype($file) { //use either to find out how to check the image or if you already know it's a valid image to find out what type it is
$file = basename($file);
$ext = strpos($file,'.')!==FALSE?substr($file,strrpos($file,'.',1)+1):$file; //find extension: defined as part of name after the last .
$ext = strtolower($ext); //case insensitive extensions
if ($ext=='jpg'||$ext=='jpeg'||$ext=='jpe') {
return 'jpg';
}
else if ($ext=='gif') {
return 'gif';
}
else if ($ext=='png') {
return 'png';
}
return FALSE;
}

function imagecreatefromfile($file,$path='') {
if ($path=='') { $path=$file; } //in almost all cases, except file uploads [where name!=tmp_path]
$type = imagefiletype($file);
$im = FALSE; //default
if ($type=='jpg') {
$im = imagecreatefromjpeg($path);
}
else if ($type=='gif') {
$im = imagecreatefromgif($path);
}
else if ($type=='png') {
$im = imagecreatefrompng($path);
}
return $im;
}

By the way, you can also use bmp format images if you google "imagecreatefrombmp php" since there are some functions out there that are written to deal with bmp images even though it's not supported natively. There aren't many other formats supported though.

[Nicolas]
02-06-2011, 02:00 AM
Thanks. But a couple things, a user will upload an image, but it will be on a certain part of the image. And, how can I use this? Sorry, I know .000000001% of any PHP. :P

djr33
02-06-2011, 02:14 AM
Here's a function that can crop an image as desired:
http://www.php.net/manual/en/function.imagecopy.php

Start experimenting with PHP and you can learn it quickly.
Be warned, though, that the GD image library is one of the hardest parts. However, having examples like that to start with will help. Don't give up on PHP if the image functions are too hard, since that's true for everyone using them ;)

[Nicolas]
02-06-2011, 02:16 AM
Thanks!!! :D I will start as soon as possible, I just found out my webhost DOES NOT support PHP anymore :( Let me google one real quick. But thanks :)