简体   繁体   中英

PHP:GD text over image script

I have a simple facebook aplication. I have the index.php and the image.php which when accessed it creates an image with a random text on it(at least this is what i want to get from the script). Here is the image.php: http://pastebin.com/B4JhHcfj

The script is incomplete because i don't know how to get the text on the generated image. Example: I access the image.php, i get a image with a random text on it.If i access again the link the same image with the random text on it will be shown.Thanks

Ok , you can use GD to watermark images , the complete script for watermarking :

<?php  

header('content-type: image/jpeg'); 

$watermark = imagecreatefrompng('watermark.png');  
$watermark_width = imagesx($watermark);  
$watermark_height = imagesy($watermark);  
$image = imagecreatetruecolor($watermark_width, $watermark_height);  
$image = imagecreatefromjpeg($_GET['src']);  
$size = getimagesize($_GET['src']);  
$dest_x = $size[0] - $watermark_width - 5;  
$dest_y = $size[1] - $watermark_height - 5;  
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100); 
imagejpeg($image);  
imagedestroy($image);  
imagedestroy($watermark);  

?>

please note that , this script create watermarks on the fly , if you want to save watermarked image you must use imagejpeg() function with this way :

imagejpeg($image,'watermarked_img.jpg'); 

and you also use .htaccess to redirect all image request to your watermark.php ,create .htaccess file and paste this code on it :

RewriteEngine on
RewriteRule ^([^thumb].*\.[jJ].*)$ watermark.php?src=$1

good luck.

I did a PHP class to make it easier:

http://alvarotrigo.com/blog/using-php-gd-library-to-write-text-over-images-using-truetype-fonts/#comment-1048371709

It works with true type fonts (ttf) to be able to write text in big font sizes.

I presume from your question, you have already included GD and that your PHP script generates the graphic using GD.

Firstly, you have to ensure that the required fonts are installed in the folder(s) where PHP will look for them.

Then, you should take a look at the following function, ImageTTFText:

http://php.net/manual/en/function.imagettftext.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM