...

View Full Version : Text on image and saving the new jpeg into a folder



Len Whistler
01-30-2007, 12:25 AM
Below is a script that will put text onto a jpeg and output it to the browser. But I would like to save the new image with text into a folder on the server or display the new image in a html page. The original jpeg is already on the server. If I was uploading the jpeg and adding text I would be able to put it into a folder but I seem to have problems because the original image is already on the server.

The purpose of this php script is to add the copyright information as the image is displayed. The copyright info then can be changed at anytime without having to adjust the actual jpegs, this would be good if you have 100's of images.


<?php
$filename = 'test.jpg';
header('Content-type: image/jpeg');

$image_size = getimagesize($filename);

$image_t = imagecreatetruecolor($image_size[0], $image_size[1]);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_t, $image, 0, 0, 0, 0, $image_size[0], $image_size[1], $image_size[0], $image_size[1]);

$white = imagecolorallocate($image_t, 255, 255, 255);
$text_size = '24';
$text_x = '20';
$text_y = $image_size[1]-20;
$text = 'Copywrite 2007';
$font = 'arial.ttf';
imagettftext($image_t, $text_size, 0, $text_x, $text_y, $white, $font, $text);

imagejpeg($image_t, null, 100);

imagedestroy($image_t);
?>

Mhtml
01-30-2007, 02:09 AM
<?php
$filename = 'test.jpg';
header('Content-type: image/jpeg'); <--- remove this line

$image_size = getimagesize($filename);

$image_t = imagecreatetruecolor($image_size[0], $image_size[1]);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_t, $image, 0, 0, 0, 0, $image_size[0], $image_size[1], $image_size[0], $image_size[1]);

$white = imagecolorallocate($image_t, 255, 255, 255);
$text_size = '24';
$text_x = '20';
$text_y = $image_size[1]-20;
$text = 'Copywrite 2007';
$font = 'arial.ttf';
imagettftext($image_t, $text_size, 0, $text_x, $text_y, $white, $font, $text);

imagejpeg($image_t, null, 100); <--- change this line

imagedestroy($image_t);
?>


Those highlighted lines are the ones you need to correct. Obviously you should wrap this up into a function as well.


int imagejpeg ( resource image [, string filename [, int quality]])


imagejpeg() creates the JPEG file in filename from the image image. The image argument is the return from the imagecreate() function.

The filename argument is optional, and if left off, the raw image stream will be output directly.

Seeing as you won't be dumping the stream to the client you won't need to post jpeg headers.

Len Whistler
01-30-2007, 02:58 AM
Thanks Mhtml.....Script works perfectly now with those changes and I will look into the function as well. :thumbsup:

Mhtml
01-30-2007, 03:48 AM
No problems mate. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum